Re: [xsl] Condition Based Count of Sections

Subject: Re: [xsl] Condition Based Count of Sections
From: "J. S. Rawat" <jrawat@xxxxxxxxxxxxxx>
Date: Wed, 18 Apr 2007 12:09:15 +0530
I have lot of combination included suggested by you but still we are not able to skip the numbering as the logic is cleared by REQUIRED OUTPUT.
thanks
...JSR


XSL
<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="book | chapter[p] | sect1[p] | sect2[p] | sect3[p]"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:number level="multiple" count="book | chapter | sect1 | sect2 | sect3"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>


INPUT
<book>
<chapter>
<ti>Chapter title</ti>
<sect1>
<ti>SECT1: SECTION TITLE</ti>
<sect2>
<ti>SECT2: SECTION TITLE</ti>
<sect3>
<ti>SECT2: SECTION TITLE</ti>
<p>Paragraph 3: PC DATA IS HERE</p>
<p>Paragraph 4: PC DATA IS HERE</p>
</sect3>
</sect2>
</sect1>
<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>
</book>

OUTPUT
<?xml version="1.0" encoding="utf-8"?><book>
<chapter>
<ti>Chapter title</ti>
<sect1>
<ti>SECT1: SECTION TITLE</ti>
<sect2>
<ti>SECT2: SECTION TITLE</ti>
<sect3 count="2" level="1.1.1.1.1">
<ti>SECT2: SECTION TITLE</ti>
<p>Paragraph 3: PC DATA IS HERE</p>
<p>Paragraph 4: PC DATA IS HERE</p>
</sect3>
</sect2>
</sect1>
<sect1>
<ti>SECT1: SECTION TITLE</ti>
<sect2 count="3" level="1.1.2.1">
<ti>SECT2: SECTION TITLE</ti>
<p>Paragraph 1: PC DATA IS HERE</p>
<p>Paragraph 2: PC DATA IS HERE</p>
</sect2>
<sect2 count="4" level="1.1.2.2">
<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="5" level="1.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="6" level="1.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="7" level="1.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="8" level="1.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>
<ti>Chapter title</ti>
<sect1 count="9" level="1.2.1">
<ti>SECT1: SECTION TITLE</ti>
<p>Paragraph 3: PC DATA IS HERE</p>
<p>Paragraph 4: PC DATA IS HERE</p>
</sect1>
</chapter>
</book>

REQUIRED OUTPUT
<book>
<chapter count="1" level="1.1">
<ti>Chapter title</ti>
<sect1>
<ti>SECT1: SECTION TITLE</ti>
<sect2>
<ti>SECT2: SECTION TITLE</ti>
<sect3>
<ti>SECT2: SECTION TITLE</ti>
<p>Paragraph 3: PC DATA IS HERE</p>
<p>Paragraph 4: PC DATA IS HERE</p>
</sect3>
</sect2>
</sect1>
<sect1 count="2" level="1.1.1">
<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.1.1.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.1.2">
<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.1.2.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.1.2.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.1.3">
<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.2">
<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>
</book>

At 12:44 PM 4/13/2007 +0200, you wrote:
Abel Braaksma wrote:
J. S. Rawat wrote:

<sect2 count="2" level="1.1.1"> <ti>SECT2: SECTION TITLE</ti> <p>Paragraph 1: PC DATA IS HERE</p> <p>Paragraph 2: PC DATA IS HERE</p> </sect2> ...

It is towards the direction but
1. <sect2> should not count because its parent <sect1> does not have any <p>

?? You could do something like: <xsl:if test="not(self::sect2 and parent::sect1/p)" > ...

around your xsl:attribute statements.

Perhaps better to change your match clause, which does away the xsl:if. I.e., change:


<xsl:template match="chapter[p] | sect1[p] | sect2[p] | sect3[p]">

into:

<xsl:template match="chapter[p] | sect1[p] | sect2[p][../sect1/p] | sect3[p][../(sect1 | sect2)/p]">

If you stick to XSLT 1.0, you'll have to change the parenthesized expression into two expressions.

-- AB

Current Thread