开发者

Get distinct node values in XML with XSL

How can I find all the distinct names in my XML by using XSL?

<NewDataSet>
  <SearchResult>
    <Name>HAREDIN </Name>
    <Surname>FEIMI</Surname>
    <FathersName>QAMIL</FathersName>
    <Birthdate>1949-06-13T00:00:00+02:00</Bi开发者_开发知识库rthdate>
    <CustomerSegment>Individe Standart </CustomerSegment>
  </SearchResult>
  <SearchResult>
    <Name>HARMENAK</Name>
    <Surname>BADEJAN</Surname>
    <FathersName>VARAHAN  </FathersName>
    <Birthdate>1943-10-02T00:00:00+02:00</Birthdate>
    <CustomerSegment>Individe Standart </CustomerSegment>
  </SearchResult>
   <SearchResult>
    <Name>HARMENAK</Name>
    <Surname>BADEJAN</Surname>
    <FathersName>VARAHAN  </FathersName>
    <Birthdate>1943-10-02T00:00:00+02:00</Birthdate>
    <CustomerSegment>Individe Standart </CustomerSegment>
  </SearchResult>
</NewDataSet>

I wont all the distinct name node, th output like thse:

<root>

<Name>HAREDIN </Name>

<Name>HARMENAK</Name>

</root>


This XSLT 1.0 transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kNamesByVal" match="Name" use="."/>

 <xsl:template match="/">
  <t>
    <xsl:copy-of select=
    "*/*/Name[generate-id()
             =
              generate-id(key('kNamesByVal', .)[1])
              ]
    "/>
  </t>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document, produces the wanted, correct result:

<t>
   <Name>HAREDIN </Name>
   <Name>HARMENAK</Name>
</t>

An XSLT 2.0 solution that doesn't use keys :

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>  

 <xsl:template match="/">
  <t>
   <xsl:for-each-group select="*/*/Name" group-by=".">
     <xsl:copy-of select="."/>
   </xsl:for-each-group>
  </t>
 </xsl:template>
</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜