0

Answers can be XSLT 2.0 or 3.0; Saxon EE parser

Given XML sample:

<theRootNode xmlns="http://myNamespace">
    <xml>
        &lt;Company&gt;
            &lt;TaxIdentity&gt;
                &lt;id/&gt;
            &lt;/TaxIdentity&gt;
            &lt;TaxIdentity&gt;
                &lt;id&gt;42-123456&lt;/id&gt;
            &lt;/TaxIdentity&gt;
            &lt;Name&gt;ABC Company&lt;/Name&gt;
            &lt;Number&gt;2230&lt;/Number&gt;
        &lt;/Company&gt;
    </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>

1 Answer 1

1

Here is my XSLT 3.0 suggestion which simply parses the XML with parse-xml and then apply-templates to the parsed nodes with a template removing the TaxIdentity with all empty child elements:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:is="http://myNamespace"
    exclude-result-prefixes="is xs math"
    version="3.0">

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="is:*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* , node()"></xsl:apply-templates>
        </xsl:element>
    </xsl:template>

    <xsl:template match="is:xml">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="parse-xml(.)/node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="TaxIdentity[not(*[normalize-space()])]"/>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! It works, I'm still deciphering the techniques.
<xsl:mode on-no-match="shallow-copy"/> replaces in XSLT 3.0 your second template. And the empty <xsl:template match="TaxIdentity[not(*[normalize-space()])]"/> is then the standard way to avoid copying nodes you don't want to be copied.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.