[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: Numbering - how to exclude "cousin" elements?
I don't know if you can do this with a single number element, here is a template that groups the nodes with Standalone attributes set to 'true', and everything else. <xsl:template match="Sentence"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:choose> <xsl:when test="../@Standalone='true'"> <xsl:attribute name="Number"> <xsl:number count="Sentence" level="single"></xsl:number> </xsl:attribute> </xsl:when> <xsl:otherwise> <xsl:attribute name="Number"> <xsl:number count="Sentence[not(../@Standalone) or ../@Standalone != 'true']" level="any"></xsl:number> </xsl:attribute> </xsl:otherwise> </xsl:choose> <xsl:apply-templates/> </xsl:copy> </xsl:template> Josh -----Original Message----- From: Glen Little [mailto:glittle@xxxxxxxxxxxxxxxxxxxxx] Sent: Thursday, June 17, 2004 11:48 AM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Numbering - how to exclude "cousin" elements? 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|