The Identity Template – XSLT 2.0 on steroids

Identity templates are arguably one of the most important things you should know about XSLT. Identity templates are really useful if you want to change only a part of the XML input data. The idea is not to create a explicit node copy of each item. The output of the identity transform is the same as the input,  except for CDATA elements and attribute orders. Additional templates are created to define the exceptions for the matched elements which should be modified. This is mandatory if:

  1. The Interface / XML structure changes
  2. Output enriched with rule results in additional nodes
  3. Non-redundant template code is a must-have

Example of a XSLT2 Identity Transform template:


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|*|processing-instruction()|comment()">
<xsl:copy>
<xsl:apply-templates
select="*|@*|text()|processing-instruction()|comment()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

From now on, other templates can match nodes which should be modified. For example we wish to remove all nodes with the name “Bill”:


<xsl:template match="Bill"/>

Because the template is empty, the node is removed in the output.

If multiple transformations are applied, XML pipelines are better (XProc, …). Projects under active development include QuiXProc and Tabular.

comments

2 Responses to “The Identity Template – XSLT 2.0 on steroids”

  1. Debrah on November 13th, 2011

    I have exactly what info I want. Check, please. Wait, it’s free? Awsomee!

  2. Hote obs on December 11th, 2011

    The recommendations you discussed here are really useful. It absolutely was such a fun surprise to have that awaiting me after i woke up to find this. Thank you very much for the tip!

Leave a Reply