XPath concat()不返回多个项目

时间:2016-03-28 14:41:52

标签: xml xpath

我有以下XML文件。 我正在编写一个XPath查询,以便为fname的所有订单返回weightweight>50的串联。

<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>

我把查询写成:

concat(
 /purple/customer[@cid=/purple/order[weight>50]/custid]/fname/text(),
 /purple/order[weight>50]/weight/text())

输出结果为:

John55

所需的输出是:

John55
Anthony75

关于如何实现这一目标的任何建议?

1 个答案:

答案 0 :(得分:1)

XPath 1.0

XPath 1.0无法像您请求的那样返回多个字符串连接。您的输出更接近重排而不是纯选择,因此您应该考虑XSLT。

XPath 2.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

按要求