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

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

  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