Using XPath for XML Documents with Default Namespace
When working with XML documents featuring a default namespace, it may seem challenging to utilize XPath expressions without explicitly declaring namespace URIs. However, there are approaches to address this situation.
Setting Namespace Awareness to False
As mentioned in the original question, the initial understanding was that setting namespaceAware to false in DocumentBuilderFactory would eliminate the need to work with namespace URIs. However, this approach is not suitable for documents with a default namespace. The default namespace requires explicit handling.
Utilizing NamespaceContext
An alternative solution involves employing a NamespaceContext during XPath execution. This allows you to associate prefixes with namespaces within the XPath expression. The prefixes used can be different from those defined in the XML document.
Here's an example implementing this approach:
<code class="java">import javax.xml.namespace.NamespaceContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class Demo { public static void main(String[] args) { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); try { DocumentBuilder builder = domFactory.newDocumentBuilder(); Document dDoc = builder.parse("E:/test.xml"); XPath xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new MyNamespaceContext()); NodeList nl = (NodeList) xPath.evaluate("/ns:root/ns:author", dDoc, XPathConstants.NODESET); System.out.println(nl.getLength()); } catch (Exception e) { e.printStackTrace(); } } private static class MyNamespaceContext implements NamespaceContext { public String getNamespaceURI(String prefix) { if("ns".equals(prefix)) { return "http://www.mydomain.com/schema"; } return null; } public String getPrefix(String namespaceURI) { return null; } public Iterator getPrefixes(String namespaceURI) { return null; } } }</code>
In this example, the provided NamespaceContext maps the 'ns' prefix to the namespace URI used in the XML document.
Revised XPath Expression
Dennis rightly points out that the original XPath expression "/root/author" is incorrect. For documents with a default namespace, the correct expression should be "/ns:root/ns:author". This aligns with the newly introduced namespace prefixes in the code.
Simplification with Namespace Awareness
Interestingly, it appears that setting namespaceAware to true and omitting the NamespaceContext also provides the desired results. This is demonstrated in the following code:
<code class="java">import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class Demo { public static void main(String[] args) { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); try { DocumentBuilder builder = domFactory.newDocumentBuilder(); Document dDoc = builder.parse("E:/test.xml"); XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nl = (NodeList) xPath.evaluate("/root/author", dDoc, XPathConstants.NODESET); System.out.println(nl.getLength()); } catch (Exception e) { e.printStackTrace(); } } }</code>
In this case, the familiar XPath expression "/root/author" can be used, as the document's namespace is recognized and implied during processing.
The above is the detailed content of How to Handle XML Documents with Default Namespace Using XPath?. For more information, please follow other related articles on the PHP Chinese website!