我有以下xml:
<?xml version="1.0" encoding="UTF-8"?>
<football xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="football originalfootball.xsd">
<teams>
<team>
<name>Jets</name>
<city>New York</city>
<stadium>Giants Stadium</stadium>
</team>
<team>
<name>Giants</name>
<city>New York</city>
<stadium>Giants Stadium</stadium>
</team>
<team>
<name>Bills</name>
<city>Buffalo</city>
<stadium>Wilson Stadium</stadium>
</team>
</teams>
<scores>
<score>
<home>
<competitor>Jets</competitor>
<points>20</points>
</home>
<visitor>
<competitor>Giants</competitor>
<points>7</points>
</visitor>
</score>
<score>
<home>
<competitor>Bills</competitor>
<points>10</points>
</home>
<visitor>
<competitor>Jets</competitor>
<points>17</points>
</visitor>
</score>
</scores>
</football>
我希望获得每个团队的积分总和。
我尝试使用sum(//competitor[.=//name]/../points)
,但它没有用。
我想要那些击败巨人的球队。
有关此查询的任何想法?
答案 0 :(得分:1)
要总结特定团队的积分,您可以:
sum(//score/*[competitor='Jets']/points)
在XPath 2.0中,您可以列出所有团队及其分数,如:
for $team in //teams/team/name
return concat($team, ': ', sum(//score/*[competitor=$team]/points))
找到击败巨人队的球队
XPath 1.0
//score/*[points > ../*[competitor='Giants']/points]/competitor
XPath 2.0
//score/*[number(points) > ../*[competitor='Giants']/points/number()]/competitor
您的查询存在以下问题:
//competitor[.=//name]
对于给定的文档,这实际上与//competitor
相同,因为每个<competitor>
元素都有一个具有相同值的<name>
元素,因此谓词始终求值为true。这意味着它总结了所有竞争对手的所有积分。