I want to use identity template to transform XML->XML, while excluding certain nodes.
Those nodes would be at different levels of the document – example XML below:
<root>
. <item1>
. <contents>
. <fieldA/>
. ...
. <fieldZ/>
. </contents>
. </item1>
. <item2>
. <field1/>
. ...
. <field9/>
. </item2>
</root>
For example, I would like only to include “fieldC” from “root/item1/contents”
and “field2” from “root/item2”.
My XSLT is below. It doesnt work, I think its because I dont include parent elements of the field I want to include? But I am not sure how can I do that…
<?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:strip-space elements="*"/>
. <xsl:template match="@* | node()">
. <xsl:copy>
. <xsl:apply-templates select="@* | node()" />
. </xsl:copy>
. </xsl:template>
.
. <xsl:template match="fieldC|field2">
. <xsl:element name="{name()}">
. <xsl:value-of select="text()" />
. </xsl:element>
. </xsl:template>
</xsl:stylesheet>
If anyone could help it would be appreciated.
Thanks.
Use:
When this transformation is applied on the following XML document (derived from the provided sketch):
the wanted, correct result (the specified elements deleted), is produced: