I have the following XML document:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Entity Type="defect">
<Fields>
<Field Name="user-28"/>
<Field Name="user-29">
<Value>1</Value>
</Field>
<Field Name="has-change">
<Value></Value>
</Field>
...
I am trying to convert this so that all Field elements become elements with the Name attribute converted to an element, and that it drops the Fields element. So far I have partial success using the following transformation:
<?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="//Entity">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Field">
<xsl:element name="{@Name}">
<xsl:value-of select="Value"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
It results in the following, which is partly right, but it loses the Type attribute on the Entity element:
<?xml version="1.0" encoding="UTF-8"?>
<Entity>
<user-28/>
<user-29>1</user-29>
<has-change/>
An additional complication is that I need this to work on the following document as well, which contains an Entities root node with multiple Entity nodes, without losing the root node:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Entities TotalResults="60">
<Entity Type="defect">
<Fields>
<Field Name="id">
<Value>1161</Value>
</Field>
<xsl:copy>only copies a node itself, none if its attributes or child nodes. Applied on attributes, it copies the complete attribute, i.e. name and value.When doing a transformation that preserves most of the document, it’s a good idea to use an identity transform template. What about this: