Re: [xsl] xsl value-of

Subject: Re: [xsl] xsl value-of
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 3 Apr 2002 10:19:12 +0100
Hi Matias,

> I didn't understand why I have to put only label and not
> label/text()

You don't *have* to use label rather than label/text(), it's just that
it's shorter and means you have to do less travelling up and down the
node tree.

The label elements in your hierarchy look something like:

    +- text: 'some text'

In other words each label element has a text node as its child.

When you use xsl:value-of, you get the "string value" of whatever you
select with the select attribute. So if you do:

  <xsl:value-of select="label/text()" />

you get the string value of the text node under the label element. And
if you do:

  <xsl:value-of select="label" />

you get the string value of the label element.

The definition of the "string value" of an element is that it's the
string value of each of its descendant text nodes, concatenated
together. In other words, the string value of the label element is
exactly the same as the string value of its child text node. So:

  <xsl:value-of select="label/text()" />


  <xsl:value-of select="label" />

Give you *exactly* the same thing.

So there's no reason, in terms of what you get, to use either one or
the other. I usually use just the name of the element, for several

  - it's shorter to type and easier to read
  - it's slightly less work for the processor to only travel down to
    the label element rather than travelling down to its text node
  - if the content of the label element changes in the future, then
    it's likely that you'll still want its string value rather than
    the string value of the first of its text node children

Basically, the only time I'd ever use text() is when you have an
element with mixed content and you purposefully want to extract the
text from that mixed content. For example, if you had:

  <label>                           label
    some string                       +- text: 'some string'
    <dated>2002-04-03</dated>         +- dated
  </label>                                 +- text: '2002-04-03'

and *didn't* want the date, then you have to do label/text() to get
the child text node ('some string') of the label element.



Jeni Tennison

 XSL-List info and archive:

Current Thread