Reputation:
I am trying to convert a simple xml to structured xml using xslt.
This is my xml :
<?xml version="1.0" encoding="UTF-8"?>
<foo bar="12" baz="34">ONE</foo>
And this is what I want:
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<a>
<a>
<a>bar</a>
<v>12</v>
</a>
<a>
<a>baz</a>
<v>34</v>
</a>
<v>ONE</v>
</foo>
The “foo” element contains two attributes, “bar” and “baz” and the value of the element “ONE”. In the converted format, the element foo will contain an attributes sequence “a” which will contain a sequence-of attribute “a”.
The attribute sequence-of will contain “a” and “v” element which will be the name and value of the attribute respectively. All the attributes of the foo element are contained inside the attributes sequence. The value of foo is places inside a “v” element after the attribute sequence. I can break attributes to elements but I don't know how to add new element.
Upvotes: 1
Views: 116
Reputation: 117100
I am guessing you want something like:
<?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" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<v><xsl:value-of select="."/></v>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<a>
<a><xsl:value-of select="name()"/></a>
<v><xsl:value-of select="."/></v>
</a>
</xsl:template>
</xsl:stylesheet>
Applied to your input of:
<?xml version="1.0" encoding="UTF-8"?>
<foo bar="12" baz="34">ONE</foo>
the result is:
<?xml version="1.0" encoding="utf-8"?>
<foo>
<a>
<a>bar</a>
<v>12</v>
</a>
<a>
<a>baz</a>
<v>34</v>
</a>
<v>ONE</v>
</foo>
Upvotes: 1