|
Subject: Re: [xsl] Flat to Structured: Handling List Items with Subordinate Paragraphs From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Tue, 26 May 2009 16:04:36 -0400 |
<p type="para" level="1">Para 1</p> <p container="ol" type="li" level="1">ol item 1</p> <p container="li" type="para" level="2">Para w/in li</para> <p container="ol" type="li" level="1">ol item 2</p> <p container="li" type="para" level="2">Para w/in li</para> <p type="para" level="1">Para 2</p>
Where the desired result is:
<para>Para 1</para> <ol> <li>ol item 1 <para>Para w/in li</para> </li> <li>ol item 2 <para>Para w/in li</para> </li> </ol> <para>Para 2</para>
I've reviewed various discussions of sibling recursion, which I'm pretty sure is what I need to apply to this problem.
But I'm having a very hard time wrapping my head around it--I suspect procedural brain damage.
What I'm not seeing is how, through sibling recursion, I can start a result wrapper and then end it. Likewise, doing something like for-each-group on all the level-1 elements would provide no way to group the list items under a common container.
I suspect there's some simple thing I'm missing here or some clever recursion/grouping technique that escapes me.
Can anyone point me in the right direction?
T:\ftemp>type eliot.xml <doc> <p type="para" level="1">Para 1</p> <p container="ol" type="li" level="1">ol item 1</p> <p container="li" type="para" level="2">Para w/in li</p> <p container="ol" type="li" level="1">ol item 2</p> <p container="li" type="para" level="2">Para w/in li</p> <p type="para" level="1">Para 2</p> </doc> T:\ftemp>xslt2 eliot.xml eliot.xsl <?xml version="1.0" encoding="UTF-8"?> <para>Para 1</para> <ol> <li>ol item 1<para>Para w/in li</para> </li> <li>ol item 2<para>Para w/in li</para> </li> </ol> <para>Para 2</para> T:\ftemp>type eliot.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="doc">
<!--determine containers by the members being adjacent-->
<xsl:for-each-group select="*" group-adjacent="boolean(@container)">
<xsl:choose>
<xsl:when test="@container">
<!--at a container of a particular type-->
<!--create the necessary kind of container-->
<xsl:element name="{@container}">
<!--populate the container-->
<xsl:for-each-group select="current-group()"
group-starting-with="*[@type='li']">
<li>
<xsl:apply-templates select="node()"/>
<xsl:apply-templates select="current-group()[position()>1]"/>
</li>
</xsl:for-each-group>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<!--not at a container; just constitute the elements-->
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template><xsl:template match="*">
<xsl:element name="{@type}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>-- XSLT/XSL-FO/XQuery hands-on training - Los Angeles, USA 2009-06-08 Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| [xsl] Flat to Structured: Handling , Eliot Kimber | Thread | Re: [xsl] Flat to Structured: Handl, Eliot Kimber |
| [xsl] Flat to Structured: Handling , Eliot Kimber | Date | Re: [xsl] Flat to Structured: Handl, Eliot Kimber |
| Month |