Re: [xsl] XSLT - update attribute with new value

Subject: Re: [xsl] XSLT - update attribute with new value
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 4 Nov 2004 14:09:32 GMT
  The XSLT stylesheet nees to search for the var and replace its value
  with the new value.  It must also rewrite the rest of the file. The
  program performs other functions that are needed by the stylesheet and
  best performed by the program so the XSLT stylesheet's function is just
  to accept the var and value from the program and update the value in the
  XML file.

so your stylesheet is basically an identity transform, there is one in
the XSL REC and dozens in the archive of this list:


<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

but you need a couple of parameters (declared first)
<xsl:param name="attr"/>
<xsl:aram anme="value"/>

and then you want a template that fixes up that attribute

<xsl:template match="@*[name()=$attr]">
which you could do in xslt 2 draft but annoyingly you can't have
variables in match patterns in xslt 1 so instead match on all attributes
and test inside the template

<xsl:template match="@*" priority="10">
<xsl:attribute name="{name()}">
 <xsl:choose>
   <xsl:when test="name()=$attr"><xsl:value-of select="$value"/></xsl:when>
   <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
  </xsl:choose>
</xsl:attribute>
</xsl:template>


and that should be all you need.

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. 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
________________________________________________________________________

Current Thread