RE: [xsl] Condition Based Count of Sections

Subject: RE: [xsl] Condition Based Count of Sections
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 13 Apr 2007 10:57:50 +0100
I can't work out the precise logic for your numbering - why is the chapter
1.1 and its first section 1.2 when the section is a child of the chapter?

However, you might be able to get what you want by adapting the following:

Michael Kay
http://www.saxonica.com/


<?xml version="1.0" encoding="utf-8"?> 
 
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";> 
 
<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>
 
<xsl:template match="chapter[p] | sect1[p] | sect2[p] | sect3[p]">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:attribute name="count">
      <xsl:number level="any" count="chapter[p] | sect1[p] | sect2[p] |
sect3[p]"/>
    </xsl:attribute>
    <xsl:attribute name="level">
      <xsl:number level="multiple" count="chapter | sect1 | sect2 | sect3"/>
    </xsl:attribute> 
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>  
 
</xsl:stylesheet>  

> -----Original Message-----
> From: J. S. Rawat [mailto:jrawat@xxxxxxxxxxxxxx] 
> Sent: 13 April 2007 10:30
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Condition Based Count of Sections
> 
> Hi:
> This seems a very complex condition as per my understanding 
> because I am new in this field. I want a "count" and "level" 
> attribute (as shown in the output file) of <chapter>,  
> <sect1> , <sect2> and  <sect3> on the following
> basis:
> 1. <sect> or <chapter> which does have paragraph should be 
> skiped because no section should be empty.
> 2.  For counting perpose <chapter>,  <sect1> , <sect2> and  
> <sect3> should be treated as same level.
> 3.  Depth of "level" attribute should be measure by its parent tag.
> 
> INPUT
> <chapter>
> <ti>Chapter title</ti>
> <p>PC DATA IS HERE</p>
> <p>PC DATA IS HERE</p>
> <sect1>
> <ti>SECT1: SECTION TITLE</ti>
> <sect2>
> <ti>SECT2: SECTION TITLE</ti>
> <p>Paragraph 1: PC DATA IS HERE</p>
> <p>Paragraph 2: PC DATA IS HERE</p>
> </sect2>
> <sect2>
> <ti>SECT2: SECTION TITLE</ti>
> <p>Paragraph 3: PC DATA IS HERE</p>
> <p>Paragraph 4: PC DATA IS HERE</p>
> </sect2>
> </sect1>
> <sect1>
> <ti>SECT1: SECTION TITLE</ti>
> <p>Paragraph 5: PC DATA IS HERE</p>
> <p>Paragraph 6: PC DATA IS HERE</p>
> <sect2>
> <ti>SECT2: SECTION TITLE</ti>
> <p>Paragraph 7: PC DATA IS HERE</p>
> <p>Paragraph 8: PC DATA IS HERE</p>
> <sect3>
> <ti>SECT3: SECTION TITLE</ti>
> <p>Paragraph 9: PC DATA IS HERE</p>
> <p>Paragraph 10: PC DATA IS HERE</p>
> </sect3>
> </sect2>
> </sect1>
> <sect1>
> <ti>SECT1: SECTION TITLE</ti>
> <p>Paragraph 11: PC DATA IS HERE</p>
> <p>Paragraph 12: PC DATA IS HERE</p>
> </sect1>
> </chapter>
> <chapter>
> <ti>Chapter title</ti>
> <sect1>
> <ti>SECT1: SECTION TITLE</ti>
> <p>Paragraph 3: PC DATA IS HERE</p>
> <p>Paragraph 4: PC DATA IS HERE</p>
> </sect1>
> </chapter>
> 
> OUTPUT
> <chapter count="1" level="1.1">
> <ti>Chapter title</ti>
> <p>PC DATA IS HERE</p>
> <p>PC DATA IS HERE</p>
> <sect1 count="2" level="1.2">
> <ti>SECT1: SECTION TITLE</ti>
> <sect2>
> <ti>SECT2: SECTION TITLE</ti>
> <p>Paragraph 1: PC DATA IS HERE</p>
> <p>Paragraph 2: PC DATA IS HERE</p>
> </sect2>
> <sect2 count="3" level="1.2.1">
> <ti>SECT2: SECTION TITLE</ti>
> <p>Paragraph 3: PC DATA IS HERE</p>
> <p>Paragraph 4: PC DATA IS HERE</p>
> </sect2>
> </sect1>
> <sect1 count="4" level="1.3">
> <ti>SECT1: SECTION TITLE</ti>
> <p>Paragraph 5: PC DATA IS HERE</p>
> <p>Paragraph 6: PC DATA IS HERE</p>
> <sect2 count="5" level="1.3.1">
> <ti>SECT2: SECTION TITLE</ti>
> <p>Paragraph 7: PC DATA IS HERE</p>
> <p>Paragraph 8: PC DATA IS HERE</p>
> <sect3 count="6" level="1.3.1.1">
> <ti>SECT3: SECTION TITLE</ti>
> <p>Paragraph 9: PC DATA IS HERE</p>
> <p>Paragraph 10: PC DATA IS HERE</p>
> </sect3>
> </sect2>
> </sect1>
> <sect1 count="7" level="1.4">
> <ti>SECT1: SECTION TITLE</ti>
> <p>Paragraph 11: PC DATA IS HERE</p>
> <p>Paragraph 12: PC DATA IS HERE</p>
> </sect1>
> </chapter>
> <chapter count="8" level="1.5">
> <ti>Chapter title</ti>
> <sect1>
> <ti>SECT1: SECTION TITLE</ti>
> <p>Paragraph 3: PC DATA IS HERE</p>
> <p>Paragraph 4: PC DATA IS HERE</p>
> </sect1>
> </chapter>
> 
> thanks in advance
> ... J. S. Rawat

Current Thread