Subject: Re: [xsl] transforming siblings to a hierarchy From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Sat, 01 Sep 2007 10:48:41 -0400 |
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>
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">
<!--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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] transforming siblings to a hi, John Smith | Thread | Re: [xsl] transforming siblings to , John Smith |
[xsl] transforming siblings to a hi, John Smith | Date | Re: [xsl] transforming siblings to , John Smith |
Month |