1
votes

I am looking for a help I am writting a XSL to remove empty nodes, it is working but If I have xsi:xsi = true in one of the XML node, then it is not removing that node, I need style sheet which remove empty node, empty attributes and node which contain xsi:xsi = true

INPUT XML

<root>
<para>
 <Description>This is my test description</Description>
 <Test></Test>
 <Test1 attribute="1"/>
 <Received_Date xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</para>
</root>

OutPut XML

<root>
<para>
 <Description>This is my test description</Description> 
 <Test1 attribute="1"/>
 <Received_Date xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</para>

Expected Output

<root>
<para>
 <Description>This is my test description</Description> 
 <Test1 attribute="1"/>
</para>
</root>

XSL Code

<?xml version="1.0" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output omit-xml-declaration="yes" indent="yes" />
    <xsl:template match="node()|SDLT">
        <xsl:if test="count(descendant::text()[string-length(normalize-space(.))>0]|@*)">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
        </xsl:if>

    </xsl:template>
    <xsl:template match="@*">
        <xsl:copy />
    </xsl:template>
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)" />
    </xsl:template>

</xsl:stylesheet>
1
xsi:nil is an attribute, and your stylesheet does not consider an element with an attribute as "empty".michael.hor257k
Also, why do you match for elements named SDLT? This element doesn't seem to exist in any of your input files, and anyway, that would already be covered by matching on any node().Eiríkr Útlendi
Yes I do agree, We dont need SDLT. For me I need to remove element which is having attribute xsi:nil = trueuser2631055

1 Answers

1
votes

You can use this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="1.0">
    <xsl:output omit-xml-declaration="yes" indent="yes" />
    <xsl:template match="node()|SDLT">
        <xsl:if test="(node() or @* ) and not(@xsi:nil = 'true')">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
        </xsl:if>

    </xsl:template>

    <xsl:template match="@*">
        <xsl:copy />
    </xsl:template>

    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)" />
    </xsl:template>

</xsl:stylesheet>