简体   繁体   English

节点集上的XPath查询就像SQL一样工作

[英]XPath query on node-set working like a SQL where in

a sample of xml document: xml文档的示例:

  <xml>
    <list>
      <item refid="1" />
      <item refid="3" />
    </list>
    <catalogue>
      <model id="1"><details /></model>
      <model id="2"><details /></model>
      <model id="3"><details /></model>
    </catalogue>
  </xml>

I'd like to query something like //model[ @id = (//item/@refid) ] to obtain all "model" having a referenced id in "list" 我想查询类似// model [@id =(// item / @ refid)]的内容,以获取在“list”中具有引用id的所有“model”

Your xpath expression should already return exactly what you want. 您的xpath表达式应该已经完全返回您想要的内容。 Quoting from http://www.w3.org/TR/xpath/#booleans , 5th paragraph: 引自http://www.w3.org/TR/xpath/#booleans ,第5段:

If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the string-value of the node and the other string is true 如果要比较的一个对象是节点集而另一个是字符串,那么当且仅当节点集中有节点时才进行比较,以便对字符串值执行比较的结果节点和另一个字符串是真的

I'd like to query something like //model[ @id = (//item/@refid) ] to obtain all "model" having a referenced id in "list" 我想查询类似// model [@id =(// item / @ refid)]的内容,以获取在“list”中具有引用id的所有“model”

The main problem here is your lack of confidence and not actually running an XPath engine to evaluate the expressions you've come up with. 这里的主要问题是你缺乏信心而没有实际运行XPath引擎来评估你提出的表达式。

If you evaluate the XPath expression you proposed: 如果您评估XPath表达式,则建议:

//model[ @id = (//item/@refid) ]

You'll see that it selects exactly the (two) model elements, whose id attributes are referenced by the refid attributes of item elements that are children of list . 您将看到它精确选择(两个) model元素,其id属性由作为listitem元素的refid属性引用。

@Jörn-Horstmann in his answer already explained why you get these results. @Jörn-Horstmann在他的回答中已经解释了为什么你得到这些结果。

A minor remark is to generally avoid using the // abbreviation . 一个小问题是通常避免使用//缩写 It causes the whole document to be scanned and is very inefficient. 它会导致整个文档被扫描并且效率非常低。 In this case I would use the equivalent but probably faster to evaluate XPath expression: 在这种情况下,我会使用等效但可能更快来评估XPath表达式:

/*/catalogue/model[@id = /*/list/item/@refid]

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

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