XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
casper VoortmanSubject: XML to XML Namespace conversion
Author: casper Voortman
Date: 08 Dec 2008 03:35 AM
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, since i looked around these forums and the internet but the only examples i can find always seem to deal with the reverse method.

Regards,

Casper Voortman

Posttop
(Deleted User) Subject: XML to XML Namespace conversion
Author: (Deleted User)
Date: 08 Dec 2008 03:05 PM
Hi Casper,
if you want to create an element inside a specific namespace, you need to add to xsl:element the namespace="..." attribute. Or just write it as a literal <rdf:name xmlns:rdf="...">

Alberto

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.