Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
961 views
in Technique[技术] by (71.8m points)

xml - Select Distinct Element into a node XSLT

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...