I am trying to do some XML data migration, migrating xml documents from one schema to another updated version. The changes are not huge so I am wondering whether there is a easy way for xslt to only transform partially the xml. e.g. rename an element name only etc.
1 Answer
XSLT takes an input document and creates a new output document. As for doing small changes, yes, start your stylesheet with the identity transformation template and add more specific templates for the changes e.g.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- identity transformation template -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- rename foo to bar elements -->
<xsl:template match="foo">
<bar>
<xsl:apply-templates select="@* | node()"/>
</bar>
</xsl:template>
</xsl:stylesheet>
1 Comment
biziclop
If you're using XSLT 2.0 and got lots of rules that involve renaming a single element (and no other changes), you can go one step further and define your mapping in a global variable and write a template that handles all of them.