In my previous question I asked how to convert specific attribute to element in simple XML. Now I have more complex input. I need to transform the attribute 'query' into an element. Complex input:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<filter query="select" name="hello" description="world">
<certification>WFA</certification>
<uuid>fd5d9f15-f6d9-4e71-aaf4-024aaaa627f2</uuid>
<parameters>
<parameter type="STRING" name="name" label="name">
<description>Some name</description>
</parameter>
</parameters>
<returned-attributes>
<returned-attribute>id</returned-attribute>
</returned-attributes>
</filter>
My desire output looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<filter name="hello" description="world">
<certification>WFA</certification>
<uuid>fd5d9f15-f6d9-4e71-aaf4-024aaaa627f2</uuid>
<query>select<query/>
<parameters>
<parameter type="STRING" name="name" label="name">
<description>Some name</description>
</parameter>
</parameters>
<returned-attributes>
<returned-attribute>id</returned-attribute>
</returned-attributes>
</filter>
I use the following XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- match the filter element -->
<xsl:template match="filter">
<xsl:choose>
<xsl:when test="/filter/@query">
<!-- output a filter element -->
<xsl:element name="filter">
<!-- add the name attribute, using the source name attribute value -->
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<!-- add the description attribute (if found), using the source name attribute value -->
<xsl:choose>
<xsl:when test="/filter/@description">
<xsl:attribute name="description">
<xsl:value-of select="@description"/>
</xsl:attribute>
</xsl:when>
</xsl:choose>
<!-- add the query as child element, using the source query attribute value -->
<xsl:element name="query">
<xsl:value-of select="@query"/>
</xsl:element>
<!-- add all common elements -->
<xsl:element name="certification">
<xsl:value-of select="certification"/>
</xsl:element>
<xsl:element name="uuid">
<xsl:value-of select="uuid"/>
</xsl:element>
<!-- copy parameters -->
<xsl:copy>
<xsl:apply-templates select="/filter/parameters"/>
</xsl:copy>
<!-- copy attributes -->
<xsl:copy>
<xsl:apply-templates select="/filter/returned-attributes"/>
</xsl:copy>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The transformation works, but it seems to me to complicated. Note, that I use if/else logic because my input can contain "old" (not transformed) and "new" (transformed) XML files.
Please advise. Thanks in advance.
Here is a short and simple solution:
when this transformation is applied on the provided XML document:
the wanted, correct result is produced:
This should do the trick:
A quick tip for you, learn about the identity transform and how it works.