Alex A
asked on
XML: writing xslt to transform xmlelement into xmlattribute
Existing XML:
<ROOT>
<Element1>
<Element2>
Value
</Element2>
</Element1>
</ROOT>
Transformed XML should be like this:
<ROOT>
<Element1 Element2="Value">
</Element1>
</ROOT>
Thank you for your help.
<ROOT>
<Element1>
<Element2>
Value
</Element2>
</Element1>
</ROOT>
Transformed XML should be like this:
<ROOT>
<Element1 Element2="Value">
</Element1>
</ROOT>
Thank you for your help.
ASKER
Thank you kmartin7.
Existing XML is actually like this:
<ROOT>
<Element1>
<Element2>
Value
</Element2>
</Element1>
<Element3>Value</Element3>
...
<ElementN>Value</ElementN>
</ROOT>
Element2 will be always the same. All other elements can have different names in different XML files. I need solution to be as generic as possible.
Existing XML is actually like this:
<ROOT>
<Element1>
<Element2>
Value
</Element2>
</Element1>
<Element3>Value</Element3>
...
<ElementN>Value</ElementN>
</ROOT>
Element2 will be always the same. All other elements can have different names in different XML files. I need solution to be as generic as possible.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
so you would like it to iterate to all nodes ?
quasar_ee:
Geert's solution will work for you, regarding your last comment.
kmartin7
Geert's solution will work for you, regarding your last comment.
kmartin7
<?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="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="ROOT">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Element1">
<Element1>
<xsl:attribute name="Element2"><xsl:value
</Element1>
</xsl:template>
</xsl:stylesheet>
You can actually match many elements and use the value of name() or local-name(), and grab the following element's value as an attribute value. Without knowing more about what your specific needs are, the above is a rudimentary but working example.
HTH,
kmartin7