Copying elements between namespaces in XSLT

Monday, 14 Mar 2005

I recently wrote a transform to downgrade Atom draft-05 feeds (which Liferea won’t grok) to Atom 0.3. Between those versions, the namespace changed from http://purl.org/atom/ns# to http://purl.org/atom/ns#draft-ietf-atompub-format-05. There are a few elements which have been renamed, and taking care of them is easy enough:

<xsl:template match="atom05:published">
  <published>
    <xsl:apply-templates select="@*|node()"/>
  </published>
</xsl:template>

But there are a few too many elements which have not changed – other than that their namespace is now different. What to do when you don’t want to write heaps of repetitious rules which instantiate the element matched in the old namespace as the same thing in the new namespace? Actually, after a bit of thought, it turned out that it’s very simple:

<xsl:template match="atom05:*|atom05:@*">
  <xsl:element name="{ local-name( . ) }">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

The curious can look at the whole thing.