[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: How to select text only for element with mixed content?


Subject: Re: How to select text only for element with mixed content?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 28 Jun 2000 22:19:22 +0100

>Is there a way of getting only the text for
>the current tag without having to write a
>seperate template?

Sure.  The XPaths that you use in select expressions can pinpoint
particular types of nodes, and one of those types is text, through text().
So, you can select only the text() children of Tag1 in just the same way as
you would select only the Tag2 children of Tag1, and apply templates to them:

<xsl:template match="Tag1">
  <xsl:apply-templates select="text()" />
</xsl:template>

This relies on using the built-in template to give the value of the text.
You can use xsl:value-of if you prefer instead.  One thing to note if you
do that, though, is that all the whitespace between the child elements
counts as text too.  If you do something like:

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

you get the confusing result that you seem to get no output.  In fact, you
are getting output, but you are only getting the value of the first piece
of text, which is whitespace.  If you want to get the value of text that
isn't just whitespace, you can use a predicate to choose only those bits of
text that aren't made up solely of whitespace:

<xsl:value-of select="text()[normalize-space(.) != '']" />

I hope this helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email: jeni.tennison@xxxxxxxxxxxxxxxx


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread