使用xslt查找父节点下的所有唯一xml第一个节点

时间:2011-01-18 20:44:03

标签: xml xslt xpath

我是xsl n00b并尝试在<section>节点下找到所有唯一的xml节点

这是我的代码:

<section>
   <books>
     <book>
       <title>Child 44</title>
       <author>Thomas Rob Smith</author>
       <price>5.00</price>
     </book>
     <book>
       <title>Atlas Shrugged</title>
       <author>Ayn Rand</author>
       <price>7.00</price>
     </book>
   </books>
   <cds>
     <cd>
       <title>A Passage in time</title>
       <band>Authority Zero</band>
       <price>10.00</price>
      </cd>
   </cds>
</section>

这是我想要的输出:

books
cds

提前致谢!

2 个答案:

答案 0 :(得分:2)

此转化

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:strip-space elements="*"/>
 <xsl:output method="text"/>

 <xsl:key name="kElByName" match="/*/*" use="name()" />

 <xsl:template match=
  "/*/*[generate-id()=generate-id(key('kElByName',name())[1])]">
  <xsl:value-of select="concat(name(),'&#xA;')"/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<section>
    <books>
        <book>
            <title>Child 44</title>
            <author>Thomas Rob Smith</author>
            <price>5.00</price>
        </book>
        <book>
            <title>Atlas Shrugged</title>
            <author>Ayn Rand</author>
            <price>7.00</price>
        </book>
    </books>
    <cds>
        <cd>
            <title>A Passage in time</title>
            <band>Authority Zero</band>
            <price>10.00</price>
        </cd>
    </cds>
</section>

生成想要的正确结果

books
cds

请注意:使用 Munchian method for grouping 。这可能是XSLT 1.0最有效的分组方法。

答案 1 :(得分:0)

/section/*[not(name(.)=name(preceding-sibling::*)) and not(name(.)=name(following-sibling::*))]