简体   繁体   中英

Xquery not working with namespaces

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

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM