如何使用xpath查找相同的元素

时间:2014-11-07 14:06:00

标签: xpath

使用下一个xml,如何获得两个导演在一部电影中具有相同LastName的导演列表?

<MoviesLib>
  <Movie Title="Batman" Year="2013">
    <Directors>
      <Director>
        <Name>Robert</Name>
        <LastName>Zemeckis</LastName>
      </Director>
    </Directors>
  </Movie>
  <Movie Title="Gru" Year="2012">
    <Directors>
      <Director>
        <Name>john</Name>
        <LastName>tailer</LastName>
      </Director>
      <Director>
        <Name>Emma</Name>
        <LastName>Smith</LastName>
      </Director>
      <Director>
        <Name>Lana</Name>
        <LastName>Smith</LastName>
      </Director>
    </Directors>
  </Movie>
</MoviesLib>

例如在这种情况下将是:Emma Smith,Lana Smith

感谢

1 个答案:

答案 0 :(得分:0)

以下XPath 2.0表达式应该有效:

for $d in //Director
    return $d[../Director[not(. is $d) and LastName = $d/LastName]]

我无法提出单个XPath 1.0表达式,因为它不支持for表达式(有关某些背景,请参阅问题How to get the context of outer predicate?)。