xsl-list
[Top] [All Lists]

Finding nester distinct items

2003-03-10 12:26:48
I have the following XML:

<products>
<product id="439" model="FD12H">
        <desc>text</desc>
        <product_fact id="5" name="Finish">
                <val>Ivory Decorative</val>
        </product_fact>
        <product_fact id="8" name="Hold">
                <val>no</val>
        </product_fact>
        <product_fact id="6" name="Pole">
                <val>SPST</val>
        </product_fact>
        <product_fact id="7" name="Time">
                <val>12 hours</val>
        </product_fact>
</product>
<product id="439" model="FD6H">
        <desc>text</desc>
        <product_fact id="5" name="Finish">
                <val>Ivory Decorative</val>
        </product_fact>
        <product_fact id="8" name="Hold">
                <val>no</val>
        </product_fact>
        <product_fact id="6" name="Pole">
                <val>SPST</val>
        </product_fact>
        <product_fact id="7" name="Time">
                <val>6 hours</val>
        </product_fact>
</product>
<product id="439" model="FD4H">
        <desc>text</desc>
        <product_fact id="5" name="Finish">
                <val>Ivory Decorative</val>
        </product_fact>
        <product_fact id="8" name="Hold">
                <val>no</val>
        </product_fact>
        <product_fact id="6" name="Pole">
                <val>SPDT</val>
        </product_fact>
        <product_fact id="7" name="Time">
                <val>4 hours</val>
        </product_fact>
</product>
</products>

What I need is to get a distinct list of Pole val's and then for each Pole a 
distinct list of Time val's. (I need to be able to recreate this 
http://www.intermatic.com/comind/fdff.htm).

The best I've come up with is:

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

<xsl:template match="/">
        <xsl:apply-templates select="//product" />
</xsl:template>

<xsl:template match="product">
        <xsl:apply-templates select="product_fact[(_at_)name='Pole']" 
mode="pole_values">
                <xsl:sort select="@name" />
                <xsl:sort select="val" />
        </xsl:apply-templates>  
</xsl:template>

<!-- get the unique pole values -->
<xsl:template match="product_fact" mode="pole_values">
        <xsl:for-each 
select="../product_fact[not(val=following::product_fact/val) and @name='Pole']">
                <xsl:value-of select="val" /><br />
                <xsl:apply-templates select="." mode="time_values" />
        </xsl:for-each>
</xsl:template>

<!-- get the unique time values per pole -->
<xsl:template match="product_fact" mode="time_values">
        <xsl:for-each 
select="../ancestor::*/product/product_fact[not(val=following::product_fact/val)
 and @name='Time']">
                <xsl:value-of select="." /><br />
        </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

What I want is:
SPST
        12 Hours
        6 Hours
SPDT
        4 Hours

What I get is:
SPST
        12 Hours
        6 Hours
        4 Hours
SPDT
        12 Hours
        6 Hours
        4 Hours

Using MSXML 4 SP 1 for this if it makes a difference.

Thanks,
Craig


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>