简体   繁体   中英

Parse string to XML and xPath into it - XSLT 2 or 3

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM