tags:

views:

456

answers:

3

Hi -

I am looking for a XSL snippet that simply returns the XML unaltered. It sounds trivial but I can't seem to find an example anywhere on the web. Any help out there?

A: 

How about:

<xsl:template match ="/">
  <xsl:copy-of select="."/>
</xsl:template>

Maybe there's an even simpler way ?

krosenvold
First line should be <xsl:template match="/">, no?
Alabaster Codify
Oh, and third line should be </xsl:template>
Alabaster Codify
-1 this answer is incorrect.
cletus
Incorrect how? Please explain, instead of just rating it down. The rest of us might learn something. :)
jalf
@jamesbrady thanks for syntax corrections. I'd really also like to know if there is something other wrong with this?
krosenvold
I still believe this is the correct answer to the question *as asked*. Unless cletus and the bunch can explain why not, I believe they are answering a different question - maybe the one they wanted OP to ask ?
krosenvold
See my answer as to why.
cletus
No you're not saying while copy-of is wrong. You're saying that identity tranform is even better.
krosenvold
Yes, the current solution seems correct to me. (Although Dimitre Novatchev's answer is much better.)
bortzmeyer
+6  A: 

This is a common problem and the answer is called an identity transform. The following template will copy the existing XML correctly. You then add extra templates to modify the behaviour (eg removing certain elements, renaming elements or attributes, etc).

<xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>
cletus
+11  A: 

In order to copy the complete XML document, it is necessary to have a template that matches the root. This might be:

     <xsl:template match="/">

or

     <xsl:template match="node()">

Then a single copying of the current node (the root node) is just sufficient:

     <xsl:copy-of select="."/>

So, one such complete transformation is:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="/">
      <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

Although this is probably the simplest such transformation, XSLT programmers use another one, widely known as the identity transformation or the identity rule:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

The reson the identity transformation is considered to be one the most fundamental XSLT design patterns and to be so massively used, is that by overriding this template rule with other, more specific templates, one can very easily perform a variety of operations that otherwise will be difficult. Examples are deleting a particular (set of) element(s) that have a specific name or satisfy some other condition, renaming particular elements, changing the namespace of particular elements, creating new children or siblings of particular elements, ..., etc.

For more information and code snippets using the identity transformation, do look here.

Dimitre Novatchev
Perfect explanation!
Eduardo
Unfortunately, this is wrong. There is no way to leave the XML document unaltered, as the XSLT has no access to the doctype declaration; you can replicate the actual XML data, but you can't claim it's unaltered, as the output of the transform will not preserve the doctype declaration.
Flynn1179
@Flynn1179: XSLT doesn't operate on the lexical level. It operates on a *tree*. That is, after the string representation of the XML has been parsed. There are a number of differences at the lexical level that may appear in the result document. *All of these are known* and nobody pretends that the result document will be identical to the source document char by char. What XSLT guarantees is that the result document and the wanted document will be identical *as trees*.
Dimitre Novatchev
I'm not disputing that, but my point is, the result of any XSLT will NOT output a document that is identical to the original XML. Regardless of how it's represented, a doctype representation is part of the XML, and cannot be preserved in transformed output; Of course you can leave the tree unaltered, but XML is more than just a tree.
Flynn1179
@Flynn1179: Nobody is arguing this. This is stated in the XSLT Specification(s).
Dimitre Novatchev