Skip to main content

XPath Problems - why can't I match elements?

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2007-01-02

Here is some XML code:

<CxF xmlns:xsi="" xmlns="" xsi:schemaLocation=" CxF3_Core.xsd">
        <Creator>X-Rite - RFW</Creator>
        <Description>Example CXF3 file</Description>

and some Java code
    XPath xpath = xPathFactory.newXPath();
    XPathExpression expr = xpath.compile("*");
    Object result = expr.evaluate(documentElement, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    if (nodes.getLength() < 1)
        getLogger().error("not found");
    for (int i = 0; i < nodes.getLength(); i++) {
        getLogger().info(" node name = " + nodes.item(i).getNodeName());

which produces
[2011-04-30 08:18:15,845]  INFO    95[main] -  node name = FileInformation
[2011-04-30 08:18:15,845]  INFO    95[main] -  node name = Resources
[2011-04-30 08:18:15,845]  INFO    95[main] -  node name = CustomResources

Now, if I change
    XPathExpression expr = xpath.compile("*");

    XPathExpression expr = xpath.compile("FileInformation");
    XPathExpression expr = xpath.compile("//FileInformation");
    XPathExpression expr = xpath.compile("/CxF/FileInformation");
    XPathExpression expr = xpath.compile("/*/FileInformation");

or any number of permutations that should work, I always get
[2011-04-30 08:28:48,739] ERROR    97[main] - not found

I've been playing around with this for hours, and as far as I can tell, it is impossible to match any element names. Is there some configuration problems I am missing somewhere?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-06-22

The problem is that your XML document defines an XML namespace identified by the URL ""
(In fact, it is a default namespace, since it uses no prefix.)
Thus, there is actually no root node "CxF", but rather a root node "someUndefinedNamespacePrefix:CxF".
You need to do the following things in order to make your XPath expression work:
1. Make sure your document was parsed with namespace-awareness switched on (See
2. Implement the interface and map the namespace URL to an arbitrary prefix.
3. Tell the XPath about your namespace context, using