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”
2 个解决方案
#1
2
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
.
您将看到它精确选择(两个)模型元素,其id属性由作为list子项的item元素的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]
#2
2
Your xpath expression should already return exactly what you want. Quoting from http://www.w3.org/TR/xpath/#booleans, 5th paragraph:
你的xpath表达式应该已经完全返回你想要的。引自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
如果要比较的一个对象是节点集而另一个是字符串,那么当且仅当节点集中有节点时才进行比较,以便对字符串值执行比较的结果节点和另一个字符串是真的
#1
2
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
.
您将看到它精确选择(两个)模型元素,其id属性由作为list子项的item元素的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]
#2
2
Your xpath expression should already return exactly what you want. Quoting from http://www.w3.org/TR/xpath/#booleans, 5th paragraph:
你的xpath表达式应该已经完全返回你想要的。引自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
如果要比较的一个对象是节点集而另一个是字符串,那么当且仅当节点集中有节点时才进行比较,以便对字符串值执行比较的结果节点和另一个字符串是真的