Numbering - how to exclude "cousin" elements?

Subject: Numbering - how to exclude "cousin" elements?
From: "Glen Little" <glittle@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 17 Jun 2004 12:48:02 -0600
I?ve got a problem trying to number elements in my document. 
 
For use in this discussion, I?ve created a small sample XML file that has a
similar structure.  (My real structure is not actually paragraphs and
sentences. Hopefully the sample is big enough to show the issues.)
<Section>
       <Page>
             <Paragraph>
                    <Sentence>Sentence A</Sentence>
             </Paragraph>
       </Page>
       <Page>
             <Paragraph Standalone="true">
                    <Sentence>Sentence B</Sentence>
                    <Sentence>Sentence C</Sentence>
             </Paragraph>
             <Paragraph>
                    <Sentence>Sentence D</Sentence>
                    <Sentence>Sentence E</Sentence>
             </Paragraph>
       </Page>
</Section>
 
I want to number the sentences, but exclude some Sentences from the general
numbering scheme, and give them their own numbers.
 
Here is an XSL file to number all the sentences (not excluding any):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
       <xsl:template match="Sentence">
             <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:attribute name="Number">
                           <xsl:number count="Sentence" level="any"
></xsl:number>
                    </xsl:attribute>
                    <xsl:apply-templates/>
             </xsl:copy>
       </xsl:template>
       <xsl:template match="*">
             <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates/>
             </xsl:copy>
       </xsl:template>
</xsl:stylesheet>
 
And the output (not quite what I want)
<Section>
       <Page>
             <Paragraph>
                    <Sentence Number="1">Sentence A</Sentence>
             </Paragraph>
       </Page>
       <Page>
             <Paragraph Standalone="true">
                    <Sentence Number="2">Sentence B</Sentence>
                    <Sentence Number="3">Sentence C</Sentence>
             </Paragraph>
             <Paragraph>
                    <Sentence Number="4">Sentence D</Sentence>
                    <Sentence Number="5">Sentence E</Sentence>
             </Paragraph>
       </Page>
</Section>
 
If I change the XSL to takes the ?Standalone? into consideration:
                           <xsl:number count="Sentence" level="any"
from="Paragraph[@Standalone='true'] | Section"/>
 
Then I get closer: 
<Section>
       <Page>
             <Paragraph>
                    <Sentence Number="1">Sentence A</Sentence>
             </Paragraph>
       </Page>
       <Page>
             <Paragraph Standalone="true">
                    <Sentence Number="1">Sentence B</Sentence>
                    <Sentence Number="2">Sentence C</Sentence>
             </Paragraph>
             <Paragraph>
                    <Sentence Number="3">Sentence D</Sentence>
                    <Sentence Number="4">Sentence E</Sentence>
             </Paragraph>
       </Page>
</Section>
 
In this output, the Standalone paragraph is correctly numbered on its own.
The problem is that the sentences following it are continuing the numbering
started in the Standalone area, since the level is ?any?. I want them to
continue the general numbering.


 
If I try to use ?multiple?:
                           <xsl:number count="Sentence" level="multiple"
from="Paragraph[@Standalone='true'] | Section"/>
 
then the Sentences on the first page are no longer counted on the second
page. Sentences are only numbered within their own page:
<Section>
       <Page>
             <Paragraph>
                    <Sentence Number="1">Sentence A</Sentence>
             </Paragraph>
       </Page>
       <Page>
             <Paragraph Standalone="true">
                    <Sentence Number="1">Sentence B</Sentence>
                    <Sentence Number="2">Sentence C</Sentence>
             </Paragraph>
             <Paragraph>
                    <Sentence Number="1">Sentence D</Sentence>
                    <Sentence Number="2">Sentence E</Sentence>
             </Paragraph>
       </Page>
</Section>
 
What I really want to see is this:
<Section>
       <Page>
             <Paragraph>
                    <Sentence Number="1">Sentence A</Sentence>
             </Paragraph>
       </Page>
       <Page>
             <Paragraph Standalone="true">
                    <Sentence Number="1">Sentence B</Sentence>
                    <Sentence Number="2">Sentence C</Sentence>
             </Paragraph>
             <Paragraph>
                    <Sentence Number="2">Sentence D</Sentence>
                    <Sentence Number="3">Sentence E</Sentence>
             </Paragraph>
       </Page>
</Section>
 
Do you have any suggestions on how to accomplish this?  I expect that the
xsl:number element will need to have more/better attributes and filters to
solve the problem. (I?m using MSXML 4.)
 
Please test any solution to make sure the output is like my last sample
output, and that the numbering within the Standalone section is not lost!
 
Thanks,
Glen
 
Current Thread