xsl-list
[Top] [All Lists]

RE: Correcting an XML document

2004-07-16 00:50:57
 

Is this neat little trick: 

        <xsl:copy><xsl:copy-of
select="@*"/><xsl:apply-templates/></xsl:copy> 

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:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

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




<Prev in Thread] Current Thread [Next in Thread>