Re: [xsl] Dealing without global counters

Subject: Re: [xsl] Dealing without global counters
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Tue, 11 Nov 2003 22:18:14 -0700
Hi Robert:

The following XML...  If I understand correctly what you are saying the
following XML...

<?xml version="1.0" encoding="utf-8" ?>

<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<chapters>
<chapter>
    <para>testPara_one
    </para>
    <para>testPara_two
    </para>
    <image>testImage_one
    </image>
    <para>testPara_three
    </para>
    <image>testImage_two
    </image>
    <table>testTable
    </table>
    <para>testPara_four
    </para>
</chapter>

<chapter>
    <para>testTwoPara_one
    </para>
    <para>testTwoPara_two
    </para>
    <image>testTwoImage_one
    </image>
    <para>testTwoPara_three
    </para>
    <image>testTwoImage_two
    </image>
    <table>testTwoTable
    </table>
    <para>testTwoPara_four
    </para>
</chapter>
</chapters>

Applied to the following stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet
 version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns="http://www.w3.org/TR/REC-html40";>


 <xsl:template match="/">

  <xsl:apply-templates/>

 </xsl:template>

 <xsl:template match="chapters/chapter">
 Chapter: <xsl:number/><br/>

  Paragraphs:<br/>
  <xsl:for-each select="para">
   &gt;&gt;&#160;
   <xsl:number/> :
   <xsl:value-of select="."/><br/>
  </xsl:for-each>

  Images:<br/>
  <xsl:for-each select="image">
   &gt;&gt;&#160;
   <xsl:number/> :
   <xsl:value-of select="."/><br/>
  </xsl:for-each>

  Tables:<br/>
  <xsl:for-each select="table">
   &gt;&gt;&#160;
   <xsl:number/> :
   <xsl:value-of select="."/><br/>
  </xsl:for-each>

 End of Chapter <xsl:number/><br/><br/>

 </xsl:template>

</xsl:stylesheet>

Will give you the following output (which is what I think you said you want.
Let me know if it something different)...

Chapter: 1
Paragraphs:
>>  1 : testPara_one
>>  2 : testPara_two
>>  3 : testPara_three
>>  4 : testPara_four
Images:
>>  1 : testImage_one
>>  2 : testImage_two
Tables:
>>  1 : testTable
End of Chapter 1

Chapter: 2
Paragraphs:
>>  1 : testTwoPara_one
>>  2 : testTwoPara_two
>>  3 : testTwoPara_three
>>  4 : testTwoPara_four
Images:
>>  1 : testTwoImage_one
>>  2 : testTwoImage_two
Tables:
>>  1 : testTwoTable
End of Chapter 2


Let me know if you desire a different output.

Best of luck!

M.



----- Original Message ----- 
From: "Robert Ogden" <Robert.Ogden@xxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Tuesday, November 11, 2003 5:24 PM
Subject: [xsl] Dealing without global counters


> I have done a bit of reading, and it seems global counters are not the
> answer in XSLT. That being said I have a problem (normally solved by a
> global counter) I need to solve. I'm building a TOC for an XML
> document.
> The best way is to offer xml structure and intended output.
>
> XML:
> <chapter>
>     <para>
>     </para>
>     <para>
>     </para>
>     <image>
>     </image>
>     <para>
>     </para>
>     <image>
>     </image>
>     <table>
>     <table>
>     <para>
>     <para>
> </chapter>
>
> Output (this output is a javascript array for an expandable TOC)
> db[1]=new dbRecord(chapter stuff)
> db[2]=new dbRecord(first para stuff)
> db[3]=new dbRecord(second para stuff)
> db[4]=new dbRecord(third para stuff)
> db[5]=new dbRecord(fourth para stuff)
> db[6]=new dbRecord(first image stuff)
> db[7]=new dbRecord(second image stuff)
> db[8]=new dbRecord(first table stuff)
>
> What I want to do is output a chapter, all text in a chapter, then all
> images for a chapter, and lastly all tables for a chapter.
> This is done repetitively for each chapter.
>
> For the text, calling a template that contains <xsl:number> would work.
> I would get a sequential numbering in the db[] (which IS required,
> db[number] where number is sequential 1 to N). But as you see the
> figures and tables are interlaced in the XML, and not desired to be that
> way in the TOC.
>
> Any help would be appreciated.
> Thanks,
>
> Robert Ogden
> IETM Developer
> Navy Programs
> (763) 572-7121
>
>  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