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:
- The Interface / XML structure changes
- Output enriched with rule results in additional nodes
- 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”
Leave a Reply
I have exactly what info I want. Check, please. Wait, it’s free? Awsomee!
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!