Xpath表达式有条件地获取元素计数

时间:2019-03-21 15:59:23

标签: xml xpath

我有以下XML示例

<TravelerIDs AssociationID="1">
        <CustomerID>1</CustomerID>
        <CustomParams>
            <Param Name="AGE">
                <Value xsi:type="xsd:int">4</Value>
            </Param>
            <Param Name="IsKnownTraveler">
                <Value xsi:type="xsd:boolean">false</Value>
            </Param>
        </CustomParams>
    </TravelerIDs>
    <TravelerIDs AssociationID="2">
        <CustomerID>2</CustomerID>
        <CustomParams>
            <Param Name="AGE">
                <Value xsi:type="xsd:int">8</Value>
            </Param>
        </CustomParams>
    </TravelerIDs>
    <TravelerIDs AssociationID="3">
        <CustomerID>3</CustomerID>
        <CustomParams>
            <Param Name="AGE">
                <Value xsi:type="xsd:int">8</Value>
            </Param>
        </CustomParams>
    </TravelerIDs>

我正在尝试获取具有Param元素且名称为AGE并且值大于5且小于14的TravelerID的数量

2 个答案:

答案 0 :(得分:2)

这应该有效:

//TravelerIDs[CustomParams//Param[@Name='AGE' and Value >= 8 and Value <= 14]]

如果要计数:

count(//TravelerIDs[CustomParams//Param[@Name='AGE' and Value >= 8 and Value <= 14]])

答案 1 :(得分:0)

每当您知道文档架构不会更改时,最好不要使用后代轴,因为这样做可能会降低性能。改用

count(
   /TravelerIDs
      /CustomParams
         /Param[@Name="AGE"]
            /Value[. > 5][. < 14]
)