Douglas Russell wrote:
I've got some XML with namespaces and I just can't find a way to set the
namespace in my query.
<snip>
The XPATH '/' gets all the XML in Definition, that works. If I try and search
for '//Name' however, I get nothing. Presumably this is because Name is
actually in a namespace. How do I construct my XPATH to look for this?
I think that in order to get this to work you have to query for "Name" as the
*local* name, i.e. search for elements named "Name" in any namespace. One way
to do this is with an XPath expression like:
//child::*[local-name()="Name"]
which should (I think?) return all Name elements. If you want to restrict the
Name elements to a specific namespace, then you could do something like:
//child::*[local-name()="Name" and
namespace::*[string()="http://www.myproject.ox.ac.uk/MyProject"]]
I realize this is pretty (very) ugly, but I can't think of any better way at the
moment. It is of course very possible that there *is* a better way and I just
don't know about it...
I've tried:
'//myns:Name' and that would seem to be correct, but I can find no way of
setting myns to resolve to "http://www.myproject.ox.ac.uk/MyProject" in
Derby. When I do that I just get the error:
Caused by: org.apache.derby.client.am.SqlException: Encountered error while
evaluating XML query expression for XMLQUERY operator: Prefix must resolve to
a namespace: myns SQLSTATE: XJ001: Java exception: 'Prefix must resolve to a
namespace: myns: org.apache.xpath.domapi.XPathStylesheetDOM3Exception'.
This error comes from the underlying Xalan engine, so perhaps you can find a way
around this by searching Xalan documentation and/or help files? If it is
possible to set namespaces within an XPath query in Xalan, then hopefully it is
documented somewhere. And if it is, then feeding that into Derby as part of the
query should (hopefully) return the desired results, since Derby ultimately just
passes the query on to Xalan for evaluation...
Army