Subject: [xsl] Suggestion for converting from flat file to a structured html type list From: "Nicholas Waltham" <info@xxxxxxxxxxxx> Date: Mon, 15 Oct 2001 11:11:12 +0200 |
Hello All, I have been working on this just lately, and I wondered if I could have your feedback. It may also be useful in a more general context. Its based on help here, and also from the FAQs. Regards, Nicholas Waltham -----------Source File ---------- <?xml version="1.0" encoding="UTF-8"?> <doc> <p style="Normal">Normal</p> <p style="L1"> - 1.1</p> <p style="L1"> - 1.2</p> <p style="L1"> - 1.3</p> <p style="L1"> - 1.4</p> <p style="L2"> - 1.4.1</p> <p style="L2"> - 1.4.2</p> <p style="L3"> - 1.4.2.1</p> <p style="L1">- 1.5</p> <p style="L1">- 1.6</p> <p style="Normal">Normal</p> <p style="L1">b First Level 1</p> <p style="L1">b Second Level 1</p> <p style="L2">b First <em>Level</em> 2</p> <p style="L2">b Second Level 2</p> <p style="L3">b First Level 3</p> <p style="L2">c Second Level 2</p> <p style="L3">c First Level 3</p> <p style="L1">b Third <em>Level</em> 1</p> <p style="L1">b Forth Level 1</p> </doc> -----------Desired Output ---------- <html> <body bgcolor="#ffffff"> Normal <ol> <li> - 1.1</li> <li> - 1.2</li> <li> - 1.3</li> <li> - 1.4</li> <ol> <li> - 1.4.1</li> <li> - 1.4.2</li> <ol> <li> - 1.4.2.1</li> </ol> </ol> <li>- 1.5</li> <li>- 1.6</li> </ol> Normal <ol> <li>b First Level 1</li> <li>b Second Level 1</li> <ol> <li>b First <em>Level</em> 2</li> <li>b Second Level 2</li> <ol> <li>b First Level 3</li> </ol> <li>c Second Level 2</li> <ol> <li>c First Level 3</li> </ol> </ol> <li>b Third <em>Level</em> 1</li> <li>b Forth Level 1</li> </ol> </body> </html> -----------XSL to do the job ---------- <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <!--Items in a list which are not the first items /ie list continuation items--> <xsl:key name='listitems1' match="p[@style='L1'and preceding-sibling::p[1][@style='L1' or @style='L2' or @style='L3']]" use="generate-id(preceding-sibling::p[@style='L1'][1])"/> <xsl:key name='listitems2' match="p[@style='L2'and preceding-sibling::p[1][ @style='L2' or @style='L3']]" use="generate-id(preceding-sibling::p[@style='L2' and preceding-sibling::p[1][@style='L1']][1])"/> <xsl:key name='listitems3' match="p[@style='L3'and preceding-sibling::p[1][@style='L3']]" use="generate-id(preceding-sibling::p[@style='L3' and preceding-sibling::p[1][@style='L2']][1])"/> <!--First Items in sublists--> <xsl:key name='sublistitems2' match="p[@style='L2' and preceding-sibling::p[1][@style='L1']]" use="generate-id(preceding-sibling::p[@style='L1'][1])"/> <xsl:key name='sublistitems3' match="p[@style='L3' and preceding-sibling::p[1][@style='L2']]" use="generate-id(preceding-sibling::p[@style='L2'][1])"/> <!--Process Level 1 Lists--> <xsl:template match="p[@style='L1']" mode="startlist"> <xsl:apply-templates select="." mode="Items"/> <xsl:apply-templates select="key('sublistitems2', generate-id())" mode="openlist"/> <xsl:apply-templates select="key('listitems1', generate-id())" mode="startlist"/> </xsl:template> <!--Process Level 2 Lists--> <xsl:template match="p[@style='L2']" mode="startlist"> <xsl:apply-templates select="." mode="Items"/> <xsl:apply-templates select="key('sublistitems3', generate-id())" mode="openlist"/> <xsl:apply-templates select="key('listitems2', generate-id())" mode="startlist"/> </xsl:template> <!--Process Level 3 Lists--> <xsl:template match="p[@style='L3']" mode="startlist"> <xsl:apply-templates select="." mode="Items"/> <xsl:apply-templates select="key('listitems3', generate-id())" mode="startlist"/> </xsl:template> <!--Start a list if the contents is not empty--> <xsl:template match="p[@style='L1'or @style='L2' or @style='L3']" mode="openlist"> <xsl:if test="string(.)!=''"> <ol> <xsl:apply-templates select="." mode="startlist"/> </ol> </xsl:if> </xsl:template> <!--Output Individual List Items--> <xsl:template match="p[@style='L1'or @style='L2' or @style='L3']" mode="Items"> <xsl:if test="string(.)!=''"> <li> <xsl:copy-of select="node()"/> </li> </xsl:if> </xsl:template> <!--Pick out the first level 1 item in a list, dump other items --> <xsl:template match="p[@style='L1'or @style='L2' or @style='L3']" > <xsl:if test="@style='L1' and not (preceding-sibling::p[1][@style='L1'or @style='L2' or @style='L3'])"> <ol> <xsl:apply-templates select="." mode="startlist"/> </ol> </xsl:if> </xsl:template> <xsl:template match="/"> <html> <head> </head> <body bgcolor="#ffffff"> <xsl:apply-templates /> </body> </html> </xsl:template> </xsl:stylesheet> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] selecting nodes - syntax?, Denis Kranjcec | Thread | [xsl] xtchunk.xsl - Navigation Imag, Brendan Boyle |
[xsl] selecting nodes - syntax?, Denis Kranjcec | Date | Re: [xsl] keys and idrefs - XSLT2 r, Jeni Tennison |
Month |