This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Newbie : conditional node move


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]