Subject: Re: [xsl] a table-of-contents for XHTML problem From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Sun, 15 Jun 2008 15:30:44 -0400 |
I wonder, what would be the simplest way to transform a sequence of sibling nodes, e. g.: ... into a nested list, like: ... in XSLT 1.0?
The following conditions are assumed to be true:
* all the `hN' nodes are the children of a single `body' node;
* for any consequent elements `hN' and `hM', M <= 1 + N; the first child of the `body' node is `h1'; i. e., it's assumed that, e. g., the following input could never happen:
<body> <h1>Foo</h1> <h3>Bar</h3> </body>
t:\ftemp>type ivan.xml <body> <h1>Chapter</h1> <h2>Section</h2> <h3>Subsection</h3> <h3>Another subsection</h3> <h2>Another section</h2> </body>
t:\ftemp>type ivan.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--for each possible child, indicate who they are a child of--> <xsl:key name="nested" match="h2" use="generate-id(preceding-sibling::h1[1])"/> <xsl:key name="nested" match="h3" use="generate-id(preceding-sibling::h2[1])"/> <xsl:key name="nested" match="h4" use="generate-id(preceding-sibling::h3[1])"/> <xsl:key name="nested" match="h5" use="generate-id(preceding-sibling::h4[1])"/> <xsl:key name="nested" match="h6" use="generate-id(preceding-sibling::h5[1])"/>
<!--get things started--> <xsl:template match="body"> <xsl:copy> <ul> <xsl:apply-templates select="h1"/> </ul> </xsl:copy> </xsl:template>
<!--for each layer, show title and present only the children--> <xsl:template match="h1|h2|h3|h4|h5|h6"> <li> <!--this does the title--> <xsl:apply-templates/> <!--this does the children (if they exist)--> <xsl:variable name="children" select="key('nested',generate-id(.))"/> <xsl:if test="$children"> <ul> <xsl:apply-templates select="$children"/> </ul> </xsl:if> </li> </xsl:template>
</xsl:stylesheet> t:\ftemp>xslt ivan.xml ivan.xsl con <?xml version="1.0" encoding="utf-8"?> <body> <ul> <li>Chapter<ul> <li>Section<ul> <li>Subsection</li> <li>Another subsection</li> </ul> </li> <li>Another section</li> </ul> </li> </ul> </body> t:\ftemp>
-- Upcoming XSLT/XSL-FO hands-on courses: Wellington, NZ 2009-01 World-wide corporate, govt. & user group XML, XSL and UBL training RSS feeds: publicly-available developer resources and training G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] a table-of-contents for X, Florent Georges | Thread | Re: [xsl] a table-of-contents for X, Ivan Shmakov |
Re: [xsl] a table-of-contents for X, Florent Georges | Date | Re: [xsl] a table-of-contents for X, Ivan Shmakov |
Month |