Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert XML Attributes To Elements XSLT

Tags:

xml

xslt

I am trying to convert attributes to sub-elements, ie turn the following:

<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.nba.com/warriors/photos/eclipse_logos_</imgurl>
    <res>1024x1024r(iPad/iPhone)?1280x1024r(Regular)?1440x900r(Widescreen)?1920x1080r(HDTV)?1920x1200r(Widescreen)</res>
</WP>

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.nba.com/warriors/photos/eclipse_logos_</imgurl>
    <res>1024x1024r(iPad/iPhone)?1280x1024r(Regular)?1440x900r(Widescreen)?1920x1080r(HDTV)?1920x1200r(Widescreen)</res>
</WP>
like image 730
dmack Avatar asked May 18 '12 00:05

dmack


People also ask

Is there any benefit of converting XML to XSLT?

XSLT is commonly used to convert XML to HTML, but can also be used to transform XML documents that comply with one XML schema into documents that comply with another schema. XSLT can also be used to convert XML data into unrelated formats, like comma-delimited text or formatting languages such as troff.

Can we convert XML to XSL?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

What is text () in XSLT?

The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.


2 Answers

Try this:

<?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>
like image 198
dan radu Avatar answered Oct 20 '22 01:10

dan radu


dradu's code is generic, the transformation will be applicable to all the attributes, below code is more specific about WP element: only those attributes coming under WP element will be converted to elements.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="WP/@*">
    <xsl:element name="{name()}">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
like image 35
InfantPro'Aravind' Avatar answered Oct 20 '22 00:10

InfantPro'Aravind'