martedì 15 maggio 2012

XPath with namespaces in Java


http://blog.davber.com/2006/09/17/xpath-with-namespaces-in-java/

XPath with namespaces in Java

XPath is the expression language operating on an XML tree, used from XSLT. It can also be used stand-alone, such as from a Java application. There is also a standard API, called JAXP, for Java. So, everything is nice and dandy, and the post should stop here! Well... for real XML documents, using namespaces, it is not this easy. That is why you need this post!
If you do not care about lengthy posts, but just want the bare facts, go to the end of this post :-)
Let us first look at a simple example where the XML documents do not have namespaces. Say we have this XML document with some orders:
XML:
  1. <? xml version="1.0"?>
  2. <Sales>
  3. <Customer name="CostCo, Inc.">
  4. <Order price="2000">
  5.   <Description>A tiny web site</Description>
  6. </Order>
  7. <Order price="12000">
  8.   <Description>Database configuration</Description>
  9. </Order>
  10. </Customer>
  11. <Customer name="BigBuyer, Inc.">
  12. <Order price="30000">
  13.   <Description>A J2EE system for lawyers</Description>
  14. </Order>
  15. </Customer>
  16. </Sales>
A quick XPath to get all big orders from all customers is:
XPATH:
//Customer/Order[@price> 10000]
Sun provides us with an API, JAXP, for accessing and manipulating XML documents, including using XPath. So, let us see exactly what we need to do to use XPath.
If you use Java 5.0, then you already have JAXP at your fingertips. Version 1.3 of JAXP, to be exact. Having an API is no fun without a corresponding implementation of it, though. Fortunately, J2SE comes with a reference implementation, based on Xerces and Xalan. This in contrast to Java 1.4, which used Crimson. There are some crucial differences between these versions, but I will leave that discussion - it is just too painful ;-)
Using Java 5.0, a complete application, which you can just compile and run, follows:
JAVA:
  1. package com.davber.test;
  2. import javax.xml.parsers.*;
  3. import javax.xml.xpath.*;
  4.  
  5. import org.w3c.dom.Document;
  6.  
  7. public class App
  8. {
  9.     public static void main( String[] args )
  10.     {
  11.         try {
  12.             // First, the XML document
  13.            
  14.             String xmlStr =
  15.                 "<?xml version=\"1.0\" ?>\n" +
  16.                 "<Sales xmlns=\"http://www.davber.com/sales-format\">\n" +
  17.                 "<Customer name=\"CostCo, Inc.\">\n" +
  18.                 "<ord:Order xmlns:ord=\"http://www.davber.com/order-format\" price=\"12000\">\n" +
  19.                 "<ord:Description>A bunch of stuff" +
  20.                 "</ord:Description>\n" +
  21.                 "</ord:Order>\n" +
  22.                 "</Customer>\n" +
  23.                 "</Sales>\n";
  24.             DocumentBuilderFactory xmlFact =
  25.                 DocumentBuilderFactory.newInstance();
  26.             xmlFact.setNamespaceAware(true);
  27.             DocumentBuilder builder = xmlFact.
  28.                 newDocumentBuilder();
  29.             Document doc = builder.parse(
  30.                     new java.io.ByteArrayInputStream(
  31.                             xmlStr.getBytes()));
  32.            
  33.             // Now the XPath expression
  34.            
  35.             String xpathStr =
  36.                 "//Customer/Order[@price> 6000]";
  37.             XPathFactory xpathFact =
  38.                 XPathFactory.newInstance();
  39.             XPath xpath = xpathFact.newXPath();
  40.             String result = xpath.evaluate(xpathStr, doc);
  41.             System.out.println("XPath result is \"" +
  42.                     result + "\"");
  43.         }
  44.         catch (Exception ex) {
  45.             ex.printStackTrace();
  46.         }
  47.     }
  48. }
Compile and run it now. You will see

XPath result is "A bunch of stuff"

i.e., it matched one order.
So, let us get down to business and add some namespaces to the input document.
XML:
  1. <? xml version="1.0"?>
  2. <Sales xmlns="http://www.davber.com/sales-format">
  3. <Customer name="CostCo, Inc.">
  4. <ord:Order xmlns:ord="http://www.davber.com/order-format" price="2000">
  5.   <ord :D escription>A tiny web site</ord :D escription>
  6. </ord:Order>
  7. <ord:Order price="12000">
  8.   <ord :D escription>Database configuration</ord :D escription>
  9. </ord:Order>
  10. </Customer>
  11. <Customer name="BigBuyer, Inc.">
  12. <ord:Order price="30000">
  13.   <ord :D escription>A J2EE system for lawyers</ord :D escription>
  14. </ord:Order>
  15. </Customer>
  16. </Sales>
So we have two namespaces, where one is the the default namespace, i.e.,added to all local names without an explicit prefix. This is important to understand, since that means they will not match names without namespace in XPath. So, even for the default namespace, we need to explicitly add a namespace to the XPath! Let us try without namespaces, i.e., use the exact same code as before but with the namespace-annotated variant of the input XML.
You will get this intriguing output:

XPath result is ""
Yep, nothing! Your non-namespaced XPath did not match the now namespaced input. No real surprise here. What might come as a surprise is that we cannot even use local names in the XPath expression for names in the default namespace. Try with the XPath
XPATH:
/Sales/Customer/@name
which should give us the concattenation of all the customer names. Replace the XPath of the application and recompile and run. You will again see this output:

XPath result is ""

i.e., no match!
So, as soon as we have namespaces in the XML document, default or not, we need to use namespaces in the XPath. We will later see how can circumvent this requirement, but let us now take it as a gospel :-)
How do we adorn the XPath with namespaces? Well, just as done in the XML document, there has to be a mapping between prefixes and URIs. The problem in the stand-alone XPath case - as opposed to when it is used within an XSLT document - is that there is no place to do it in the XPath "document" itself.
Can we not just use the same prefixes as the XML document?
No, the matching procedure only cares about the (mapped) URIs and could not care less about the specific prefix we chose. If you do not believe me, try with the prefixed XPath
XPATH:
//ord:Order/ord:Description
Believe me now?
Ok, so how do we create and provide that mapping in JAXP?
There is this interface javax.xml.namespace.NamespaceContext which declares three methods, whereof we only care about one of them, which maps namespace prefix to URI. The other two are not used by the standard XPath executing implementations (AFAIK.) The complete application with an implementation of that interface corresponding to our test XML input follows.
JAVA:
  1. package com.davber.test;
  2. import java.util.Iterator;
  3. import java.util.List;
  4.  
  5. import javax.xml.namespace.NamespaceContext;
  6. import javax.xml.parsers.*;
  7. import javax.xml.xpath.*;
  8.  
  9. import org.w3c.dom.Document;
  10.  
  11. public class App
  12. {
  13.     public static void main( String[] args )
  14.     {
  15.         try {
  16.             // First, the XML document
  17.            
  18.             String xmlStr =
  19.                 "<?xml version=\"1.0\" ?>\n" +
  20.                 "<Sales xmlns=\"http://www.davber.com/sales-format\">\n" +
  21.                 "<Customer name=\"CostCo, Inc.\">\n" +
  22.                 "<ord:Order xmlns:ord=\"http://www.davber.com/order-format\" price=\"12000\">\n" +
  23.                 "<ord:Description>A bunch of stuff" +
  24.                 "</ord:Description>\n" +
  25.                 "</ord:Order>\n" +
  26.                 "</Customer>\n" +
  27.                 "</Sales>\n";
  28.             DocumentBuilderFactory xmlFact =
  29.                 DocumentBuilderFactory.newInstance();
  30.             xmlFact.setNamespaceAware(true);
  31.             DocumentBuilder builder = xmlFact.
  32.                 newDocumentBuilder();
  33.             Document doc = builder.parse(
  34.                     new java.io.ByteArrayInputStream(
  35.                             xmlStr.getBytes()));
  36.            
  37.             // We map the prefixes to URIs
  38.            
  39.             NamespaceContext ctx = new NamespaceContext() {
  40.                 public String getNamespaceURI(String prefix) {
  41.                     String uri;
  42.                     if (prefix.equals("ns1"))
  43.                         uri = "http://www.davber.com/order-format";
  44.                     else if (prefix.equals("ns2"))
  45.                         uri = "http://www.davber.com/sales-format";
  46.                     else
  47.                         uri = null;
  48.                     return uri;
  49.                 }
  50.                
  51.                 // Dummy implementation - not used!
  52.                 public Iterator getPrefixes(String val) {
  53.                     return null;
  54.                 }
  55.                
  56.                 // Dummy implemenation - not used!
  57.                 public String getPrefix(String uri) {
  58.                     return null;
  59.                 }
  60.             };
  61.            
  62.             // Now the XPath expression
  63.            
  64.             String xpathStr =
  65.                 "//ns1:Order/ns1:Description";
  66.             XPathFactory xpathFact =
  67.                 XPathFactory.newInstance();
  68.             XPath xpath = xpathFact.newXPath();
  69.             xpath.setNamespaceContext(ctx);
  70.             String result = xpath.evaluate(xpathStr, doc);
  71.             System.out.println("XPath result is \"" +
  72.                     result + "\"");
  73.         }
  74.         catch (Exception ex) {
  75.             ex.printStackTrace();
  76.         }
  77.     }
  78. }
Now the XPath matches. Note that I did not use the same prefix as the XML document, since the choice of prefix is arbitrary. In order to match against the default namespace in the XML document, we need to use the prefix ns2then, as in the following XPath expression:
XPATH:
/ns2:Sales/ns2:Customer/@name
Try it and you will get:

XPath result is "CostCo, Inc."
If you think it is a PITA to explicitly map those prefixes to namespaces you have two choices:
  1. extract the mapping from the XML document itself
  2. disable the namespace awareness of the XML document
Let us look at the first alternative. Unfortunately (?), there is no such facility in the JAXP API, so we have to diverge into implementation specificity. Xalancontains an interface PrefixResolver (at least in version 2.7.0) which is very similar to the aforementioned JAXP interface. Unfortunately, "similar" is not the same as "compatible with", so we need to wrap the PrefixResolver asNamespaceContext to use it with the JAXP API.
Why do we want to use this non-compatible prefix resolver interface in the first case? Because of a convenient implementation calledPrefixResolverDefault. Instances suck their namespace mapping out of a living DOM node, as in
JAVA:
  1. PrefixResolver resolver = new PrefixResolverDefault(doc.getDocumentElement());
Making that prefix resolver compatible with JAXP is not that hard:
JAVA:
  1. NamespaceContext ctx = new NamespaceContext() {
  2.    public String getNamespaceURI(String prefix) {
  3.       return resolver.getNamespaceForPrefix(prefix);
  4.    }
The other two methods have their old dummy implementation. Of course you need to make the prefix resolver final before using it from the instance of that anonymous class, but you already know that...
The complete code with this Xalan tool follows.
JAVA:
  1. package com.davber.test;
  2. import java.util.Iterator;
  3. import javax.xml.namespace.NamespaceContext;
  4. import javax.xml.parsers.*;
  5. import javax.xml.xpath.*;
  6. import org.w3c.dom.Document;
  7. import com.sun.org.apache.xml.internal.utils.PrefixResolver;
  8. import com.sun.org.apache.xml.internal.utils.PrefixResolverDefault;
  9.  
  10. public class App
  11. {
  12.     public static void main( String[] args )
  13.     {
  14.         try {
  15.             // First, the XML document
  16.            
  17.             String xmlStr =
  18.                 "<?xml version=\"1.0\" ?>\n" +
  19.                 "<Sales xmlns=\"http://www.davber.com/sales-format\">\n" +
  20.                 "<Customer name=\"CostCo, Inc.\">\n" +
  21.                 "<ord:Order xmlns:ord=\"http://www.davber.com/order-format\" " +
  22.                 "price=\"12000\">\n" +
  23.                 "<ord:Description>A bunch of stuff" +
  24.                 "</ord:Description>\n" +
  25.                 "</ord:Order>\n" +
  26.                 "</Customer>\n" +
  27.                 "</Sales>\n";
  28.             DocumentBuilderFactory xmlFact =
  29.                 DocumentBuilderFactory.newInstance();
  30.             xmlFact.setNamespaceAware(true);
  31.             DocumentBuilder builder = xmlFact.
  32.                 newDocumentBuilder();
  33.             Document doc = builder.parse(
  34.                     new java.io.ByteArrayInputStream(
  35.                             xmlStr.getBytes()));
  36.            
  37.             // We map the prefixes to URIs, using Xalan's
  38.             // document-extracting mapping tool and wrapping
  39.             // it in a nice JAXP shell
  40.            
  41.             final PrefixResolver resolver =
  42.                 new PrefixResolverDefault(doc.getDocumentElement());
  43.             NamespaceContext ctx = new NamespaceContext() {
  44.                 public String getNamespaceURI(String prefix) {
  45.                     return resolver.getNamespaceForPrefix(prefix);
  46.                 }
  47.                
  48.                 // Dummy implementation - not used!
  49.                 public Iterator getPrefixes(String val) {
  50.                     return null;
  51.                 }
  52.                
  53.                 // Dummy implemenation - not used!
  54.                 public String getPrefix(String uri) {
  55.                     return null;
  56.                 }
  57.             };
  58.            
  59.             // Now the XPath expression
  60.            
  61.             String xpathStr =
  62.                 "//ord:Order/ord:Description";
  63.             XPathFactory xpathFact =
  64.                 XPathFactory.newInstance();
  65.             XPath xpath = xpathFact.newXPath();
  66.             xpath.setNamespaceContext(ctx);
  67.             String result = xpath.evaluate(xpathStr, doc);
  68.             System.out.println("XPath result is \"" +
  69.                     result + "\"");
  70.         }
  71.         catch (Exception ex) {
  72.             ex.printStackTrace();
  73.         }
  74.     }
  75. }
Try it out! It produces the output you expected, right? Well, only if you expected Java to throw up a stack trace ;-)
It will say something like

javax.xml.transform.TransformerException: Prefix must resolve to a namespace: ord
But we did map ord to a namespace URI in the XML document, so why does that Xalan tool not see it? Is there a bug? Well, you might argue a design flaw, since it only uses the mapping found at the node passed to it, i.e., to top level namespace declarations in our case. So, let us move the namespace declaration to the top level, to get the following XML input
XML:
  1. <?xml version="1.0" ?>
  2. <Sales xmlns="http://www.davber.com/sales-format"
  3.    xmlns:ord="http://www.davber.com/order-format">
  4.   <Customer name="CostCo, Inc.">
  5.     <ord:Order price="12000">
  6.        <ord :D escription>A bunch of stuff</ord :D escription>
  7.     </ord:Order>
  8.   </Customer>
  9. </Sales>
Now you will see the output

XPath result is "A bunch of stuff"
So what about the default namespace? Do we need a prefix there, and in that case which one?
We try with
XPATH:
/Sales/Customer/@name
Hmm, no match... So, it is not that simple. Again, we have to use a namespace in the XPath since the elements we try to match against are in a namespace (though it happens to be the default namespace) and the only way to use a namespace is via a prefix. What is that illusive prefix we need to map, via the Xalan prefix resolver, to the default namespace? The answer is: the empty string, i.e., the following XPath will match:
XPATH:
/:Sales/:Customer/@name
Yes, the output is

XPath result is "CostCo, Inc."
So, the "Xalan" path allows us to quite easily deal with both explicitly prefixed elements and defaulted elements in the XML input. Note: this route is pretty fragile, since we here rely on a quite arbitrary choice of prefixes for those namespace URIs. I.e., if the XML author changes the prefix we are doomed. So, my recommendation is to use the URI via explicit mapping if you want/need to deal with namespace at all ;-)
How can we not care about the namespaces in the XML input? The only way is to disable namespace awareness, via the method invocation
JAVA:
  1. xmlFact.setNamespaceAware(false);
Whether the factory is namespace aware or not by default is implementation-specific. Well, it should not be, since the SAX 2 Specification states that itshould be namespace aware by default. It just happens that the Xerces parser of the J2SE reference implementation is not aware by default, so we could just leave the method invocation out of the picture. Let us set it explicitly to be on the safe side, and to support other implementations of JAXP... Thus, we get the following code:
JAVA:
  1. package com.davber.test;
  2. import java.util.Iterator;
  3. import javax.xml.namespace.NamespaceContext;
  4. import javax.xml.parsers.*;
  5. import javax.xml.xpath.*;
  6. import org.w3c.dom.Document;
  7. import com.sun.org.apache.xml.internal.utils.PrefixResolver;
  8. import com.sun.org.apache.xml.internal.utils.PrefixResolverDefault;
  9.  
  10. public class App
  11. {
  12.     public static void main( String[] args )
  13.     {
  14.         try {
  15.             // First, the XML document
  16.            
  17.             String xmlStr =
  18.                 "<?xml version=\"1.0\" ?>\n" +
  19.                 "<Sales xmlns=\"http://www.davber.com/sales-format\" " +
  20.                 "xmlns:ord=\"http://www.davber.com/order-format\">\n" +
  21.                 "<Customer name=\"CostCo, Inc.\">\n" +
  22.                 "<ord:Order " +
  23.                 "price=\"12000\">\n" +
  24.                 "<ord:Description>A bunch of stuff" +
  25.                 "</ord:Description>\n" +
  26.                 "</ord:Order>\n" +
  27.                 "</Customer>\n" +
  28.                 "</Sales>\n";
  29.             DocumentBuilderFactory xmlFact =
  30.                 DocumentBuilderFactory.newInstance();
  31.             xmlFact.setNamespaceAware(false);
  32.             DocumentBuilder builder = xmlFact.
  33.                 newDocumentBuilder();
  34.             Document doc = builder.parse(
  35.                     new java.io.ByteArrayInputStream(
  36.                             xmlStr.getBytes()));
  37.            
  38.             // We map the prefixes to URIs, using Xalan's
  39.             // document-extracting mapping tool and wrapping
  40.             // it in a nice JAXP shell
  41.            
  42.             final PrefixResolver resolver =
  43.                 new PrefixResolverDefault(doc.getDocumentElement());
  44.             NamespaceContext ctx = new NamespaceContext() {
  45.                 public String getNamespaceURI(String prefix) {
  46.                     return resolver.getNamespaceForPrefix(prefix);
  47.                 }
  48.                
  49.                 // Dummy implementation - not used!
  50.                 public Iterator getPrefixes(String val) {
  51.                     return null;
  52.                 }
  53.                
  54.                 // Dummy implemenation - not used!
  55.                 public String getPrefix(String uri) {
  56.                     return null;
  57.                 }
  58.             };
  59.            
  60.             // Now the XPath expression
  61.            
  62.             String xpathStr =
  63.                 "/:Sales/:Customer/@name";
  64.             XPathFactory xpathFact =
  65.                 XPathFactory.newInstance();
  66.             XPath xpath = xpathFact.newXPath();
  67.             xpath.setNamespaceContext(ctx);
  68.             String result = xpath.evaluate(xpathStr, doc);
  69.             System.out.println("XPath result is \"" +
  70.                     result + "\"");
  71.         }
  72.         catch (Exception ex) {
  73.             ex.printStackTrace();
  74.         }
  75.     }
  76. }
It is interesting to note that with namespace awareness disabled,namespaced XPath names do not match, i.e., they live in an alternative universe. The XPath evaluation is still namespace sensitive, but the XML document now resides in the non-namespaced corner of that world. The only way to get back to the namespace-agnostic universe is to leave the prefixes out altogether, such as in
XPATH:
/Sales/Customer/@name
Alas, we are back to square one!
There are two important issues we have to consider:
  1. try to apply a namespaced XPath to a document not declaringthose namespaces will throw an exception
  2. other implementations of JAXP (earlier versions or not) might act a bit differently
The second issue is ignored for now, but please make sure you have the classpath setup correctly, so that the right implementation is at play; this classpath issue came to bite me a few days ago, effectively wasting a full day of work! The first issue is best explained by removing the prefixord along with URI in the XML input, while keeping it in the XPath. I.e., we will have the following XML input and XPath expressions.
XML:
  1. <?xml version="1.0" ?>
  2. <Sales xmlns="http://www.davber.com/sales-format">
  3.   <Customer name="CostCo, Inc.">
  4.     <Order price="12000">
  5.        <Description>A bunch of stuff</Description>
  6.     </Order>
  7.   </Customer>
  8. </Sales>
XPATH:
//ord:Order/ord:Description
We here assume the same namespace mapping as before, either via an extraction - using the Xalan tool above - against the original document or with the explicit NamespaceContext we used initially. Using the latter method, we get the following code:
JAVA:
  1. package com.davber.test;
  2. import java.util.Iterator;
  3. import javax.xml.namespace.NamespaceContext;
  4. import javax.xml.parsers.*;
  5. import javax.xml.xpath.*;
  6. import org.w3c.dom.Document;
  7. public class App
  8. {
  9.     public static void main( String[] args )
  10.     {
  11.         try {
  12.             // First, the XML document
  13.            
  14.             String xmlStr =
  15.                 "<?xml version=\"1.0\" ?>\n" +
  16.                 "<Sales xmlns=\"http://www.davber.com/sales-format\">\n " +
  17.                 "<Customer name=\"CostCo, Inc.\">\n" +
  18.                 "<ord:Order " +
  19.                 "price=\"12000\">\n" +
  20.                 "<ord:Description>A bunch of stuff" +
  21.                 "</ord:Description>\n" +
  22.                 "</ord:Order>\n" +
  23.                 "</Customer>\n" +
  24.                 "</Sales>\n";
  25.             DocumentBuilderFactory xmlFact =
  26.                 DocumentBuilderFactory.newInstance();
  27.             xmlFact.setNamespaceAware(true);
  28.             DocumentBuilder builder = xmlFact.
  29.                 newDocumentBuilder();
  30.             Document doc = builder.parse(
  31.                     new java.io.ByteArrayInputStream(
  32.                             xmlStr.getBytes()));
  33.            
  34.             NamespaceContext ctx = new NamespaceContext() {
  35.                 public String getNamespaceURI(String prefix) {
  36.                     String uri;
  37.                     if (prefix.equals("ord"))
  38.                         uri = "http://www.davber.com/order-format";
  39.                     else if (prefix.equals("sal"))
  40.                         uri = "http://www.davber.com/sales-format";
  41.                     else
  42.                         uri = null;
  43.                     return uri;
  44.                 }
  45.                
  46.                 // Dummy implementation - not used!
  47.                 public Iterator getPrefixes(String val) {
  48.                     return null;
  49.                 }
  50.                
  51.                 // Dummy implemenation - not used!
  52.                 public String getPrefix(String uri) {
  53.                     return null;
  54.                 }
  55.             };
  56.            
  57.             // Now the XPath expression
  58.            
  59.             String xpathStr =
  60.                 "//ord:Order/ord:Description";
  61.             XPathFactory xpathFact =
  62.                 XPathFactory.newInstance();
  63.             XPath xpath = xpathFact.newXPath();
  64.             xpath.setNamespaceContext(ctx);
  65.             String result = xpath.evaluate(xpathStr, doc);
  66.             System.out.println("XPath result is \"" +
  67.                     result + "\"");
  68.         }
  69.         catch (Exception ex) {
  70.             ex.printStackTrace();
  71.         }
  72.     }
  73. }
Try it out. It will generate a stack trace:

[Fatal Error] :4:26: The prefix "ord" for element "ord:Order" is not bound.
org.xml.sax.SAXParseException: The prefix "ord" for element "ord:Order" is not bound.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at com.davber.test.App.main(App.java:34)
What was that other issue again? Ah, something about other implementations behaving differently than the reference implementation of J2SE! Let us try this conjecture by putting the J2EE 1.4 reference implementation in the classpath. We will use WSDP 1.6.
It is time to sum up our experiences with namespaces and XPath in Java.
Lessons Learned
For those of you who skipped the lengthy post and jumped right here:Welcome back!.
The lessons learned are - whereof most are applicable to XPath with namespaced XML input in any language:
  1. whenever an XML element belongs in a namespace, the XPath pattern must use the same namespace for names to match
  2. just as with XML documents, namespaces are given via prefixesin XPath
  3. the XPath evaluator does not care about prefixes at all, but only with the namespace URI
  4. the above lesson implies we need to map prefixes to URIs, which is done via the JAXP interface NamespaceContext
  5. even the default namespace is a namespace, and thus matching names have to be prefixed in XPath
  6. there is a Xalan-specific tool to extract prefix to URI mappings from a DOM node: PrefixResolverDefault
  7. using that Xalan resolver, you have to embed it in a proper JAXPNamespaceContext
  8. that Xalan resolver will map the empty prefix to the default namespace URI, so you have to use the syntax :elem in XPath order to match elem in the default namespace.
  9. using a namespace URI in the XPath that is not declared in the parsed XML document will throw an exception
  10. you can disable namespace awareness with the methodsetNamespaceAware of the document builder factory in JAXP.
  11. some implementations, such as Xerces, have namespace awareness disabled by default
  12.      

julienw said,

March 6, 2007 @ 4:55 pm
I’ve a question :
If the parser is “Namespace Unaware” by default , why the XPath expression failed on the namespaced document at first ?
I got stuck by the same problem today; I nearly found the solution, with NamespaceContext, but I didn’t try using empty prefixes for my expression…
Thanks anyway, I hope it’ll work tomorrow ;)

xPaths problem - Java Forums said,

April 23, 2008 @ 10:49 pm
[...] davber does IT � XPath with namespaces in Java __________________ dont worry newbie, we got you covered. [...]

Leave a Comment

You must be logged in to post a comment.

Nessun commento: