使用XPath 1.0仅选择不同的值

时间:2018-09-25 23:54:22

标签: xml xpath xpath-1.0

在使用XPath 1.0的replicate中的City处,我只返回 unique Name值吗? >

state = 'Washington'

到目前为止,我已经尝试了以下XPath查询:

<StateDataSet>
    <State>
        <Name>Washington</Name>
        <City>Seattle</City>
        <business>Starbucks</business>
    </State>
    <State>
        <Name>Washington</Name>
        <City>Seattle</City>
        <business>Amazon</business>
    </State>
    <State>
        <Name>Washington</Name>
        <City>Redmond</City>
        <business>Microsoft</business>
    </State>
    <State>
        <Name>Washington</Name>
        <City>Redmond</City>
        <business>Starbucks</business>
    </State>
    <State>
        <Name>Washington</Name>
        <City>Seattle</City>
        <business>Dicks</business>
    </State>
    <State>
        <Name>Alaska</Name>
        <City>Anchorage</City>
        <business>Starbucks</business>
    </State>
    <State>
        <Name>Oregon</Name>
        <City>Portland</City>
        <business>Starbucks</business>
    </State>
    <State>
        <Name>Alaska</Name>
        <City>Gnome</City>
        <business>Starbucks</business>
    </State>
</StateDataSet>

这返回了华盛顿的城市,但价值并不唯一。我使用xpathtester.com

测试了查询

我在做什么错?我的查询基于get-distinct-values-within-specific-attributes

1 个答案:

答案 0 :(得分:2)

此XPath,

/StateDataSet/State[Name ='Washington' and 
                    not(City = preceding-sibling::State[Name='Washington']/City)]
             /City

将使用CityState元素中选择唯一的Name = 'Washington'元素:

<City>Seattle</City>
<City>Redmond</City>

根据要求。