[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] seriously trivial question about processing the root element - not at all trivial


Subject: Re: [xsl] seriously trivial question about processing the root element - not at all trivial
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 19 Mar 2003 21:24:26 -0500

At 2003-03-19 17:16 -0500, Robert P. J. Day wrote:
  more than one book i've been reading has presented examples
of processing an XML document whose document element is, say,
<doc>, with:

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

  <xsl:template match="doc">
   ....etc etc ...

  if one assumes that an XML document will have a single
document element anyway, is there any value to the template
that matches "/", if one is going to start processing at
the document root anyway?

Absolutely yes there is value.


  at least in the above example, it seems to be little
more than aesthetics.  is this accurate?

Not at all from my perspective ... I believe much more than aesthetics are involved.


Actually, I counsel the students of my training courses to do the above because it "protects" a stylesheet from abuse.

From XSLT's perspective, *every* stylesheet will work with *every* source file ... where "work" is loosely defined as "produce a well-formed result from the given inputs".

Consider that I have a business card stylesheet that skips over the document element <card> and just relies on the built-in templates to get to the template rules for <name> and <address>.

Should I accidentally or maliciously pass an invoice instance to the business card stylesheet, the built-in template rules might easily find <name> and <address> under <buyer> and nicely format my invoice instance as a business card ... accurate but nonsensical results.

However, with a root template rule that pushes the <card> child, a passed <invoice> instance will produce an empty result: this is not a well-formed instance (though it happens to be a well-formed external parsed general entity) and would appropriately choke the next step of a multi-step transformation as a document entity, or at least would easily be recognized as the result of improper use of the stylesheet.

So far, so good ... but what if I pass a greeting card instance with a <card> document element to the stylesheet? That would also produce nonsensical results. At this point I support my argument to my students that "namespaces are good, not evil" and show the students how:

<xsl:stylesheet xmlns:bc="http://www.....com/ns/businessCard"

  <xsl:template match="/">
     <xsl:apply-templates select="bc:card"/>

  <xsl:template match="bc:card">
     ...

Would be the *best* protection from a business card stylesheet being abused by being passed an unexpected source document that might kick in the built-in rules to produce a non-empty but nonsensical result.

So I personally consider such a practice as a "good" practice. And it supports my namespace preaching. It is not at all a trivial practice from my perspective.

I hope this helps.

.................... Ken


-- Upcoming hands-on in-depth XSLT/XPath and/or XSL-FO North America: June 16-20, 2003

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                      Definitive XSLT and XPath
ISBN 0-13-140374-5                              Definitive XSL-FO
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-10-1              Practical Formatting Using XSL-FO
Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list




Current Thread
Keywords