Re: [xsl] Use of xsl:apply-templates exception with an element

Subject: Re: [xsl] Use of xsl:apply-templates exception with an element
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 28 Sep 2009 12:16:15 -0400
Hi,

In addition to what's been said, one can also probably say fairly that XSLT experts avoid name testing where possible? Why?

1. It is sometimes a source of obscure errors, for example when it's not namespace-safe and our documents have namespaces.

2. It's inelegant, especially compared to available alternatives.

3. It does what XPath is mostly designed to do without it -- XPath already does name testing in a namespace-safe way, without its being explicit -- and thereby deprives the processor of optimization opportunities.

All that having been said, there are corner cases (such as when names are not known in advance), and explicit testing on names may sometimes be allowed by an expert to be the best available solution on balance. (In my experience.)

<xsl:copy-of select="@*[not(node-name(.) = $new/@*/node-name(.))]"/>

... as here.


On the other hand, even here there may be available alternatives. For example -- since this case is hypothetical -- let's say we do this because we want to add to an element any attributes on the current node that would fail to be accounted for by $new since $new does not have an attribute by that name.

We could do:

<xsl:copy-of select="@*"/>
<xsl:copy-of select="$new/@*"/>

In other words, copy all the attributes, then copy the attributes on $new.

The attributes on $new will replace any attributes of the same name copied from the current node.

Again, no name testing is required.

And again, the example is entirely hypothetical.

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