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

Re: Count previous-siblings w/same attribute value up

Subject: Re: Count previous-siblings w/same attribute value up to attribute value - 1 possible?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 12 Dec 2011 21:09:32 -0500
Re:  Count previous-siblings w/same attribute value up
At 2011-12-12 21:01 -0500, I wrote:
If you actually want to do something with the groups at each level, then it is best to use XSLT 2:

I neglected to include the numbering at each level in the XSLT 2 version of the solution ... the code below addresses this.


I hope this helps.

. . . . . . . . . Ken

t:\ftemp>type lou.xml
<flatOutline>
 <item numDisplay="I." level="1" />
 <item numDisplay="II." level="1" />
  <item numDisplay="A." level="2" />
  <item numDisplay="B." level="2" />
  <item numDisplay="C." level="2" />
 <item numDisplay="III." level="1"/>
  <item numDisplay="A." level="2"/>
   <item numDisplay="1." level="3" />
   <item numDisplay="2." level="3" />
   <item numDisplay="3." level="3" />
   <item numDisplay="4." level="3" />
  <item numDisplay="B." level="2"/>
   <item numDisplay="1." level="3" />
   <item numDisplay="2." level="3" />
   <item numDisplay="3." level="3" />
  <item numDisplay="C." level="2"/>
</flatOutline>

t:\ftemp>xslt2 lou.xml lou2.xsl
<?xml version="1.0" encoding="UTF-8"?>
<section level="1">
   <item numDisplay="I." level="1">1</item>
</section>
<section level="1">
   <item numDisplay="II." level="1">2</item>
   <section level="2">
      <item numDisplay="A." level="2">1</item>
      <item numDisplay="B." level="2">2</item>
      <item numDisplay="C." level="2">3</item>
   </section>
</section>
<section level="1">
   <item numDisplay="III." level="1">3</item>
   <section level="2">
      <item numDisplay="A." level="2">1</item>
      <section level="3">
         <item numDisplay="1." level="3">1</item>
         <item numDisplay="2." level="3">2</item>
         <item numDisplay="3." level="3">3</item>
         <item numDisplay="4." level="3">4</item>
      </section>
   </section>
   <section level="2">
      <item numDisplay="B." level="2">2</item>
      <section level="3">
         <item numDisplay="1." level="3">1</item>
         <item numDisplay="2." level="3">2</item>
         <item numDisplay="3." level="3">3</item>
      </section>
   </section>
   <section level="2">
      <item numDisplay="C." level="2">3</item>
   </section>
</section>
t:\ftemp>type lou2.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
<xsl:output indent="yes"/>

<xsl:template match="/" name="do-next-group">
  <xsl:param name="group" select="/*/item"/>
  <xsl:param name="level" select="1"/>
  <xsl:choose>
    <xsl:when test="$group[@level!=$level]">
      <!--then more splitting of the group is necessary-->
      <xsl:for-each-group select="$group"
                          group-starting-with="item[@level=$level]">
        <xsl:variable name="this-position" select="position()"/>
        <section level="{$level}">
          <!--put the current level into this section-->
          <xsl:for-each select="current-group()[@level=$level]">
            <xsl:copy>
              <xsl:copy-of select="@*"/>
              <xsl:value-of select="$this-position"/>
            </xsl:copy>
          </xsl:for-each>
          <!--only nest when this group has members from deeper levels-->
          <xsl:if test="current-group()[@level!=$level]">
            <xsl:call-template name="do-next-group">
              <xsl:with-param name="group"
                              select="current-group()[@level!=$level]"/>
              <xsl:with-param name="level" select="$level+1"/>
            </xsl:call-template>
          </xsl:if>
        </section>
      </xsl:for-each-group>
    </xsl:when>
    <xsl:otherwise>
      <!--no more splitting of the group is necessary-->
      <section level="{$level}">
        <xsl:for-each select="$group">
          <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:value-of select="position()"/>
          </xsl:copy>
        </xsl:for-each>
      </section>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>

--
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour video lecture: XSLT/XPath 1.0 & 2.0 http://ude.my/t37DVX
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/
G. Ken Holman                   mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal

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.