xsl-list
[Top] [All Lists]

RE: Omit Data While Using Copy

2004-08-11 14:25:40
Charles:

You asked for some clarification.

At 04:18 PM 8/11/2004, you wrote:
To get to the heart of your problem, when you don't specify a template to use in processing a node, the default template is applied. That built-in template copies the text value of an un-matched node to the output.

This is true only of text nodes, whose default template copies its value to the result. For element nodes, the default template simply applies templates to its children. Accordingly, the entire tree is traversed, with any text nodes copied out, which is why the text value of an unmatched node (providing none of its descendants match a template either) appears in output (whether it appeared in one text node or many in the source).

In this case, the text value of <titel>. By adding a "no-op" template that matches <titel>, the output will be suppressed:

<xsl:template match="titel" />

Right.

The next thing I noticed was that your template matches "kapitel/hanvisning". The <hanvisning> element has no child elements.

Correct. An apply-templates inside it will apply templates to its text node child (the default template if necessary). Note that its attribute is not a child and would not be selected for processing. But since the OP has value-of select="node()", the *value* of that text node is copied out (which is also what would happen by default).

A bug would emerge were there more than one text node inside the hanvisning, since value-of select="node()" will get us the value only of the first child node (the value of a node-set in XPath 1.0 being the value of the first node in the set in document order).

I am a little puzzled as to why the <titel> element is being processed. It is not a child of <hanvisning>. I had always thought that the default template was applied when <xsl:apply-templates /> is used, but it seems that is incorrect. Perhaps one of those better acquainted with the XSL spec can explain it to us.

The <titel> node is being picked up by the (defaulted) traversal of its parent, the <kapitel>, which is being processed (not matching otherwise) by the built-in template for elements, described above.

To clarify utterly (I hope), the default template is not applied when <xsl:apply-templates/> is used, but rather whenever a node is selected for processing (i.e. has templates applied to it) which does not match a template explicitly given. If your only template matches "kapitel/hanvisning", the default template will apply to the kapitel itself, as to the titel (its children are processed and text node descendants eventually copied across) and the "text" elements (which disappear since there's nothing inside them to copy).

Cheers,
Wendell

The source:

<?xml version="1.0"?>
<?xml:stylesheet type='text/xsl' href='manual.xsl'?>
<kapitel>
  <titel>Measures every 10 percussion hours</titel>
  <text/>
  <hanvisning from="ID(SEUHS-SLAGVT-10-MATARE13)">Feeder</hanvisning>
  <text/>
  <hanvisning
from="ID(SEUHS-SLAGVT-10-BOM19)">Boom</hanvisning>
  <text/>
</kapitel>



======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
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
======================================================================



<Prev in Thread] Current Thread [Next in Thread>