[border @country] 和 [border /@country] 的区别

Difference between [border @ country] and [border /@ country]

我有一个xml文件,其中一行是这样的

<mondial>
      <country>
         <encompassed continent="europe" percentage="100"/>
      </country>
</mondial>

假设我想 select 包含大陆的国家是欧洲 所以我写道:

<result> {
 doc("mondial.xml")//country[encompassed @continent="europe"]//
city
 } </result>

但答案是

<result> {
 doc("mondial.xml")//country[encompassed/ @continent="europe"]//
city
 } </result>

我想如果我们在这里使用“/”我们会转到文件中的下一个目录 有人可以帮我吗?解释将不胜感激

您的第一个示例 [encompassed @continent="europe"] 是无效的 XPath。

你的第二个例子是有效的,尽管我建议删除 [encompassed/ @continent="europe"] 中的 space,因为这很不寻常。

在您的第二个示例中,在谓词之后您试图找到 self-or-descendant city 元素,但是该元素不存在于 XML提供。如果您的 XML 是正确的,那么只需将您的第二个查询更改为:

<result>{
    doc("mondial.xml")//country[encompassed/@continent = "europe"]
}</result>

您还可以考虑在此处使用 eq 而不是 =,因为您正在进行原子比较。但是,我会让您阅读相关内容。如果您刚刚开始使用 XQuery,Priscialla Walmsley 的 XQuery 书是一个很好的资源。