xsl-list
[Top] [All Lists]

Re: [xsl] Numbering new nodes using consecutive integers

2009-03-27 13:13:03
At 2009-03-27 17:40 +0100, Michael Ludwig wrote:
I added an additional level <G2> to a hierarchy of <Groups> and <G1>.
I want to assign numbers to the newly created <G2> nodes, which identify
them in the document. I managed to do so using <xsl:number>.

<xsl:number> should be used for adding text to the result tree and only used in variables as a last resort.

Now it would be perfect if the numbering was done as a strict sequence
of consecutive integers, meaning 1,2,3 instead of, say, 1,3,5.

Can this be done in 1.0, or 2.0, for that matter?

If I've understood what you need, the XSLT 2.0 example below will number all groups sequentially.

I'll have to wait to find more time to do this in XSLT 1 as it isn't nearly as easy as it is in XSLT 2.

I hope this helps.

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

T:\ftemp>type michael.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<Groups>
  <G1>
    <M>eins</M>
    <M>zwei</M>
    <M>drei</M>
    <M>vier</M>
    <M>fünf</M>
    <M>sechs</M>
    <M>sieben</M>
  </G1>
  <G1>
    <M>acht</M>
    <M>neun</M>
    <M>zehn</M>
    <M>elf</M>
    <M>zwölf</M>
  </G1>
  <G1>
    <M>eins</M>
    <M>zwei</M>
    <M>drei</M>
    <M>vier</M>
    <M>fünf</M>
    <M>sechs</M>
    <M>sieben</M>
  </G1>
  <G1>
    <M>acht</M>
    <M>neun</M>
    <M>zehn</M>
    <M>elf</M>
    <M>zwölf</M>
  </G1>
</Groups>
T:\ftemp>call xslt2 michael.xml michael.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<Groups>
  <G1>
      <G2 no="1">
         <M>eins</M>
         <M>zwei</M>
      </G2>
      <G2 no="2">
         <M>drei</M>
         <M>vier</M>
      </G2>
      <G2 no="3">
         <M>fünf</M>
         <M>sechs</M>
      </G2>
      <G2 no="4">
         <M>sieben</M>
      </G2>
   </G1>
  <G1>
      <G2 no="5">
         <M>acht</M>
         <M>neun</M>
      </G2>
      <G2 no="6">
         <M>zehn</M>
         <M>elf</M>
      </G2>
      <G2 no="7">
         <M>zwölf</M>
      </G2>
   </G1>
  <G1>
      <G2 no="8">
         <M>eins</M>
         <M>zwei</M>
      </G2>
      <G2 no="9">
         <M>drei</M>
         <M>vier</M>
      </G2>
      <G2 no="10">
         <M>fünf</M>
         <M>sechs</M>
      </G2>
      <G2 no="11">
         <M>sieben</M>
      </G2>
   </G1>
  <G1>
      <G2 no="12">
         <M>acht</M>
         <M>neun</M>
      </G2>
      <G2 no="13">
         <M>zehn</M>
         <M>elf</M>
      </G2>
      <G2 no="14">
         <M>zwölf</M>
      </G2>
   </G1>
</Groups>
T:\ftemp>type michael.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:output indent="yes" encoding="iso-8859-1"/>

<xsl:variable name="M-per-G2" select="2"/>

<xsl:template match="G1">
  <xsl:copy>
    <xsl:for-each-group select="M"
                        group-by="(position()-1) idiv $M-per-G2">
      <G2>
        <xsl:attribute name="no"
                       select="position() (:within this group:) +
                               (:count of subgroups within previous groups:)
                               sum( for $g1 in ../preceding-sibling::G1 return
                                    ((count($g1/M)-1) idiv $M-per-G2 + 1) )"/>
        <xsl:copy-of select="current-group()"/>
      </G2>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>rem Done!



--
XSLT/XSL-FO/XQuery training in Los Angeles (New dates!) 2009-06-08
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--