Re: [xsl] Apply-templates - how to omit top level element tags?

Subject: Re: [xsl] Apply-templates - how to omit top level element tags?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 08 Sep 2005 18:43:54 -0400
Mike,

At 06:06 PM 9/8/2005, David wrote:

                                <h1>
                                        <!-- THIS IS WHAT I NEEDED -->
                                        <xsl:apply-templates
select="Name/node()"/>
                                </h1>


That works but makes things look a lot more complicated than need be. You probably just want to replace all that by <xsl:apply-templates select="Name"/>

and have
<xsl:template match="Guide/Name">
 <h1><xsl:apply-templates/></h1>
</xsl:template>

That's probably a more natural xslt coding style.

... and it would implicitly override your identity template (the one matching "@*|node()", which would get rid of that problem -- so your stylesheet would "just work" had you had this template.


OTOH, you couldn't have known this. Another vital detail in the XSLT processing model: how the processor decides among templates when more than one template (oops: "template rule"; see below) matches a given node. In this case, a template that matches "node()" (any node) and one that matches "Guide/Name" (a name element child of a Guide element) both would match your Name. The processor sees this and chooses the latter as a "better match". (The reason: the rules for template matching assign it a priority of 1, while the template matching "node()" has a priority of -0.5....)

BTW, a "template rule" is that construct we generally call a "template". No one knows this or bothers to observe it, but "templates" are the *content* of template rules ... templates also appear inside xsl:for-each instructions.

I'm a secularist too, but I'm willing to be just as persnickety as any religionist if it helps get the job done. Not persnickety for its own sake, mind you. :-)

Cheers,
Wendell


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