This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Newbie : conditional node move
- To: xsl-list at lists dot mulberrytech dot com
- Subject: Re: [xsl] Newbie : conditional node move
- From: Gavin Corfield <gavin at twobluedots dot com dot au>
- Date: Wed, 16 May 2001 17:30:30 +0800
- Organization: two blue dots
- References: <200105160630.CAA03395@biglist.com> <3B023728.22A4F6DF@swing.be>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Godefroid Chapelle wrote:
>
> Hi all,
>
> A question about a problem I tried to solve the whole yesterday evening...
>
> I could not find enough information or apply it correctly even in Michael Kay's
> book.
>
> I have the following two cases:
>
> 1)
> input
>
> <AAA>
> <BBB>bbb</BBB>
> <CCC>ccc</CCC>
> <DDD>ddd</DDD>
> <EEE>eee</EEE>
> </AAA>
>
> wished output
>
> <AAA>
> <DDD>
> <XXX>bbb</XXX>
> ddd</DDD>
> <EEE>eee</EEE>
> </AAA>
>
> 2)
> input
>
> <AAA>
> <BBB>zzz</BBB>
> <CCC>ccc</CCC>
> <DDD>ddd</DDD>
> <EEE>eee</EEE>
> </AAA>
>
> output
>
> <AAA>
> <DDD>ddd</DDD>
> <EEE>eee</EEE>
> </AAA>
>
> In words :
> There are nodes I want to delete (ie CCC) : I can do that
> There are nodes I want to copy (ie EEE) : I can do that also.
>
> There is a node I want to copy (ie DDD) and modify conditionnally depending on
> another node's value (ie BBB):
> THIS IS WHAT I WAS NOT ABLE TO DO.
>
Use:
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<!-- copy all-->
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- delete CCC-->
<xsl:template match="CCC | BBB" />
<!-- copy DDD and modify depending on BBB-->
<xsl:template match="DDD">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:if test="../BBB='bbb'" >
<XXX><xsl:value-of select="../BBB"/></XXX>
</xsl:if>
<xsl:value-of select="." />
</xsl:copy>
</xsl:template>
This does what you want, you need to nest the <xsl:if > inside the
<xsl:copy> to get the content inside the DDD tag in the output.
gavin
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list