Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml Namespace breaking my xpath!

I have the following XML:

<List xmlns="http://schemas.microsoft.com/sharepoint/soap/">  <Fields>    <Field>    </Field>  </Fields> </List> 

This is a slimmed down version of XML being returned from a SharePoint web service. 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. Is there something I should be doing differently with my xPath? Modifying the XML is not an option.

like image 869
Abe Miessler Avatar asked Mar 08 '11 23:03

Abe Miessler


People also ask

How does XPath handle namespace?

XPath queries are aware of namespaces in an XML document and can use namespace prefixes to qualify element and attribute names. Qualifying element and attribute names with a namespace prefix limits the nodes returned by an XPath query to only those nodes that belong to a specific namespace.

How do you prevent a name conflict in XML?

Name conflicts in XML can easily be avoided using a name prefix. In the example above, there will be no conflict because the two <table> elements have different names.

How do I change the default namespace in XML?

You can change the default namespace within a particular element by adding an xmlns attribute to the element. Example 4-4 is an XML document that initially sets the default namespace to http://www.w3.org/1999/xhtml for all the XHTML elements.


1 Answers

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'] 
like image 100
Dimitre Novatchev Avatar answered Sep 29 '22 06:09

Dimitre Novatchev