using html in variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Schwartz

    using html in variables

    So, I'm having a bit of a time trying to insert an <imgtag into an
    <atag 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 "&#160;"]>
    <xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/
    Transform">
    <xsl:output 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
  • Martin Honnen

    #2
    Re: using html in variables

    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

    Comment

    • David Schwartz

      #3
      Re: using html in variables

      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

      Comment

      • Stanimir Stamenkov

        #4
        Re: using html in variables

        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-stringand 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

        Comment

        • Martin Honnen

          #5
          Re: using html in variables

          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

          Comment

          Working...