Hi
I would like to have some clarifications on how to apply XPath query on a
XML document depending if it comes with or without a namespace definition.
When I run the following code executing the XPath query /a/b on a document
with no namespace:
String doc = "<a><b/></a>";
String query = "/a/b";
XmlObject xml = XmlObject.Factory.parse(doc);
XmlCursor cursor = xml.newCursor();
cursor.push();
cursor.selectPath(query);
it gives me the correct result (<c/>).
But if if the document comes with a namespace, for instance:
<a xmlns="mynamespace">
<b/>
</a>
the same code does not give me any result.
After investigating I found that a working code could be:
String doc = "<a xmlns="mynamespace"><b/></a>";
String query = "declare namespace xs='mynamespace';/xs:a/xs:b";
XmlObject xml = XmlObject.Factory.parse(doc);
XmlCursor cursor = xml.newCursor();
cursor.push();
cursor.selectPath(query);
Is there a better way to do this without having to set the prefix "xs:" in
the Xpath query for each level ? I would like to find a generic way to
handle XML documents with or without namespace.
Regards,
Pascal