[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Newbie issue with apply-templates and output

Subject: Re: Newbie issue with apply-templates and output
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 11 Mar 2005 12:29:09 -0500
apply templates suppress
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.