Re: [xsl] Re: Wrapping problem

Subject: Re: [xsl] Re: Wrapping problem
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Tue, 15 Aug 2006 17:27:03 +0530
Thanks Mike..

On 8/15/06, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> Hi Mike,
>    Can you please post this approach in more detail. I am not
> able to find anything about it on google.
>

Since you insist:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="xml" indent="yes" />

<xsl:template match="/play">
 <play>
   <xsl:apply-templates select="scene" />
 </play>
</xsl:template>

<xsl:template match="scene">
 <scene name="{.}">
   <xsl:apply-templates select="following-sibling::character[1]"/>
 </scene>
</xsl:template>

<xsl:template match="character">
   <character name="{.}">
       <xsl:apply-templates select="following-sibling::*[1][self::line]"/>
   </character>
   <xsl:apply-templates
select="following-sibling::*[self::character|self::scene][1][self::character
]"/>
</xsl:template>

<xsl:template match="line">
   <xsl:copy-of select="."/>
   <xsl:apply-templates select="following-sibling::*[1][self::line]"/>
</xsl:template>

</xsl:stylesheet>

I call the technique "sibling recursion". It's more concise than most
recursive code, because it's self-terminating: you don't need to test
explicitly whether there are more items to process, because apply-templates
does that automatically for you.

Michael Kay
http://www.saxonica.com/

-- Regards, Mukul Gandhi

http://gandhimukul.tripod.com

Current Thread