如何使用XSL在 XML中找到所有不同的名称? NewDataSet SearchResult NameHAREDIN /Name SurnameFEIMI/Surname FathersNameQAMIL/FathersName Birthdate1949-06-13T00:00:00+02:00/Birthdate CustomerSegmentIndivide Standart /CustomerSegme
<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>这个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>