如何使用XSL在XML中找到所有不同的名称?
<NewDataSet>
<SearchResult>
<Name>HAREDIN </Name>
<Surname>FEIMI</Surname>
<FathersName>QAMIL</FathersName>
<Birthdate>1949-06-13T00: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>
<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>
我不会使用所有不同的名称节点,输出如下:
<root>
<Name>HAREDIN </Name>
<Name>HARMENAK</Name>
</root>
答案 0 :(得分:12)
此XSLT 1.0转换:
<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>
应用于提供的XML文档时,会生成所需的正确结果:
<t>
<Name>HAREDIN </Name>
<Name>HARMENAK</Name>
</t>
不使用密钥的XSLT 2.0解决方案:
<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>