RE: [xsl] Correcting an XML document

Subject: RE: [xsl] Correcting an XML document
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Fri, 16 Jul 2004 08:50:57 +0100
>Is this neat little trick: 


>in a book somewhere?  

Yes, it's on page 194 of my XSLT Programmer's Reference (and no doubt in
many other places - I didn't invent it!). It's called the "identity
template": not a very good name, because the result is an exact copy of the
original, but has different identity.

It sometimes appears in a different form:

<xsl:template match="node()|@*">
    <xsl:apply-templates select="node()|@*"/>

And could you possibly explain it from the inside out? I had tried
xsl:copy-of, and I had considered xsl:copy, but I never considered putting
them both together.

In the form I gave it, it's a template rule that applies to every element;
it does a shallow copy of the element (i.e., copies the start and end tags),
uses copy-of to copy all the attributes unchanged, and then does
apply-templates to process the children, which will invoke the same template
rule recursively (for each one) unless there is an overriding rule.

The other variant (which appears in the XSLT spec itself) applies-templates
to the attribute nodes as well; the only difference is that you can then
write an overriding template rule for individual attribute nodes.

Michael Kay

Current Thread