using html in variables

D

David Schwartz

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>
said:
<xsl:value-of select="$bar"/></a>
</xsl:template>
</xsl:stylesheet>

Any help on this would be greatly appreciated!

David
 
M

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"/>
 
D

David Schwartz

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
 
S

Stanimir Stamenkov

Tue, 5 Aug 2008 11:03:41 -0700 (PDT), /David Schwartz/:
Tue, 05 Aug 2008 18:29:16 +0200, /Martin Honnen/:


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

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>.
 
M

Martin Honnen

David said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top