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?
XSLT Processor takes the XSLT stylesheet and applies the transformation rules on the target XML document and then it generates a formatted document in the form of XML, HTML, or text format. This formatted document is then utilized by XSLT formatter to generate the actual output which is to be displayed to the end-user.
XSL (eXtensible Stylesheet Language) is a styling language for XML. XSLT stands for XSL Transformations.
The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.
XSLT is designed to be used as part of XSL. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.
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"> <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"> <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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With