Search code examples
xmlxsltstylesheet

Convert All XML Attributes to Elements


I have a bunch of xml elements formatted like so:

<WP featured="yes" player="no" dancers="no" series="logos" archive="no" fanart="no" id="eclipse_logos_">
    <seriesName>LOGOS</seriesName>
    <selection>ECLIPSE</selection>
    <imgurl>http://www.example.com/eclipse_logos_</imgurl>
    <res>1024x1024r(iPad/iPhone)?1280x1024r(Regular)?1440x900r(Widescreen)?1920x1080r(HDTV)?1920x1200r(Widescreen)</res>
</WP>

that I want to turn into

<WP>
    <featured>yes</featured>
    <player>no</player>
    <dancers>no</dancers>
    <series>logos</series>
    <archive>no</archive>
    <fanart>no></fanart>
    <id>eclipse_logos_</id>
    <seriesName>LOGOS</seriesName>
    <selection>ECLIPSE</selection>
    <imgurl>http://www.example.com/eclipse_logos_</imgurl>
    <res>1024x1024r(iPad/iPhone)?1280x1024r(Regular)?1440x900r(Widescreen)?1920x1080r(HDTV)?1920x1200r(Widescreen)</res>
</WP>

after some research, it seems that using an xslt stylesheet is the recommended way to go about this conversion:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="@*">
        <xsl:element name="{name()}"><xsl:value-of select="."/></xsl:element>
    </xsl:template>
</xsl:stylesheet>

However I am not very familiar with xsl stylesheets. Could someone walk me through the process of applying the transformation? Whenever I try, I get a blank page. I put the following at the top of my xml doc:

<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="wallpaper.xsl"?>

and name the xsl file accordingly. Am I missing something?


Solution

  • Whenever you want do anything on a computer, you have to find a program that will do it. If you want to apply a stylesheet to an XML document, the program you are looking for is an XSLT processor.

    All modern browsers have an XSLT 1.0 processor built-in, and they will invoke it automatically if they see an <?xml-stylesheet?> processing instruction in the XML document. But that isn't going to help you much, because all they will do with the output is to display it on the screen, and you presumably want it sent to a file.

    There are quite a few XSLT processors around. In nearly all cases they will have a command-line interface. If you're comfortable using the command line on your computer, that's a good way to go. xsltproc and Saxon have been mentioned as possible choices: both are good, but Saxon has the advantage that it supports XSLT 2.0, which you will need sooner or later when you want to do something a bit more complex than this example.

    If you aren't comfortable with a command line, there are GUI applications. If you are going to be doing a lot of XML work, it's worth investing in an XML IDE, of which the most popular are Altova XML Spy, Stylus Studio, and oXygen. I use oXygen. If you want something lighter-weight, there's a GUI front-end to Saxon called "Kernow for Saxon" which you can download from SourceForge.