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

Subject: RE: [xsl] Change text on given node at runtime
From: Mukul Gandhi <mukulw3@xxxxxxxxx>
Date: Thu, 24 Jul 2003 04:20:47 -0700 (PDT)
Hi Jeff,
  i tried to solve your problem. Below is the complete
XSL and XML(i have assumed an example XML). Please let
me know if my stylesheet works with your actual XML.
Since you are using Xalan-J, i have used a Xalan
extension function nodeset which helps me enumerate
over the attributes.

Sample XML
----------
<?xml version="1.0" encoding="UTF-8"?>
<rootelem>
<A id="1" name="name1">aaa</A>
<B id="2" name="name2">bbb</B>
<C id="3" name="name3">ccc</C>
<D id="4" name="name4">ddd</D>
<E id="5" name="name5">eee</E>
<F id="6" name="name6">fff</F>
<G id="7" name="name7">ggg</G>
<H id="8" name="name8">hhh</H>
</rootelem>

XSL
---

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xalan="http://xml.apache.org/xalan";
exclude-result-prefixes="xalan">
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>

<xsl:param name="tag"/>
<xsl:param name="text"/>
	
<xsl:template match="@* | node()">
   <xsl:if test="(name(.) = $tag)">
     <xsl:element name="{$tag}">
       <xsl:for-each select="xalan:nodeset(@*)">
	<xsl:variable name="attName" select="name(.)"/>
	<xsl:attribute name="{$attName}"><xsl:value-of
select="."/></xsl:attribute>
      </xsl:for-each>
      <xsl:value-of select="$text"/>
     </xsl:element>
   </xsl:if>	     

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

</xsl:stylesheet>

Regards,
Mukul


--- jeff.day@xxxxxx wrote:
> 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
> 


__________________________________
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


Current Thread