When to use <xsl:text>

Derek Fountain

Usenet Archive
Joined
Jul 17, 2001
Messages
90
Reaction score
0
When is the use of <xsl:text>blah blah</xsl:text> necessary? I'm finding that, although it's used religously in the book I'm learning from, if I miss it out, the output is just the same.
 

Christopher Boomer

Usenet Archive
Joined
Jul 25, 2003
Messages
8
Reaction score
0
Derek Fountain said:
When is the use of <xsl:text>blah blah</xsl:text> necessary? I'm finding that, although it's used religously in the book I'm learning from, if I miss it out, the output is just the same.

I'm no guru but I'd say the element probably comes into play whenever you are using variables rather than actual text. A similar scenario is possible with <xsl:element name="Mine">, which is equivalent to simply entering <Mine>, but because the attriibute is quoted text, you can use a variable instead.

hth
Christopher Boomer
 

Andy Fish

Usenet Archive
Joined
Dec 30, 2002
Messages
605
Reaction score
0
Derek

the main reason you want to use this is when whitespace is significant in the result document. For instance, consider this snippet of xsl

<h1>
details for user
<xsl:value-of select="@username"/>
in group
<xsl:value-of select="@groupname"/>
</h1>

the text node for the phrase 'details for user' starts just after the closing > of <h1> and ends just before the opening < of <xsl:value-of>. All this extra whitespace will be copied into the resulting output. This doesn't normally matter with HTML but you might be using an output format where whitespace is significant or you maybe want the HTML to look nice. in this case you can do this:

<h1>details for user<xsl:value-of select="@username"/>in group<xsl:value-of select="@groupname"/></h1>

but this can get unwieldy (and if you use a pretty printer to reformat your XSL it might break it up again). Here's where xsl:text comes in:

<h1>
<xsl:text>details for user</xsl:text>
<xsl:value-of select="@username"/>
<xsl:text>in group</xsl:text>
<xsl:value-of select="@groupname"/>
</h1>

now you have no whitespace within those text nodes. Instead you have created some new text nodes which just contain the whitespace but fortunately the XSLT processor will ignore those.

Dealing with whitespace in XSL is a pain the butt and you usually don't need to bother. on those occasions where you need to control the whitespace that gets generated, xsl:text is the way to do it

HTH

Andy
 

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

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,798
Members
249,594
Latest member
henrywalsh

Latest Threads

Top