Re: [xsl] Finding nester distinct items

Subject: Re: [xsl] Finding nester distinct items
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 10 Mar 2003 14:55:17 -0500
At 2003-03-10 13:26 -0600, Craig Kattner wrote:
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).

When you say "distinct list of Time val's", your test data doesn't reflect duplicate values for time the way the web page does. I modified the data to include a duplicate for 6-hour single-pole single-throw switches.


I find that using variables for multiple levels of uniqueness is better than using the Muenchian Method, but tastes vary.

When using variables, the idea is to create a variable with all items from which uniqueness is determined, and then create a variable of those for each of those that are unique and find uniqueness within those variables. This avoids concatenating key values for each level of uniqueness in the Muenchian Method.

The first of each variable for each value is determined using generate-id() since generate-id() only works on the first member of a passed node set.

An example is below ... I hope this helps.

................. Ken


T:\ftemp>type craig.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="FD6HH"> <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>


T:\ftemp>type craig.xsl <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:variable name="products" select="/products/product"/>
  <xsl:for-each select="$products">
    <xsl:if test="generate-id(.) =
                  generate-id( $products[ product_fact[@name='Pole']/val=
                                current()/product_fact[@name='Pole']/val ])">
      <xsl:value-of select="product_fact[@name='Pole']/val"/>
      <xsl:text>
</xsl:text>
      <xsl:variable name="times"
                    select="$products[ product_fact[@name='Pole']/val=
                             current()/product_fact[@name='Pole']/val ]"/>
      <xsl:for-each select="$times">
        <xsl:if test="generate-id(.) =
                      generate-id( $times[ product_fact[@name='Time']/val=
                                 current()/product_fact[@name='Time']/val ])">
          <xsl:text>      </xsl:text>
          <xsl:value-of select="product_fact[@name='Time']/val"/>
          <xsl:text>
</xsl:text>
          <xsl:for-each select="$times[ product_fact[@name='Time']/val=
                              current()/product_fact[@name='Time']/val ]">

            <xsl:text>             </xsl:text>
            <xsl:value-of select="@model"/>
            <xsl:text>
</xsl:text>
          </xsl:for-each>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>saxon craig.xml craig.xsl
SPST
      12 hours
             FD12H
      6 hours
             FD6H
             FD6HH
SPDT
      4 hours
             FD4H

T:\ftemp>rem Done!


-- Upcoming hands-on in-depth XSLT/XPath and/or XSL-FO North America: June 16-20, 2003

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                      Definitive XSLT and XPath
ISBN 0-13-140374-5                              Definitive XSL-FO
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-10-1              Practical Formatting Using XSL-FO
Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc


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



Current Thread