Subject: Re: limitations of preceding-sibling and following-sibling From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Fri, 22 Oct 1999 08:12:38 -0700 |
I'm hitting a limitation of xPath with preceding-sibling and following-sibling.
In which I'm having trouble creating the following:
<ul> <li>first list element</li> <li>second list element</li> <ul> <text>some text</text> <ul> <li>another list</li> <li>second anon list</li> </ul>
from the following:
<textItems> <bullet>first list element</bullet> <bullet>second list element</bullet> <normal>some text</normal> <bullet>another list</bullet> <bullet>second anon list</bullet> </textItems>
I have the following stylesheet that will grab the first group of bullets, but it will grab all the bullets and not seperate the other group of bullets.
These appears to be a limitation of axis groupings.
<xsl:for-each select="//normal|//bullet"> <xsl:choose> <xsl:when select="self::normal"> </xsl:when> <xsl:when select="self::bullet"> <xsl:if test="not(preceding-sibling::bullet)"> <ul> <xsl:for-each select="self::bullet|following::bullet"> <li><xsl:value-of select="."/></li> </xsl:for-each> </ul> </xsl:if> </xsl:when> </xsl:choose> </xsl:for-each>
following-sibling::bullet - all following siblings named "bullet" following-sibling::*[self::bullet] - all following siblings named "bullet" following-sibling::bullet[1] - the first following sibling named "bullet" following-sibling::*[1] - the immediately following sibling regardless of that sibling's name following-sibling::*[1][self::bullet] - the immediate following sibling only if it is named "bullet"
T:\earl>type earl.xml <?xml version="1.0"?> <textItems> <bullet>first list element</bullet> <bullet>second list element</bullet> <normal>some text</normal> <bullet>another list</bullet> <bullet>second anon list</bullet> </textItems>
T:\earl>type earl.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="textItems"> <result> <xsl:apply-templates/> </result> </xsl:template>
<xsl:template match="bullet"> <xsl:choose> <!--when there is an immediate sibling of the same name, then assume this node has already been addressed by the first of the contiguous siblings--> <xsl:when test="preceding-sibling::*[1][self::bullet]"/> <xsl:otherwise><!--at the first of a group of siblings--> <ul><!--"walk" along all sibling bullets for list items--> <xsl:apply-templates select="." mode="sibling-bullets"/> </ul> </xsl:otherwise> </xsl:choose> </xsl:template>
<!--place each of a group of adjacent siblings--> <xsl:template match="bullet" mode="sibling-bullets"> <li><xsl:apply-templates/></li> <!--put out this one--> <!--go to next one--> <xsl:apply-templates select="following-sibling::*[1][self::bullet]" mode="sibling-bullets"/> </xsl:template>
<xsl:template match="normal"> <!--other stuff--> <text><xsl:apply-templates/></text> </xsl:template>
T:\earl>call xsl earl.xml earl.xsl earl.out T:\earl>type earl.out <result> <ul> <li>first list element</li> <li>second list element</li> </ul>
<text>some text</text> <ul> <li>another list</li> <li>second anon list</li> </ul>
-- 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 (Fax:-0995) Web site: XSL/XML/DSSSL/SGML services, training, libraries, products. Practical Transformation Using XSLT and XPath ISBN 1-894049-02-0 Next instructor-led training: 1999-11-08, 1999-11-09, 1999-12-05/06, - 1999-12-07, 2000-02-27/28, 2000-05-11/12, 2000-05-15
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
limitations of preceding-sibling an, Earl Bingham | Thread | Fwd: Re: limitations of preceding-s, Earl Bingham |
Re: XSL FOs -> PDF using InDesign, Sara Mitchell | Date | Re: XSL and the DOM, Jon Smirl |
Month |