xsl-list
[Top] [All Lists]

How efficient is DVC? - A grouping example - FIXED

2003-03-22 14:47:02
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!

Cheers,

RvD


__________________________________________
The first example:

<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>

____________________________
The second example:

<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>

<xsl:template match="city">
  <xsl:variable name="preceding" select="./preceding-sibling::*[1]"/>
  <xsl:choose>
    <xsl:when test="not(./@country = $preceding/@country)">
      <group id2="{./@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>



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