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

Subject: Re: [xsl] XSLT - update attribute with new value
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Wed, 10 Nov 2004 15:47:09 -0800
Hi Ann Marie,

Ann Marie Rubin wrote:

Many thanks to everyone who offered help with my stylesheet. Everyone's
suggestions worked when using a hardcoded value for the attribute name. But I need to use parameters.


Just as is the case with adding dynamic data inline to any attribute you
simply use ="{dynamic logic or assignment of variable value}".

Without looking back at the previous threads to determine the best
approach to determine what the value of attribute name should be you
could do something like this:

<xsl:variable name="attName">
   <xsl:choose>
      <xsl:when test="test to determine value of this boolean">
         ROSE
      </xsl:when>
      <xsl:otherwise>
         NOT A ROSE
      </xsl:otherwise>
   </xsl:choose>
</xsl:variable>

...
<xsl:attribute name="{$attName}">
   <xsl:value-of select="$value"/>
</xsl:attribute>
...

HTH's!

<M:D/>

this is what I've tried so far. Is there another way?

thanks,


Ann Marie


<xsl:param name="attr">ROSE</xsl:param>
<xsl:param name="value">RED</xsl:param>
<xsl:param name="node">orion-web-app</xsl:param>

<xsl:template match="orion-web-app"> <!-- adds attr to orion-web-app node correctly but won't
take vars for attr or match="$node" -->


<xsl:copy>
<xsl:if test="not(//@ROSE)">
<xsl:attribute name="ROSE"><xsl:value-of
select="$value"/></xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:if> </xsl:copy>


</xsl:template>

If i include the attr test in this template:

<xsl:template match="@*" priority="10">
<!--   adds attr to each node in the tree
        it should only be added to the specified node
        and vars are not allowed where ROSE is used -->

<xsl:if test="not(//@ROSE)">
<xsl:attribute name="ROSE"><xsl:value-of
select="$value"/></xsl:attribute>
</xsl:if>-->



to get around the restriction on where attribute names can be used, I tried this:

<xsl:template match="@*" priority="10">
<!--replaces every attr in the result tree w/ RED
recoverable error: Cannot write an attribute node when no
element start tag is open-->
<xsl:choose>
<xsl:when test="not(//@*=$attr)"><xsl:value-of
select="$value"/></xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>

Current Thread