1
votes

I'm a beginner to xQuery, and I'm trying to list all subclasses of the root node in an XML file. However, the root node in the XML doc has namespaces defined within it, which means my xQuery doesn't work when referencing.

for $x in doc("/db/books.xml")/bookstore/book return $x doesn't return anything with namespaces defined in the bookstore tag

When I remove the namespaces from the tag, the query works perfectly.

Is there any way I can get around this without removing the namespaces in the XML file?

Edit: I'll eventually be executing these queries on hundreds of XML files where the namespaces vary considerably

Thank you in advance

2

2 Answers

0
votes

did you declare your namespace in your query? like :

declare namespace ns = "http://example.org";

then you use it in your query:

for $x in doc("/db/books.xml")/ns:bookstore/ns:book return $x
0
votes

If you are even lazier (and can be sure to avoid name clashes like <a:foo /> vs <b:foo />) you might even want to use:

for $x in doc("/db/books.xml")/*:bookstore/*:book return $x where the * will match any given namespace (even the "no-namespace")