David Carlisle value-of and copy-of are the same thing on a text node (although it's
normally fairly rare to access text nodes directly, some people tend to
over-use text() it makes the stylesheet rather fragile as it means that
source files with comments in often fail given <foo>onetwo</foo>
<xsl:value-of select="foo"/> and <xsl:value-of select="foo/text()"/> both generate a text node with value "onetwo". But given
<foo>one<!-- hmm -->two</foo>
<xsl:value-of select="foo"/>
generates a text node with value "one two" <xsl:value-of select="foo/text()"/> generates a text node with value
"one two" in 2.0 mode or "one" in 1.0 compatibility mode. So unless you intend your code to do something different in the presence
of comments then it's usually better just to select the element.
> But is the following true: an element node's consists of the name of the
> element; a text node consists of the CDATA assigned to that node; But an
> attribute node consists of the name of an attribute *as well* as its CDATA -
> is this not inconsistent? Nodes have various properties, as well as unique identies (element nodes
can have the same name and content but are not equal (as tested by the
is operator) unless they are "really" the same node.
attribute, text, processing instruction and comment nodes all have
simple content, that is, they have a string valued property (which is
what you mean by CDATA above, although the term CDATA isn't used in XSL
and means something slightly different in XML) element nodes and
document nodes have complex content, so they do not have a string valued
property (their string value is derived from the string value of their
children) they do have other properties, notably properties listing the
child and attribute nodes.
Andrew Welch adds
I think this is a good example:
<root>
<foo>text</foo>
<foo>text</foo>
</root>
<xsl:value-of select="/root"/>
produces "texttext" (with whitespace only nodes stripped)
This is because it's the string value of the <root> element - a
sequence of length one. <xsl:value-of select="/root/foo"/>
produces "text text"
This is because it's the string value of each <foo> element - a
sequence of length two.
As there are two items in the sequence they are concatenated using the
optional separator (the default being " "). |