Re: [xsl] Identity and Modified identity transforms

Subject: Re: [xsl] Identity and Modified identity transforms
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Mon, 3 Dec 2007 12:23:45 +0000
On 03/12/2007, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> > I don't know of the top of my head what node()[1] would do if
> > the currrent node was an attribute...
>
> Attributes don't have children, so child::node()[1] returns an empty
> sequence.

It seems:

<xsl:template match="@*|node()">
      <xsl:copy>
              <xsl:apply-templates select="@*|node()[1]"/>
      </xsl:copy>
      <xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>

...is fine because xsl:copy copies the entire attribute (name and value).

From: http://www.w3.org/TR/xslt20/#element-copy

"When the context item is an attribute node, text node, comment node,
processing instruction node, or namespace node, the xsl:copy
instruction returns a new node that is a copy of the context node. The
new node will have the same node kind, name, and string value as the
context node. In the case of an attribute node, it will also have the
same values for the is-id and is-idrefs  properties. The sequence
constructor, if present, is not evaluated."

So the two apply-templates instructions are redundant when the context
node is an attribute, making the above template compact but perhaps
inefficient.  The extra template in the two-template version could be
shortened ever so slightly to:

<xsl:template match="node()">
      <xsl:copy>
              <xsl:apply-templates select="@*|node()[1]"/>
      </xsl:copy>
      <xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>

<xsl:template match="@*">
      <xsl:copy/>
</xsl:template>



cheers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread