Monday 21 October 2013

Namespace Identity Transformation

A simple identity transformation for switching namespaces

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="oldNS" select="'http://foo/1.0'"/>
 <xsl:param name="newNS" select="'http://bar/1.0'"/>

 <xsl:template match="/">
  <xsl:apply-templates/>
 </xsl:template> 
 
 <xsl:template match="*">
  <xsl:element name="{name()}" namespace="{$newNS}">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:choose>
   <xsl:when test="namespace-uri()=$oldNS">
    <xsl:attribute name="{name()}" namespace="{$newNS}">
     <xsl:value-of select="."/>
    </xsl:attribute>
   </xsl:when>
   <xsl:otherwise>
    <xsl:copy-of select="."/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

</xsl:stylesheet>

No comments:

Post a Comment