How to use xsl variable in attribute value?

Discussion in 'XML' started by Richard, Aug 27, 2004.

  1. Richard

    Richard Guest

    Suppose I create a variable named "var" such as:
    <xsl:variable name="var">VarValue</xsl:variable>
    and then I want to insert its value into an attribute
    <Element attr=" {the value of var} ">
    so that it would generate
    <Element attr=" VarValue ">

    What syntax do I use to do this? If I specify
    <Element attr=" <xsl:value-of select='$var'/> ">
    then the parser complains that &lt; &gt; must be used for
    brackets inside attribute values. However, if I change it to
    <Element attr=" &lt;xsl:value-of select='$var'/&gt; ">
    then it generates exactly that string with no variable
    substitution. I tried using quotes and brackets in various
    ways with no success. Is there a way to insert a variable's
    value into an attribute?
     
    Richard, Aug 27, 2004
    #1
    1. Advertising

  2. Richard

    Richard Guest

    Never mind. I found that I can do the following:
    <xsl:variable name="var">VarValue</xsl:variable>
    <Element>
    <xsl:attribute name="attr"><xsl:value-of select="$var" /></xsl:attribute>
    </Element>


    > Suppose I create a variable named "var" such as:
    > <xsl:variable name="var">VarValue</xsl:variable>
    > and then I want to insert its value into an attribute
    > <Element attr=" {the value of var} ">
    > so that it would generate
    > <Element attr=" VarValue ">
    >
    > What syntax do I use to do this? If I specify
    > <Element attr=" <xsl:value-of select='$var'/> ">
    > then the parser complains that &lt; &gt; must be used for
    > brackets inside attribute values. However, if I change it to
    > <Element attr=" &lt;xsl:value-of select='$var'/&gt; ">
    > then it generates exactly that string with no variable
    > substitution. I tried using quotes and brackets in various
    > ways with no success. Is there a way to insert a variable's
    > value into an attribute?
     
    Richard, Aug 28, 2004
    #2
    1. Advertising

  3. Richard wrote:
    > Never mind. I found that I can do the following:
    > <xsl:variable name="var">VarValue</xsl:variable>
    > <Element>
    > <xsl:attribute name="attr"><xsl:value-of select="$var" /></xsl:attribute>
    > </Element>
    >


    <Element attr="{$var}"/>

    should work, too.

    HTH,
    Gerald
     
    Gerald Aichholzer, Aug 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MC
    Replies:
    1
    Views:
    7,342
    David Carlisle
    Mar 24, 2005
  2. Replies:
    1
    Views:
    3,685
    A. Bolmarcich
    May 27, 2005
  3. schaf
    Replies:
    3
    Views:
    1,340
    schaf
    Sep 16, 2005
  4. Kourosh
    Replies:
    6
    Views:
    872
    Peter Flynn
    May 17, 2006
  5. Replies:
    1
    Views:
    4,415
    Joe Kesselman
    Aug 2, 2006
Loading...

Share This Page