[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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

Subject: Re: How efficient is DVC? - A grouping example - FIXED
From: "Robbert van Dalen" <juicer@xxxxxxxxx>
Date: Sun, 23 Mar 2003 02:43:40 +0100
xsl copy of example
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[@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@xxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Sunday, March 23, 2003 12:59 AM
Subject: Re:  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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.