using html in variables

Discussion in 'XML' started by David Schwartz, Aug 5, 2008.

  1. So, I'm having a bit of a time trying to insert an <img> tag into an
    <a> tag using an xsl variable. Consider the following template. The
    contents of the foo variable doesn't show up in the anchor. When I use
    named entities instead of < and >, it gets inserted just fine although
    then it's not interpreted by the browser but, rather, simply included
    as is in the page content.

    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
    Transform">
    <xsl:eek:utput method="html" version="4.0" />
    <xsl:template match="foo">
    <xsl:variable name="bar">
    <xsl:if test="$some-boolean-variable">
    <img src="http://some-image.gif" border="0" width="7" height="7"/>
    </xsl:if>
    </xsl:variable>
    <a href="some-url.html" title="{title}"><xsl:value-of select="title"/
    ><xsl:value-of select="$bar"/></a>

    </xsl:template>
    </xsl:stylesheet>

    Any help on this would be greatly appreciated!

    David
    David Schwartz, Aug 5, 2008
    #1
    1. Advertising

  2. David Schwartz wrote:

    >> <xsl:value-of select="$bar"/></a>


    If your variable contains a result tree fragment then you need to use
    xsl:copy-of instead of xsl:value-of e.g.
    <xsl:copy-of select="$bar"/>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Aug 5, 2008
    #2
    1. Advertising

  3. > If your variable contains a result tree fragment then you need to use
    > xsl:copy-of instead of xsl:value-of e.g.
    > <xsl:copy-of select="$bar"/>


    That was it! Can you explain what the issue was a bit more fully?

    Thanks so much,
    David
    David Schwartz, Aug 5, 2008
    #3
  4. Tue, 5 Aug 2008 11:03:41 -0700 (PDT), /David Schwartz/:
    > Tue, 05 Aug 2008 18:29:16 +0200, /Martin Honnen/:
    >
    >> If your variable contains a result tree fragment then you need to use
    >> xsl:copy-of instead of xsl:value-of e.g.
    >> <xsl:copy-of select="$bar"/>

    >
    > That was it! Can you explain what the issue was a bit more fully?


    <http://www.w3.org/TR/xslt#value-of>:

    > The xsl:value-of element is instantiated to create a text node in
    > the result tree. The required select attribute is an expression;
    > this expression is evaluated and the resulting object is converted
    > to a string as if by a call to the string function. [...]
    >
    > The xsl:copy-of element can be used to copy a node-set over to the
    > result tree without converting it to a string. See [11.3 Using
    > Values of Variables and Parameters with xsl:copy-of].


    You could read about the 'string' function
    <http://www.w3.org/TR/xpath#function-string> and what is the
    string-value of root (and element) nodes
    <http://www.w3.org/TR/xpath#dt-string-value> (a bit down below).
    Such node is the one in your variable result-tree-fragment value
    <http://www.w3.org/TR/xslt#dt-result-tree-fragment>.

    --
    Stanimir
    Stanimir Stamenkov, Aug 6, 2008
    #4
  5. David Schwartz wrote:
    >> If your variable contains a result tree fragment then you need to use
    >> xsl:copy-of instead of xsl:value-of e.g.
    >> <xsl:copy-of select="$bar"/>

    >
    > That was it! Can you explain what the issue was a bit more fully?


    Well xsl:value-of takes the string value and inserts a text node in the
    result tree. xsl:copy-of however copies the nodes into the result tree.

    Your variable contains nodes and you want to copy them to the
    transformation result hence you need to use xsl:copy-of.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Aug 6, 2008
    #5
    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. sagejoshua
    Replies:
    16
    Views:
    647
    Michael Winter
    Nov 9, 2005
  2. Adel
    Replies:
    3
    Views:
    305
    Jack Klein
    Mar 17, 2005
  3. Replies:
    9
    Views:
    927
  4. Ravi Parimi
    Replies:
    16
    Views:
    199
    Thomas Kratz
    May 13, 2004
  5. Frank

    Using variables as variables

    Frank, Apr 9, 2007, in forum: Javascript
    Replies:
    4
    Views:
    129
    Dr J R Stockton
    Apr 10, 2007
Loading...

Share This Page