When to use <xsl:text>

D

Derek Fountain

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

Christopher Boomer

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
 
A

Andy Fish

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
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top