RE: [xsl] Change text on given node at runtime

Subject: RE: [xsl] Change text on given node at runtime
From: jeff.day@xxxxxx
Date: Thu, 24 Jul 2003 10:26:59 +0100
Yes, having looked at Jeni's XSL it is way over the top for my needs and
doesn't seem to actually do what I want.  Thanks to Jim Fuller for directing
me to it though - I'm sure I'll use it elsewhere  :0)

Mukul, the code you supplied is what I tried first of all but it doesn't
copy across attributes.

Here is where I am so far...

Vendor:  Apache Software Foundation
URL:  http://xml.apache.org/xalan-j
Version:  1

The following works fine but once it hits the node and question, and
processes it OK, it does not continue down into deeper nests below that
node. So I know what I want to do but can't quite crack how to do it. Any
help with this would be gratefully received.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

<xsl:param name="tag"/>     <!-- name of node containing text to be changed
-->
<xsl:param name="text"/>    <!-- new text to be placed in node -->

 <xsl:template match="node()|@*">
  <xsl:copy>

   <xsl:if test="name()=$tag">
    <xsl:apply-templates select="@*"/>
     <xsl:value-of select='$text'/>
   </xsl:if>

   <xsl:if test="not(name()=$tag)">
    <xsl:for-each select="@*|node()">
     <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
    </xsl:for-each>
   </xsl:if>

  </xsl:copy>
 </xsl:template>

</xsl:stylesheet>


Jeff 

-----Original Message-----
From: Mukul Gandhi [mailto:mukulw3@xxxxxxxxx]
Sent: 23 July 2003 17:54
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Change text on given node at runtime


Hi Jeff ,
  You can try a XSL something like below. This will
replace the contents of tag <A> with 'Some new text'
and copy other things to output as it is. But you want
to provide the tag name and the new text, *at run
time*. If you can tell what is your XSLT processor and
*how you would like to supply the 2 parameters at run
time*, i can try modifying the stylesheet accordingly.

 i don't know if Jeni's XSL is meeting your
requirement(it looks quite complicated to me for your
problem).

Regards,
Mukul

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>
 <xsl:template match="@* | node()">
   <xsl:copy>
     <xsl:apply-templates />
   </xsl:copy>	    
 </xsl:template>	
	
 <xsl:template match="A">
   <A>Some new text</A>
 </xsl:template>
	
</xsl:stylesheet>


--- jeff.day@xxxxxx wrote:
> Hi
> 
> Can you please give some guidance on this...
> 
> I would like to be able, at runtime, to parse an XML
> file and change 
> 
> <A>Original text</A>
> 
> to
> 
> <A>Some new text</A>
> 
> 
> On the next occasion I might want to do this...
> 
> <F>Old text</F>
> 
> change to
> 
> <F>New text</F>
> 
> So at runtime I need to pass in two parameters that
> specify the required
> node and the new text for it.
> 
> There are two issues: Either change the text in
> every node called <A>
> throughout the document or else only change <A>
> along a particular path. A
> solution to either (or both?) option will do for me.
> 
> Of course the rest of the document must remain in
> its entirety, e.g.
> attributes, children etc.
> 
> Many thanks for any help given.
> 
> Regards
> 
> Jeff 
> 
> 
>  XSL-List info and archive: 
> http://www.mulberrytech.com/xsl/xsl-list
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread