Skip to main content Skip to complementary content

XML namespace issue

This section describes an issue that can occur when using XPath expressions with XML documents containing namespaces. For more information about namespaces, see XML namespace and Namespaces in XML.

Description

Currently, XPath expressions work as expected when the XML document does not have any namespaces. However, with any document containing namespaces, including default namespaces, XPath expressions can return unexpected results.

For example, with the XML document below, the expressions /a, //c, and //c/@name all return an empty array:
<a xmlns="http://www.w3.org/2000/example">
  <b>
    <c name="name1"/>
  </b>
</a>

Workaround

To avoid this issue, you can use an XPath expression that fetches a tag by its name, which allows you to ignore the namespace. For the examples above, you can use /*[name()='a'], //*[name()='c'], and //*[name()='c']/@name.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!