问题
This is my XML code:
<SECTOR_LIST>
<LIGHT_SECTOR>
<SECTOR1>22</SECTOR1>
<SECTOR2>92</SECTOR2>
</LIGHT_SECTOR>
<LIGHT_SECTOR>
<SECTOR1>22</SECTOR1>
<SECTOR2>92</SECTOR2>
</LIGHT_SECTOR>
<LIGHT_SECTOR>
<SECTOR1>92</SECTOR1>
<SECTOR2>137</SECTOR2>
</LIGHT_SECTOR>
<LIGHT_SECTOR>
<SECTOR1>92</SECTOR1>
<SECTOR2>137</SECTOR2>
</LIGHT_SECTOR>
</SECTOR_LIST>
I create this XSLT 1.0:
<xsl:for-each select="SECTOR_LIST">
<xsl:for-each select="LIGHT_SECTOR">
<xsl:text>VIS </xsl:text>
<xsl:value-of select="SECTOR1"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="SECTOR2"/>
<br/>
</xsl:for-each>
</xsl:for-each>
In output I had this: VIS 22-92 VIS 22-92 VIS 92-137 VIS 92-137
I would only: VIS 22-92 VIS 92-137
回答1:
Please try this code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="sector" match="//LIGHT_SECTOR" use="."/>
<xsl:variable name="Sectors" select="//LIGHT_SECTOR"/>
<xsl:template match="SECTOR_LIST">
<xsl:for-each select="$Sectors[count(. | key('sector', .)[1]) = 1]">
<xsl:text>VIS </xsl:text>
<xsl:value-of select="SECTOR1"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="SECTOR2"/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
来源:https://stackoverflow.com/questions/21180816/select-distinct-element-into-a-node-xslt