Re: [xsl] xsl:sequence

Subject: Re: [xsl] xsl:sequence
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 9 Aug 2006 10:30:24 +0100
> Can anyone tell me when I should be using value-of, when i should be using 
> copy-of and when I should be using sequence?

I'll have a try, although I made one or two errors last time.

> >............................................................., some people 
> >tend to
> >over-use text() it makes the stylesheet rather fragile as it means that
> >source files with comments in often fail
> But how else can you retreive the value of an element when it is text?

use value-of.

In XSLT 1.0

given <foo>a <!-- b -->c</foo>

<xsl:value-of select="foo"/> is a text node "a c" and
 <xsl:value-of select="foo/text()"/> is a text node "a "
which is usually not what you want to happen just because someone added
a comment.
In XSLT2 value-of does produce the value of the whole sequence and
re-merges text nodes, so actually inthi scase in xslt2 
 <xsl:value-of select="foo/text()"/> is text node "a c"
but it's simpler for you and teh system just to go
<xsl:value-of select="foo"/>
Of course, if there are child elements of foo and you only want top
level text  then you may have to use text() to obtain that but that's a
ratherr are requirement. Normally if their is mixed content you need
_all_ the content, the contentthatjust happens to be at the top level
isn't so often interesting.

xsl:value-of always generates a _text node_ so use it when you want
to generate text.

<p><i>This</i> is <b>bold</b> text</p>

Then (in XSLT2)

<xsl:value-of select="p" produces a text node "This is bold text"

<xsl:value-of select="p/text()" produces a text node " is  text"

<xsl:sequence select="p"/> returns the _same_ p node
     <p><i>This</i> is <b>bold</b> text</p>

<xsl:copy-of select="p"/> returns a _new_ p node
     <p><i>This</i> is <b>bold</b> text</p>

xsl:copy-of is there with the semantics it has because of xslt1,
although to be honest I can't think off hand of any case where you would
need to use xsl:copy-of rather than xsl:sequence.


Current Thread