Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > using html in variables

Reply
Thread Tools

using html in variables

 
 
David Schwartz
Guest
Posts: n/a
 
      08-05-2008
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" xmlnssl="http://www.w3.org/1999/XSL/
Transform">
<xslutput 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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-05-2008
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/
 
Reply With Quote
 
 
 
 
David Schwartz
Guest
Posts: n/a
 
      08-05-2008
> 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
 
Reply With Quote
 
Stanimir Stamenkov
Guest
Posts: n/a
 
      08-06-2008
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
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      08-06-2008
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Using variables as variables Frank Javascript 4 04-10-2007 04:20 PM
Help with Javascript: naming variables using other variables sagejoshua HTML 16 11-09-2005 12:27 PM
Using explicitly sized variables in functions as auto variables or parameters Adel C Programming 3 03-17-2005 04:36 AM
Using match variables ($1, $2 ...) as variables. Ravi Parimi Perl Misc 16 05-13-2004 07:03 PM



Advertisments