10

I have the following XPath expression:

/configuration/properties

And this is my XML:

<configuration
    xmlns="http://www.ksharma.in/myXSD">
    <properties>
        <property key="a" value="1" />
        <property key="b" value="2" />
    </properties>
</configuration>

The XPath does not work. However If I change the name space from xmlns to xmlns:conf it works:

<configuration
    xmlns:conf="http://www.ksharma.in/myXSD">
    <properties>
        <property key="a" value="1" />
        <property key="b" value="2" />
    </properties>
</configuration>

Why is this so?

Serg
  • 2,346
  • 3
  • 29
  • 38
Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169
  • 2
    Search this site for "XPath default namespace" and you will get 1030 answers to your question, nearly all of them correct. – Michael Kay May 06 '14 at 11:49
  • 3
    @MichaelKay You're right. But for that one should know what to search for (And I'm not the author of Author of `XSLT 2.0 and XPath 2.0 Programmer's Reference`) :) – Kshitiz Sharma May 07 '14 at 03:30
  • Yes, finding the right search terms is difficult, especially for people who haven't learnt the terminology. However, the title you chose for your question is good enough to get 223 hits. – Michael Kay May 07 '14 at 07:25

4 Answers4

17

Placing xmlns="http://www.ksharma.in/myXSD" on the root element of your XML puts the root and its descendants in the http://www.ksharma.in/myXSD namespace. This effectively means that all of the element names in your XML document are preceded by http://www.ksharma.in/myXSD. Yet, the elements stated in your XPath are not in the http://www.ksharma.in/myXSD namespace. Thus, your XPath matches nothing.

Placing xmlns:conf="http://www.ksharma.in/myXSD" instead on the root element merely defines a prefix for the http://www.ksharma.in/myXSD namespace but doesn't actually use it. The root element and its descendants remain in no namespace and are therefore able to be found by your XPath that also tests in no namespace. Thus, your XPath matches something.

See also How does XPath deal with XML namespaces?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
7

That is because you are in a default namespace xmlns="http://www.ksharma.in/myXSD". You can try

/*[local-name()='configuration']/*[local-name()='properties']

instead.

Joel M. Lamsen
  • 7,143
  • 1
  • 12
  • 14
3

Namespace wihtout prefix is a default namespace. Having default namespace, XML element where the namespace is declared and it's descendants without prefix and without different non-prefixed namespace declaration considered in the same namespace.

The second XML above has namespace declaration with prefix. In this case, for an element to be considered in that prefixed-namespace it has to be declared explicitly using corresponding prefix.

To be able to access elements in default namespace you have to declare a prefix that point to default namespace URI and use that prefix in your XPath query (or ignore the namespace by using local-name() as suggested in @Joel's answer).

har07
  • 88,338
  • 12
  • 84
  • 137
0

Check this article.

Change your xpath config to /\*:configuration/\*:properties and you should be good to go.

James Risner
  • 5,451
  • 11
  • 25
  • 47