[xsl] Selecting vs matching text() elements and the default template

Subject: [xsl] Selecting vs matching text() elements and the default template
From: "Ross, Douglas" <DRoss@xxxxxxxxxx>
Date: Fri, 11 Mar 2005 15:07:25 -0500
I have learned on this list that matching is almost always better than

So if you find that your script cares about some text() elements and not
others, then you probably do not want to use the ignore text() elements
template because it then forces your script to use a select to get the
desired text() element.

Is there a big difference between select="./text()" and select="." In
the examples below? How does this impact performance and scalability?

	unwanted text
	<element>desired text</element>

<xsl:template match="text()"/>
<xsl:template match="element">
	<xsl:value-of select="./text()"/>


<xsl:template match="element">
	<xsl:apply-templates />
<xsl:template match="element/text()">
	<xsl:value-of select="."/>

Thank you.

Douglas Ross
Developer, HTML UI Framework
E-mail: dross@xxxxxxxxxx

-----Original Message-----
From: Wendell Piez [mailto:wapiez@xxxxxxxxxxxxxxxx]
Sent: Friday, March 11, 2005 12:29 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Newbie issue with apply-templates and output


At 11:21 AM 3/11/2005, you wrote:
>I'm quite new to all this XSL stuff and have now got very confused with
>what's happening with the output using my XSL - it's dumping out the
>text from nodes even tough I've not specified in the XSL to do this.

That's because there are built-in templates that provide for this
automatically. As another post suggested. However, it went on to suggest

you suppress the built-in template for text nodes by saying

<xsl:template match="text()"/>

which I think is almost certainly a bad idea, even though it works on
small example.

If you adopt this approach to coding your XSLT, you'll find it doesn't
scale gracefully, and that you have to write lots of other code to get
to work ... when things could be *much* simpler if you simply used that
built-in default template, and instead changed your template matching
"element" from (I've commented it up)

<xsl:template match="element">
   <temp><xsl:value-of select="."/></temp>
   <!-- creates a 'temp' element containing the current node's value -->
   <!-- traverses to the child nodes and processes them using
        best matching templates (including built-in templates if
necessary) -->

to the simpler:

<xsl:template match="element">

This will have the same effect, and won't have the unfortunate
of forcing you to write "value-of" statements everywhere else in your
stylesheet as it gets more complex.

To suppress that extra text, you want a template that will suppress it
without altering the processing of all the text nodes you do want. So:

<xsl:template match="body/text()"/>

You may still be confused as to why this works and why it's such a good
idea. Research this question by studying the XSLT processing model and
<xsl:apply-templates/> actually does. You'll be glad you did:
this is essential to solving almost any non-trivial problem in XSLT.

Or to put it a little differently: those built-in default templates are
there for a reason, and as an XSLT author you want to know that reason
how to take advantage of it. What you don't want to do is find a way to
"work around" a central feature of the language.


>  The
>code (xml & xsl) is given below with the output - and I'm confused why
>it's output 'heading 1' etc twice, and why 'I'm here' is being output
>all. How can I change my xsl so it only outputs the headings once, and
>the extra text isn't displayed at all?
>It's probably obvious to everyone else - but not to me! :-(
>Any help/explanation much appreciated,
>  --------------------
>Here's the XSL:
><?xml version="1.0"?>
><xsl:stylesheet version="1.0"
><xsl:output method="xml"/>
><xsl:template match="/">
>         <report>
>         <xsl:apply-templates/>
>         </report>
><xsl:template match="element">
>   <temp><xsl:value-of select="."/></temp>
>   <xsl:apply-templates/>
><xsl:template match="body">
>  <xsl:apply-templates/>
>And here is the xml:
><?xml version="1.0" encoding="UTF-8"?>
>         <element>heading 1</element>
>         <element>heading 2</element>
>         <body>
>                 <element>heading 3</element>
>                 <element>heading 4</element>
>                 I'm here
>         </body>
>But the output I'm getting is:
><?xml version="1.0" encoding="UTF-16"?>
>         <temp>heading 1</temp>
>         heading 1
>         <temp>heading 2</temp>
>         heading 2
>         <temp>heading 3</temp>
>         heading 3
>         <temp>heading 4</temp>
>         heading 4
>         I'm here

Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
   Mulberry Technologies: A Consultancy Specializing in SGML and XML

Current Thread