[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Change text on given node at runtime

Subject: Re: Change text on given node at runtime
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 24 Jul 2003 12:46:15 +0100
xsl change element name

  Since you are using Xalan-J, i have used a Xalan
  extension function nodeset which helps me enumerate
  over the attributes.

You didn't need it there

   <xsl:for-each select="xalan:nodeset(@*)">

@* returns a node set so you could just have gone

   <xsl:for-each select="@*">

(since the input to xx:node-set() is supposed to be a
result-tree-fragment, I think some engines would actually error
if you pass in a node set.)

      <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>

  since you are not changing the name or the value that whole loop could
  could be

<xsl:copy-of select="@*/>

In both branches of your template you are copying the element and all
its attributes, the only difference is whether to apply templates or to
insert the text param, so you can simplify by moving the element and
attribute handling to the shared part, ie:

   <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>
   
   
can be simplified to:
   

<xsl:template match="*">
  <xsl:copy>
   <xsl:copy-of select="@*/>
    <xsl:choose>
      <xsl:when test="name()=$tag"><xsl:value-of select="$text"/></xsl:when>
      <xsl:otherwise><xsl:apply-templates/></xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.