[xsl] XML to XML Namespace conversion

Subject: [xsl] XML to XML Namespace conversion
From: Casper Voortman <casper.voortman@xxxxxxxxxxxx>
Date: Mon, 8 Dec 2008 13:14:30 +0100
I have a question regarding writing an XSLT that converts XML into an XML that uses namespace prefixes in the element names. I have written some XSLT's and call myself fairly proficient in it, but namespaces and prefixes is completely new to me.

I have a sample XML file that looks something like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Article>
    <MetaData>
        <ID>618</ID>
        <Type>Article</Type>
        <Name>Article 1 Title</Name>
        <State>Release</State>
        <RouteTo>Nobody</RouteTo>
        <PlacedOn>Layout 1</PlacedOn>
        <PlacedOnPage>4-6</PlacedOnPage>
        <Size>116179</Size>
        <LengthWords>2525</LengthWords>
        <LengthChars>15892</LengthChars>
        <LengthLines>287</LengthLines>
        <Modified>2008-11-24T14:03:54</Modified>
        <Publication>My Publication</Publication>
        <Issue>Test Issue</Issue>
        <Section>Articles</Section>
        <PublicationId>3</PublicationId>
        <IssueId>65</IssueId>
        <SectionId>14</SectionId>
        <StateId>38</StateId>
        <LockForOffline>false</LockForOffline>
    </MetaData>
</Article>


This XML structure needs to be converted into XML with prefixes. Something like:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:htm="http://www.w3.org/1999/xhtml";
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
xmlns:dc="http://purl.org/dc/elements/1.1/";
xmlns:pcv="http://prismstandard.org/namespaces/1.2/pcv/";
xmlns:prism="http://prismstandard.org/namespaces/1.2/basic/";>
<rdf:Description>
<dc:identifier>618</dc:identifier>
<dc:filetype>Article</dc:filetype>
<dc:Name>Article 1 Title</dc:Name>
<dc:State>Release</dc:State>
<dc:RouteTo>Nobody</dc:RouteTo>
<prism:PlacedOn>Layout 1</prism:PlacedOn>
<prism:PlacedOnPage>4-6</prism:PlacedOnPage>
<dc:Size>116179</dc:Size>
<dc:LengthWords>2525</dc:LengthWords>
<dc:LengthChars>15892</dc:LengthChars>
<dc:LengthLines>287</dc:LengthLines>
<prism:Modified>2008-11-24T14:03:54</prism:Modified>
<prism:Publication>My Publication</prism:Publication>
<dc:Issue>Test Issue</dc:Issue>
<dc:Section>Articles</dc:Section>
<dc:PublicationId>3</dc:PublicationId>
<dc:IssueId>65</dc:IssueId>
<dc:SectionId>14</dc:SectionId>
<dc:StateId>38</dc:StateId>
<dc:LockForOffline>false</dc:LockForOffline>
</rdf:Description>
</rdf:RDF>



Next to adding the prefixes to the xml, also some elements need to be renamed. I have tried writing an XSLT for it, but i keep having the problem that every element that i define in the XSLT get the xmlns information added to it as an attribute. Also i how can i add al xmlns attribute data to the root element like in the result sample.


The XSLT i tried to write, looks a little like:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    xmlns:pcv="http://prismstandard.org/namespaces/1.2/pcv/";
    xmlns:prism="http://prismstandard.org/namespaces/1.2/basic/";
    xmlns:dc="http://purl.org/dc/elements/1.1/";>

    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- Match 'Article'. -->
    <xsl:template match="Article">
        <!-- Define root element. -->
         <xsl:element name="rdf:RDF">
             <!-- Define description element. -->
             <xsl:element name="rdf:Description">

                 <!-- Define indentifier element. -->
                 <xsl:element name="dc:identifier">

</xsl:element>

                 <!-- Define format element. -->
                 <xsl:element name="dc:format">

</xsl:element>

                 <!-- Define language element. -->
                 <xsl:element name="dc:language">

</xsl:element>

                 <!-- Define page range element. -->
                 <xsl:element name="prism:pageRange">

                 </xsl:element>
             </xsl:element>
         </xsl:element>
    </xsl:template>
</xsl:stylesheet>


Any help would be greatly appreciated.


Regards,

Casper Voortman

Current Thread