Re: [xsl] transforming siblings to a hierarchy

Subject: Re: [xsl] transforming siblings to a hierarchy
From: "John Smith" <debrief@xxxxxxxxx>
Date: Sat, 1 Sep 2007 15:57:31 +0100
sorry my bad, I missed out the <e/> at the end. It's not a programming
exercise but I was trying to simplify my problem instead of posting
large xml and xslt content to avoid cluttering the actual problem.

thanks,
John

On 9/1/07, G. Ken Holman <gkholman@xxxxxxxxxxxxxxxxxxxx> wrote:
> At 2007-09-01 15:13 +0100, John Smith wrote:
> >How can change the following:
> >
> ><a/>
> ><plus/>
> ><b/>
> ><minus/>
> ><c/>
> ><plus/>
> ><d/>
> ><plus/>
> ><e/>
> >
> >To the following:
> >
> >
> ><addition>
> >     <addition>
> >         <takeaway>
> >             <addition>
> >                 <a/>
> >                 <b/>
> >            </addition>
> >            </c>
> >         </takeaway>
> >         <d/>
> >     </addition>
> ></addition>
>
> What feature of XSLT are you asking about?  A solution is below, but
> I have no idea if that helps you unless you express what it is about
> XSLT that is preventing you from implementing the answer.
>
> It would seem to me that XSLT is not the appropriate language to use for this.
>
> What is below is incomplete because you don't say what happens when
> there are errors.
>
> And I'm assuming your sample output above is wrong because you don't
> indicate anything for <e/>.
>
> I hope this helps, but I don't see what this has to do with learning
> XSLT ... it seems to me to be more of a programming exercise.
>
> . . . . . . . . . . . . . Ken
>
>
> t:\ftemp>type smith.xml
> <ops>
> <a/>
> <plus/>
> <b/>
> <minus/>
> <c/>
> <plus/>
> <d/>
> <plus/>
> <e/>
> </ops>
>
> t:\ftemp>type smith.xsl
> <?xml version="1.0" encoding="US-ASCII"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>                  version="1.0">
>
> <xsl:output indent="yes"/>
>
> <!--start things off with the first operand-->
> <xsl:template match="ops">
>    <xsl:apply-templates select="*[1]"/>
> </xsl:template>
>
> <!--operator creates structure-->
> <xsl:template match="plus">
>    <xsl:param name="running"/>
>    <xsl:apply-templates select="following-sibling::*[1]">
>      <xsl:with-param name="running">
>        <addition>
>          <xsl:copy-of select="$running"/>
>          <xsl:copy-of select="following-sibling::*[1]"/>
>        </addition>
>      </xsl:with-param>
>    </xsl:apply-templates>
> </xsl:template>
>
> <!--operator creates structure-->
> <xsl:template match="minus">
>    <xsl:param name="running"/>
>    <xsl:apply-templates select="following-sibling::*[1]">
>      <xsl:with-param name="running">
>        <takeaway>
>          <xsl:copy-of select="$running"/>
>          <xsl:copy-of select="following-sibling::*[1]"/>
>        </takeaway>
>      </xsl:with-param>
>    </xsl:apply-templates>
> </xsl:template>
>
> <!--as long as there are still operands, there must be operators-->
> <xsl:template match="*">
>    <xsl:param name="running" select="."/>
>    <xsl:choose>
>      <xsl:when test="not(following-sibling::*)">
>        <xsl:copy-of select="$running"/>
>      </xsl:when>
>      <xsl:otherwise>
>        <xsl:apply-templates select="following-sibling::*[1]">
>          <xsl:with-param name="running" select="$running"/>
>        </xsl:apply-templates>
>      </xsl:otherwise>
>    </xsl:choose>
> </xsl:template>
>
> </xsl:stylesheet>
> t:\ftemp>call xslt smith.xml smith.xsl smith.out
>
> t:\ftemp>type smith.out
> <?xml version="1.0" encoding="utf-8"?>
> <addition>
>     <addition>
>        <takeaway>
>           <addition>
>              <a/>
>              <b/>
>           </addition>
>           <c/>
>        </takeaway>
>        <d/>
>     </addition>
>     <e/>
> </addition>
> t:\ftemp>rem Done!
>
>
> --
> Upcoming public training: XSLT/XSL-FO Sep 10, UBL/code lists Oct 1
> 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 Jul'07  http://www.CraneSoftwrights.com/s/bc
> Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread