0

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

Joel M. Lamsen
  • 7,143
  • 1
  • 12
  • 14
Francesco Irrera
  • 445
  • 4
  • 21

1 Answers1

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>
Joel M. Lamsen
  • 7,143
  • 1
  • 12
  • 14
  • match expressions do not have to start from the root. `match="//LIGHT_SECTOR"` is equivalent to `match="LIGHT_SECTOR"` (you did the right thing in your template, after all, match expressions in keys are no different). Also: code beyond the immediately obvious should be accompanied by an explanation. – Tomalak Jan 17 '14 at 08:34
  • @Tomalak Thanks, I'll try to have some explanations later. It is the slow internet connection that is prompting me to upload the answer as fast a I could. – Joel M. Lamsen Jan 17 '14 at 09:22