Re: [xsl] how to <xsl:apply-templates /> for ELEMENT nodes only?

Subject: Re: [xsl] how to <xsl:apply-templates /> for ELEMENT nodes only?
From: "James A. Robinson" <jimr@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 30 Mar 2004 06:26:28 -0800
> But in order to stop the text node from being spewed out by default, I 
> have to explicityly surpress it with
> <xsl:template match="//text()" />
> 
> While this works, surely there is a more elegant way. The source XML 
> uses an informal schema designed by someone else which mixes text with 
> elements (document centric schema). I'm halfway through overhauling 
> their XSLT templates and I just want all templates matching any type of 
> element to be applied.

I'm rather new to XSL, so I may be missing something, but I would think
that what you did is *exactly* right -- you override default handling
of text() to display nothing.

An alternative may be to make sure you always apply-templates against
elements instead of something like node() which matches text(), or,
of course, against text() itself. The following is an example which
descends an XML tree and only prints out the elements:

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet
        xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
        xmlns:xalan='http://xml.apache.org/xalan'
        version='1.0'>

        <xsl:output
                encoding='UTF-8'
                method='xml'
                indent='yes'
                xalan:indent-amount='2'/>

        <xsl:template match='/'>
                <xsl:apply-templates select="/*"/>
        </xsl:template>

        <xsl:template match="*">
                <xsl:copy>
                        <xsl:copy-of select="./@*"/>
                        <xsl:apply-templates select="./*"/>
                </xsl:copy>
        </xsl:template>

</xsl:stylesheet>


Jim

Current Thread