[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
selecting.

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?

Example:
<doc>
	unwanted text
	<element>desired text</element>
</doc>

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

vs.

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

Thank you.

Douglas Ross
Developer, HTML UI Framework
Kronos
E-mail: dross@xxxxxxxxxx
www.kronos.com

-----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

Hi,

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
your
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
it
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 -->
   <xsl:apply-templates/>
   <!-- traverses to the child nodes and processes them using
        best matching templates (including built-in templates if
necessary) -->
</xsl:template>

to the simpler:

<xsl:template match="element">
   <temp>
     <xsl:apply-templates/>
   </temp>
</xsl:template>

This will have the same effect, and won't have the unfortunate
consequence
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
what
<xsl:apply-templates/> actually does. You'll be glad you did:
understanding
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
and
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.

Cheers,
Wendell

>  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
at
>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,
>Alex
>  --------------------
>Here's the XSL:
>
><?xml version="1.0"?>
><xsl:stylesheet version="1.0"
>xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
><xsl:output method="xml"/>
>
><xsl:template match="/">
>         <report>
>         <xsl:apply-templates/>
>         </report>
></xsl:template>
>
><xsl:template match="element">
>   <temp><xsl:value-of select="."/></temp>
>   <xsl:apply-templates/>
></xsl:template>
>
><xsl:template match="body">
>  <xsl:apply-templates/>
></xsl:template>
>
></xsl:stylesheet>
>
>And here is the xml:
>
><?xml version="1.0" encoding="UTF-8"?>
><html>
>         <element>heading 1</element>
>         <element>heading 2</element>
>         <body>
>                 <element>heading 3</element>
>                 <element>heading 4</element>
>                 I'm here
>         </body>
></html>
>
>But the output I'm getting is:
>
><?xml version="1.0" encoding="UTF-16"?>
><report>
>         <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
></report>


======================================================================
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