简体   繁体   English

XQuery无法使用名称空间

[英]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. 我是xQuery的初学者,并且试图在XML文件中列出根节点的所有子类。 However, the root node in the XML doc has namespaces defined within it, which means my xQuery doesn't work when referencing. 但是,XML文档的根节点内部定义了名称空间,这意味着xQuery在引用时不起作用。

for $x in doc("/db/books.xml")/bookstore/book return $x doesn't return anything with namespaces defined in the bookstore tag 对于doc(“ / db / books.xml”)/ bookstore / book中的$ x,返回$ x不会返回在bookstore标记中定义了名称空间的任何内容

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? 在不删除XML文件中的命名空间的情况下,有什么方法可以解决此问题?

Edit: I'll eventually be executing these queries on hundreds of XML files where the namespaces vary considerably 编辑:我最终将在数百个XML文件中执行这些查询,这些XML文件中的命名空间相差很大

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: 如果您甚至比较懒惰(并且可以确保避免像<a:foo /> vs <b:foo />这样的名称冲突),您甚至可能想要使用:

for $x in doc("/db/books.xml")/*:bookstore/*:book return $x where the * will match any given namespace (even the "no-namespace") for $x in doc("/db/books.xml")/*:bookstore/*:book return $x ,其中*将匹配任何给定的名称空间(甚至是“ no-namespace”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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