Xml Namespace breaking my xpath! Xml Namespace breaking my xpath! xml xml

Xml Namespace breaking my xpath!


I also have the following xPath:

/List/Fields/Field 

When I remove the xmlns from my XML the xPath works fine. When it's in there my xPath finds nothing

If you cannot register a namespace binding and cannot use (assuming the registered prefix is "x"):

/x:List/x:Fields/x:Field

then there is another way:

/*[name()='List']/*[name()='Fields']/*[name()='Field']


The List element has been defined with a default namespace and this is adopted by all elements inside.

You therefore need to ignore the element namespace like so:

/*[local-name()='List']/*[local-name()='Fields]/*[local-name()='Field]

but this means that the xpath will pick up any other element with List - Fields - Field

You can do a namespace check as well as a local-name check like so:

/*[local-name()='List' and namespace-uri()='http://schemas.microsoft.com/sharepoint/soap/']/*[local-name()='Fields' and namespace-uri()='http://schemas.microsoft.com/sharepoint/soap/']/*[local-name()='Field' and namespace-uri()='http://schemas.microsoft.com/sharepoint/soap/']

Or you can register the namespace with the library and then explicitly specify the prefix for that namespace and add it to xpath expression, the method of which is dependent on the library you are using.


You most likely have to register that namespace uri with your xpath library. Depending on the library, you may be able to use the 'default' prefix or you may need to give it a named prefix and use that in your xpath queries.

For example, in php (since you didn't specify a language) using DOMXPath you could do something like this:

$xpath = new DOMXPath($document);$xpath->registerNamespace('x', 'http://schemas.microsoft.com/sharepoint/soap/');$xpath->query('/x:List/x:Fields/x:Field');