Tuesday, October 26, 2010

Transforming XML containing namespaces

Whenever I get the task of transforming some XML to some other XML using XSL and namespaces are involved it is always a struggle for some reason. And I've experienced that I'm not the only one having problems with that.

Most examples of XML transformation that I stumble over on the web deals with transforming XML to HTML and almost never use namespaces. Therefore I've decided to write a little article where I describe how to transform from XML to XML including the usage of namespaces.

The task
I want to transform an XML document containing information about CDs from one format into another using XSL. The source XML will use one namespace and the output XML another.

Source XML document
This is the source XML document that I want to transform. Notice that it uses the "http://schemas.maze-dev.blogspot.com/2010/catalog" namespace.

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog xmlns="http://schemas.maze-dev.blogspot.com/2010/catalog">
<name>Absolute Whatever Vol. 1</name>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
XSL transformation document
This is the transformation document. The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" defines the standard 'xsl' transform namespace. However, the interesting part is xmlns="http://schemas.maze-dev.blogspot.com/2010/archive" which defines the output namespace (i.e. namespace in output XML) and xmlns:cat="http://schemas.maze-dev.blogspot.com/2010/catalog" which defines the namespace 'cat' used to reference elements in the input XML. The attribute exclude-result-prefixes="cat" states that the 'cat' namespace should not be included in the output XML.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.maze-dev.blogspot.com/2010/archive"
xmlns:cat="http://schemas.maze-dev.blogspot.com/2010/catalog" exclude-result-prefixes="cat">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
<archive>
<xsl:apply-templates select="cat:catalog"/>
</archive>
</xsl:template>
<xsl:template match="cat:catalog">
<name><xsl:value-of select="cat:name"/></name>
<xsl:apply-templates select="cat:cd"/>
</xsl:template>
<xsl:template match="cat:cd">
<album>
<title>Title: <xsl:value-of select="cat:title"/></title>
<artist>Artist: <xsl:value-of select="cat:artist"/></artist>
</album>
</xsl:template>
</xsl:stylesheet>
Output XML document
This is the output XML document. Notice that it uses the "http://schemas.maze-dev.blogspot.com/2010/archive" namespace that was specified in the XSL document.

<?xml version="1.0" encoding="UTF-8"?>
<archive xmlns="http://schemas.maze-dev.blogspot.com/2010/archive">
<name>Absolute Whatever Vol. 1</name>
<album>
<title>Title: Empire Burlesque</title>
<artist>Artist: Bob Dylan</artist>
</album>
<album>
<title>Title: Hide your heart</title>
<artist>Artist: Bonnie Tyler</artist>
</album>
<album>
<title>Title: Greatest Hits</title>
<artist>Artist: Dolly Parton</artist>
</album>
</archive>
So basically, that's it. Hope somebody finds it useful.

0 comments:

Post a Comment