Skip to content

Instantly share code, notes, and snippets.

@xokomola
Created January 3, 2010 11:40
Show Gist options
  • Save xokomola/96f351e06ab206b1d423 to your computer and use it in GitHub Desktop.
Save xokomola/96f351e06ab206b1d423 to your computer and use it in GitHub Desktop.
XSLT identity transforms
<!-- From http://ajwelch.blogspot.com/2008/01/identity-transform-for-xslt-20.html -->
<xsl:template match="element()">
<xsl:copy>
<xsl:apply-templates select="@*,node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="attribute()|text()|comment()|processing-instruction()">
<xsl:copy/>
</xsl:template>
<!-- identity template without namespace nodes -->
<xsl:template match="*">
<xsl:element name="{ name() }">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*|text()|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[count(node())=0]">
<xsl:element name="{ name() }">
<xsl:apply-templates select="@*"/>
</xsl:element>
</xsl:template>
declare function local:copy($element as element()) {
element {node-name($element)}
{$element/@*,
for $child in $element/node()
return if ($child instance of element())
then local:copy($child)
else $child
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment