Reputation: 3451
I have following xml file:
<table name="countries">
<column name="id">1</column>
<column name="name">Abkhazia</column>
<column name="code">XZ</column>
<column name="phone_code">+995</column>
<column name="flag">xz.gif</column>
</table>
how can i format this xml to something like this?
<countries>
<id>1</id>
<name>Abkhazia</name>
<code>XZ</code>
<phone_code>+995</phone_code>
<flag>xz.gif</flag>
</countries>
is there some online resources for this?
Upvotes: 0
Views: 157
Reputation: 243549
This short and simple transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<table name="countries">
<column name="id">1</column>
<column name="name">Abkhazia</column>
<column name="code">XZ</column>
<column name="phone_code">+995</column>
<column name="flag">xz.gif</column>
</table>
produces the wanted, correct result:
<countries>
<id>1</id>
<name>Abkhazia</name>
<code>XZ</code>
<phone_code>+995</phone_code>
<flag>xz.gif</flag>
</countries>
Upvotes: 2
Reputation: 579
It is just basic work with xml in any programming language. I've done it in XSLT without programming, transformation which produces desired output (tested here: XSLT Transformation - Online Toolz), mabye there is more elegant way, I'm not expert in XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:template match="table">
<xsl:variable name="tableName" select="@name"></xsl:variable>
<xsl:element name="{$tableName}">
<xsl:for-each select="column">
<xsl:variable name="columnName" select="@name"></xsl:variable>
<xsl:element name="{$columnName}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Upvotes: 3