0

以此为例:

<root>
  <gramps name="Bill">
    <him name="Bill Jr">
      <kid name="Bill III"/>
    </him>
  </gramps>
  <gramps name="Tom">
    <him name="Al">
      <kid name="Al Jr"/>
    </him>
  </gramps>
  <gramps name="Bob">
    <him name="Bob Jr">
      <kid name="Sam"/>
    </him>
  </gramps>
  <store name="Tom's"/>
  <store name="Bill's"/>
  <store name="Pete's"/>
</root>

我可以通过以下方式获得所有二级元素:

/*/*

我可以通过以下方式获取所有二级元素的名称 , gramps store

distinct-values(/*/*/name())

我怎样才能得到每个二级元素的第一个元素,结果是:

  <gramps name="Bill">
    <him name="Bill Jr">
      <kid name="Bill III"/>
    </him>
  <store name="Tom's"/>
4

1 回答 1

4

You can check at each name if there are previous with the same name:

/*/*[not(preceding-sibling::*/name() = name())]

Or, probably faster, find the first for each name:

for $name in distinct-values(/*/*/name()) return /*/*[name() = $name][1]
于 2013-10-10T12:32:51.337 回答