Answers can be XSLT 2.0 or 3.0; Saxon EE parser
Given XML sample:
<theRootNode xmlns="http://myNamespace">
<xml>
<Company>
<TaxIdentity>
<id/>
</TaxIdentity>
<TaxIdentity>
<id>42-123456</id>
</TaxIdentity>
<Name>ABC Company</Name>
<Number>2230</Number>
</Company>
</xml>
</theRootNode>
I have a 2.0 XSLT stylesheet which removes the namespaces and parses the xml node. Within the same transform I now need to remove any 'TaxIdentity' nodes which have empty values within the xml node I just parsed.
Current results are:
<theRootNode>
<xml>
<Company>
<TaxIdentity>
<id/>
</TaxIdentity>
<TaxIdentity>
<id>42-123456</id>
</TaxIdentity>
<Name>ABC Company</Name>
<Number>2230</Number>
</Company>
</xml>
</theRootNode>
Desired results are:
<theRootNode>
<xml>
<Company>
<TaxIdentity>
<id>42-123456</id>
</TaxIdentity>
<Name>ABC Company</Name>
<Number>2230</Number>
</Company>
</xml>
</theRootNode>
My XSLT is below. Note that my thought was to put the initial results into a variable using as="element() or use parse-xml (version 3) but I can't seem remove the empty nodes. Note that my goal is NOT to remove all empty elements or nodes...just ones named TaxIdentiy. What approach should I take? Solutions? I really need it all within the same transform. Thanks.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no" omit-xml-declaration="yes"/>
<!-- Copy elements without copying their namespace declarations -->
<xsl:template match="*[not(local-name() = 'xml')]" name="identity">
<xsl:element name="{name()}" namespace="">
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
<!-- Copy content as is with lower priority -->
<xsl:template match="node() | @*" priority="-2">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- turn xml node content back into xml -->
<xsl:template match="*[local-name() = 'xml']">
<xsl:element name="xml" inherit-namespaces="no" namespace="">
<xsl:value-of select="." disable-output-escaping='yes'/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>