Home > Java > javaTutorial > How to Handle XML Documents with Default Namespace Using XPath?

How to Handle XML Documents with Default Namespace Using XPath?

Patricia Arquette
Release: 2024-10-24 07:00:02
Original
1096 people have browsed it

How to Handle XML Documents with Default Namespace Using XPath?

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>
Copy after login

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>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template