Re: [xsl] build-in template question

Subject: Re: [xsl] build-in template question
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 04 Sep 2008 18:37:47 -0400
At 2008-09-05 00:17 +0200, Garvin Riensche wrote:
Hello,

I have a question concerning text nodes. If I have an xml file like

<root>
  <e>text</e>
</root>

and a template like

<xsl:template match="e">
  <xsl:copy/>
</xsl:template>

than, the output will be "<e>text</e>".

I don't believe this is true ... you should be getting an empty element. If you are getting text as well, then there is a problem with your XSLT processor.


To prove my first-blush response, I've coded an example below.

Is the text insterted to the output by xsl:copy or by the build-in template

<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>
?

Neither.


Template matching only happens when triggered by <xsl:apply-templates/> either explicitly in the stylesheet or behind the scenes in the built-in template rule for elements and the root node.

In your example you are copying the element node but saying nothing the nodes attached to or below the element node. Therefore, in the result tree, you only get the element node. When this is serialized the element markup will be one of two syntaxes (you don't get control over which): either an empty element tag or a start tag immediately followed by an end tag.

I hope this helps.

. . . . . . . . . Ken

t:\ftemp>type garvin.xml

<root>
  <e>text</e>
</root>

t:\ftemp>type garvin.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:template match="e">
  <xsl:copy/>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>xslt garvin.xml garvin.xsl con
<?xml version="1.0" encoding="utf-8"?>
  <e/>

t:\ftemp>

--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread