|
Subject: Re: [xsl] Changing a Tree Walking, forward walk example From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Sat, 24 Jan 2009 11:39:33 +0900 |
The problem is that my list does not have an end list node if a new list starts immediately after an existing list. So for example this:
...
Needs to be changed to this:
...
I am finding it hard because I need to match a list node that does not contain "End List" to start a list as well as end it. So far I have not been able to detect if I am still in a list before a new list starts, in which case I need to close it and start a new one.
t:\ftemp>type kidd.xml <document> <p>Non List text</p> <list>List Heading Type 1</list> <p>First item</p> <p>Second item</p> <list>List Heading Type 2</list> <p>First item</p> <p>Second item</p> <p>Third item</p> <list>End List</list> <p>Unrelated Text</p> <p>Not in a list</p> <list>List Heading Type 1</list> <p>First item</p> <list>End List</list> <p>More Unrelated Text</p> <p>Not in a list</p> </document>
t:\ftemp>call xslt2 kidd.xml kidd.xsl
<?xml version="1.0" encoding="UTF-8"?>
<document>
<p>Non List text</p>
<list>
<li>First item</li>
<li>Second item</li>
</list>
<list>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</list>
<p>Unrelated Text</p>
<p>Not in a list</p>
<list>
<li>First item</li>
</list>
<p>More Unrelated Text</p>
<p>Not in a list</p>
</document>
t:\ftemp>type kidd.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="document">
<document>
<xsl:for-each-group select="*" group-starting-with="list">
<xsl:choose>
<xsl:when test="not(self::list)">
<!--there must be a group before the first list-->
<xsl:apply-templates select="current-group()"/>
</xsl:when>
<xsl:when test=".='End List'">
<!--this indicates that what follows isn't in a list-->
<xsl:apply-templates select="current-group()[position()>1]"/>
</xsl:when>
<xsl:otherwise>
<!--we are starting a list-->
<list>
<xsl:for-each select="current-group()[position()>1]">
<li><xsl:apply-templates/></li>
</xsl:for-each>
</list>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</document>
</xsl:template><xsl:template match="@*|node()"><!--identity for all other nodes-->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template></xsl:stylesheet> t:\ftemp>rem Done!
-- Upcoming XSLT/XSL-FO, UBL and code list hands-on training classes: : Sydney, AU 2009-01/02; Brussels, BE 2009-03; Prague, CZ 2009-03 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 Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ 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] Changing a Tree Walking, forw, Graeme Kidd | Thread | Re: [xsl] Changing a Tree Walking, , Graeme Kidd |
| [xsl] Changing a Tree Walking, forw, Graeme Kidd | Date | [xsl] Avoid xmlns (namespace), J. S. Rawat |
| Month |