xsl-list
[Top] [All Lists]

Re: How efficient is DVC? - A grouping example - FIXED

2003-03-22 18:43:40
OK, here come the *complete * working examples.

_____________
Example 1:

Input:

<cities>
 <city name="Barcelona" country="Espana"/>
 <city name="Paris" country="France"/>
 <city name="Roma" country="Italia"/>
 <city name="Madrid" country="Espana"/>
 <city name="Milano" country="Italia"/>
 <city name="Firenze" country="Italia"/>
 <city name="Napoli" country="Italia"/>
 <city name="Lyon" country="France"/>
</cities>

Stylesheet:

 <xsl:template match="cities">
  <xsl:variable name="sorted">
   <xsl:for-each select="./city">
    <xsl:sort select="@country"/>
    <xsl:copy-of select="."/>
   </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="sorted-tree-fragment" select="xalan:nodeset($sorted)/*"/>

 <!-- Gets the groups -->
  <xsl:variable name="groups">
   <xsl:apply-templates select="$sorted-tree-fragment"/>
  </xsl:variable>

 <!-- Iterate through all the groups -->
  <xsl:for-each select="xalan:nodeset($groups)/*">
   <xsl:variable name="country" select="@id"/>
   <xsl:copy>
    <xsl:copy-of select="@*"/>
    <!-- Copy the nodes with the same country -->
    <xsl:copy-of select="$sorted-tree-fragment[(_at_)country = $country]"/>
   </xsl:copy>
  </xsl:for-each>
 </xsl:template>

 <xsl:template match="city">
  <xsl:variable name="preceding" select="./preceding-sibling::*[1]"/>
  <xsl:if test="not(./@country = $preceding/@country)">
   <group id="{./@country}"/>
  </xsl:if>
 </xsl:template>

_______________________
Second example:

Input: same input

Stylesheet:

  <xsl:template match="cities">
    <xsl:variable name="sorted">
      <xsl:for-each select="./city">
        <xsl:sort select="@country"/>
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </xsl:variable>

    <xsl:variable name="sorted-tree-fragment"
select="xalan:nodeset($sorted)/*"/>

    <xsl:variable name="groups">
      <xsl:apply-templates select="xalan:nodeset($sorted)/*[1]"/>
    </xsl:variable>

    <xsl:apply-templates select="xalan:nodeset($groups)"/>

  </xsl:template>

  <xsl:template match="city">
    <xsl:variable name="preceding" select="./preceding-sibling::*[1]"/>
    <xsl:choose>
      <xsl:when test="not(./@country = $preceding/@country)">
        <group id="{./@country}">
          <xsl:copy-of select="."/>
          <xsl:apply-templates select="./following-sibling::*[1]"/>
        </group>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="."/>
        <xsl:apply-templates select="./following-sibling::*[1]"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template match="group">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:copy-of select="./city"/>
    </xsl:copy>
    <xsl:apply-templates select="./group"/>
  </xsl:template>


Robbert.

----- Original Message -----
From: "Mike Brown" <mike(_at_)skew(_dot_)org>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Sunday, March 23, 2003 12:59 AM
Subject: Re: [xsl] How efficient is DVC? - A grouping example - FIXED


Robbert van Dalen wrote:
This is an update on the previous post. I didn't check if the first two
examples
worked correctly (I wrote them out of the top of my head). It should now be
fixed. Sorry for the inconvenience!

Even with the corrections, the first example is not producing the
expected output for me. With both Saxon and Xalan, I get

<result>
   <group id=""/>
   <group id="Espana">
      <city name="Barcelona" country="Espana"/>
      <city name="Madrid" country="Espana"/>
   </group>
   <group id="France">
      <city name="Paris" country="France"/>
      <city name="Lyon" country="France"/>
   </group>
</result>

Can you repost complete, working examples?

Mike

--
  Mike J. Brown   |  http://skew.org/~mike/resume/
  Denver, CO, USA |  http://skew.org/xml/

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




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