Xml XPath concat()不返回多个项
我有以下XML文件。 我正在编写一个XPath查询,以返回所有Xml XPath concat()不返回多个项,xml,xpath,Xml,Xpath,我有以下XML文件。 我正在编写一个XPath查询,以返回所有weight>50的订单的fname和weight的串联 <purple> <customer cid="1"> <fname>John</fname> <lname>Klingard</lname> <apt>27</apt> <street>30
weight>50
的订单的fname
和weight
的串联
<purple>
<customer cid="1">
<fname>John</fname>
<lname>Klingard</lname>
<apt>27</apt>
<street>30th Winstonhut St.</street>
<pobox>199183</pobox>
</customer>
<customer cid="2">
<fname>Anthony</fname>
<lname>Hurro</lname>
<apt>86</apt>
<street>Town St.</street>
<pobox>177162</pobox>
</customer>
<order oid="1">
<eta>2016-04-23</eta>
<weight>55</weight>
<custid>1</custid>
</order>
<order oid="2">
<eta>2016-05-03</eta>
<weight>75</weight>
<custid>2</custid>
</order>
</purple>
输出为:
John55
John55
Anthony75
所需输出为:
John55
John55
Anthony75
关于如何实现这一点,有什么建议吗?XPath 1.0
XPath 1.0不能像您请求的那样返回多个字符串连接。您的输出比纯选择更接近重排,因此您应该考虑XSLT。
XPath2.0
您可以在XPath 2.0中使用for
循环:
for $c in /purple/customer[@cid=/purple/order[weight>50]/custid]
return concat($c/fname, /purple/order[custid=$c/@cid]/weight)
将返回
John55
Anthony75
根据要求使用xpath 1.0,当使用字符串时,无法获得多个结果functions@splash58是的,我明白。我发现我需要使用“|”操作符。这很有趣!通过使用“|”运算符,我确实成功地接近了所需的输出。这样:purple/customer[@cid=/purple/order[weight>50]/custid]/fname/text()| purple/order[weight>50]/weight/text(),您的XPath将返回四个文本节点,
John,Anthony,55,75
,甚至不符合请求的顺序,因此我不确定是否符合“close”。你可能会列举出来,并重新安排一些预先确定的情况,但如果事先不知道所需的数字,就很难推广这种方法。我理解你的意思。“|”运算符的执行是否总是从左到右?union运算符(|
)按文档顺序返回其结果。因此,如果XML文档中的order
元素都位于customer
元素之前,那么相同的XPath将返回55,75,John,Anthony
。