[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: transforming siblings to a hierarchy

Subject: Re: transforming siblings to a hierarchy
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 01 Sep 2007 10:48:41 -0400
Re:  transforming siblings to a hierarchy
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.