Assume that you had an XML Document like the one illustrated below.
-
- <purchaseOrder xmlns="www.oracle.com/po">
- <id>100</id>
- <items>
- <item name="TV">
- <description>television</description>
- </item>
- <item name=VCR">
- <description>Video Cassette Recorder</description>
- </item>
- <item name="DVD">
- <description>Digital Video Disc</description>
- </item>
- </items>
- <comment>PO from Ramkumar Menon</comment>
- </purchaseOrder>
-
-
- DOMParser parser = new DOMParser();
- StringReader reader = new StringReader(poXMLString);
- parser.parse(reader);;
- XMLDocument doc = parser.getDocument();
- XMLElement docElement = (XMLElement)doc.getDocumentElement();
- NodeList list = doc.selectNodes("/purchaseOrder/comment",docElement);
- System.err.println("number of items in the list = " + list.getLength());
DOMParser parser = new DOMParser(); StringReader reader = new StringReader(poXMLString); parser.parse(reader);; XMLDocument doc = parser.getDocument(); XMLElement docElement = (XMLElement)doc.getDocumentElement(); NodeList list = doc.selectNodes("/purchaseOrder/comment",docElement); System.err.println("number of items in the list = " + list.getLength()); But, yes, but, if the namespace prefix on the node name is specified to be "empty", the nodes are assumed to come from null namespace, and not the default namespace.
That is exactly why you get zero nodes on the evaluation of the expression.
Whats the workaround?
- Ensure that the path elements have a namespace prefix, or
- You could use wildcard expressions . For instance,the above expression could be re-written as "/*[local-name()='purchaseOrder'][namespace-uri()='www.oracle.com/po'] /*[local-name()='comments'[namespace-uri()='www.oracle.com/po'] You could use either of these approaches, and you've got what you wished for!
Comments (2)
An alternative is to associate an explicit chosen prefix with the default namespace URI using an implementation of the javax.xml.namespace.NamespaceContext interface.
This technique is described in an excellent article by Elliote Rusty Harold at: http://www.ibm.com/developerworks/library/x-javaxpathapi.html
Posted by Phil Fearon | September 22, 2007 3:45 AM
Posted on September 22, 2007 03:45
Another option would be to implement the interface oracle.xml.parser.v2.NSResolver and have it return the default namespace URI if the prefix is empty.
Posted by Ramkumar Menon | September 24, 2007 1:29 PM
Posted on September 24, 2007 13:29