Muenchian Grouping XSLT

Tag: xslt-1.0 , muenchian-grouping Author: bantionba Date: 2014-01-01

This is my XML:

<LIGHT_DESCRIPTION_LIST>
    <LIGHT_DESCRIPTION>
        <LIGHT_CHARACTERISTICS>Fl</LIGHT_CHARACTERISTICS>
        <LIGHT_CATEGORY>17</LIGHT_CATEGORY>
    </LIGHT_DESCRIPTION>
    <LIGHT_DESCRIPTION>
        <LIGHT_CHARACTERISTICS>Fl</LIGHT_CHARACTERISTICS>
        <LIGHT_CATEGORY>UNKNOWN</LIGHT_CATEGORY>
    </LIGHT_DESCRIPTION>
    <LIGHT_DESCRIPTION>
        <LIGHT_CHARACTERISTICS>Fl</LIGHT_CHARACTERISTICS>
        <LIGHT_CATEGORY>UNKNOWN</LIGHT_CATEGORY>
    </LIGHT_DESCRIPTION>
    <LIGHT_DESCRIPTION>
        <LIGHT_CHARACTERISTICS>Fl</LIGHT_CHARACTERISTICS>
        <LIGHT_CATEGORY>17</LIGHT_CATEGORY>
    </LIGHT_DESCRIPTION>
    <LIGHT_DESCRIPTION>
        <LIGHT_CHARACTERISTICS>Fl</LIGHT_CHARACTERISTICS>
        <LIGHT_CATEGORY>UNKNOWN</LIGHT_CATEGORY>
    </LIGHT_DESCRIPTION>      
    <LIGHT_DESCRIPTION>
        <LIGHT_CHARACTERISTICS>Fl</LIGHT_CHARACTERISTICS>
        <LIGHT_CATEGORY>17</LIGHT_CATEGORY>
    </LIGHT_DESCRIPTION>
    <LIGHT_DESCRIPTION>
        <LIGHT_CHARACTERISTICS>Fl</LIGHT_CHARACTERISTICS>
        <LIGHT_CATEGORY>17</LIGHT_CATEGORY>
    </LIGHT_DESCRIPTION>
</LIGHT_DESCRIPTION_LIST>

This is my XSLT:

<xsl:for-each select="LIGHT_DESCRIPTION_LIST">
    <xsl:for-each select="LIGHT_DESCRIPTION">
        <xsl:if test="LIGHT_CATEGORY = '17'">
            <xsl:text>IRA</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:for-each>

In output I had: IRA IRA IRA IRA
I would : IRA ( if exist a light category = 17 write only one time IRA without Repetition)

Best Answer

Anyway, please try the code below:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:key name="LD" match="LIGHT_DESCRIPTION" use="."/>
    <xsl:variable name="Light_Desc" select="//LIGHT_DESCRIPTION"/>
    <xsl:template match="LIGHT_DESCRIPTION_LIST">
        <xsl:for-each select="$Light_Desc[count(. | key('LD', .)[1]) = 1]">
            <xsl:if test="LIGHT_CATEGORY = '17'">
            <br/>
            <span style="font-family:Univers Condensed; font-size:9pt; ">
                <xsl:text>IRA</xsl:text>
            </span>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

You must use keys and eliminate duplicates before testing the node content.

comments:

dont work very well Joel
The stylesheet above outputs <br/><span style="font-family:Univers Condensed; font-size:9pt; ">IRA</span>. If you would like your output to be IRA only, then remove <br/> and <span> tags in the stylesheet.

Other Answer1

<xsl:key name="LD" match="//LIGHT_DESCRIPTION" use="."/>


<xsl:for-each select="LIGHT_DESCRIPTION_LIST">
    <xsl:for-each select="LIGHT_DESCRIPTION">
    <xsl:variable name="Light_Desc" select="."/>
        <xsl:for-each select="$Light_Desc[count(. | key('LD', .)[1]) = 1]">
            <xsl:if test="LIGHT_CATEGORY = '17'">
            <xsl:text>IRA</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:for-each>
</xsl:for-each>

In output this give:

IRA IRA

comments:

dont run very well :-)
You should change <xsl:for-each select="LIGHT_DESCRIPTION_LIST"> ... </xsl:for-each> with <xsl:template match="LIGHT_DESCRIPTION_LIST"> and </xsl:template>, respectively.