Re: [xsl] Newbie issue with apply-templates and output

Subject: Re: [xsl] Newbie issue with apply-templates and output
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 11 Mar 2005 12:29:09 -0500

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


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,
Here's the XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
<xsl:output method="xml"/>

<xsl:template match="/">

<xsl:template match="element">
  <temp><xsl:value-of select="."/></temp>

<xsl:template match="body">


And here is the xml:

<?xml version="1.0" encoding="UTF-8"?>
        <element>heading 1</element>
        <element>heading 2</element>
                <element>heading 3</element>
                <element>heading 4</element>
                I'm here

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