TyranVZ
TyranVZ

Reputation: 127

Using XSLT to remove empty/unwanted nodes

I have a XML file with lots of unwanted/unneeded data that I would like to clean up using XSLT

Here is a small snippet of the data with example empty nodes

<?xml version="1.0" encoding="utf-8"?>
<Report xsi:schemaLocation="xxx">
    <Tablix3>
        <Tablix13 Textbox1164="TAX SUMMARY" Textbox10="">
            <table26 textbox155="code">
                <Detail_Collection>
                    <Detail textbox198="" TaxText2="" textbox112="0" textbox114="0" textbox200="0" textbox202="0" textbox874="0" />
                    <Detail textbox198="" TaxText2="" textbox112="0" textbox114="0" textbox200="0" textbox202="0" textbox874="0" />
                    <Detail textbox198="" TaxText2="" textbox112="0" textbox114="0" textbox200="0" textbox202="0" textbox874="0" />
                    <Detail textbox198="" TaxText2="" textbox112="0" textbox114="0" textbox200="0" textbox202="0" textbox874="0" />
                    <Detail textbox198="" TaxText2="" textbox112="0" textbox114="0" textbox200="0" textbox202="0" textbox874="0" />
                    <Detail textbox198="" TaxText2="" textbox112="0" textbox114="0" textbox200="0" textbox202="0" textbox874="0" />
                    <Detail textbox198="" TaxText2="" textbox112="0" textbox114="0" textbox200="0" textbox202="0" textbox874="0" />
                    <Detail textbox198="" TaxText2="" textbox112="0" textbox114="0" textbox200="0" textbox202="0" textbox874="0" />
                    <Detail textbox198="" TaxText2="" textbox112="0" textbox114="0" textbox200="0" textbox202="0" textbox874="0" />
                </Detail_Collection>
            </table26>
            <Tablix26>
                <table24>
                    <Detail_Collection>
                        <Detail textbox208="" textbox209="" textbox210="0" />
                        <Detail textbox208="" textbox209="" textbox210="0" />
                        <Detail textbox208="" textbox209="" textbox210="0" />
                        <Detail textbox208="" textbox209="" textbox210="0" />
                        <Detail textbox208="" textbox209="" textbox210="0" />
                        <Detail textbox208="" textbox209="" textbox210="0" />
                        <Detail textbox208="" textbox209="" textbox210="0" />
                        <Detail textbox208="" textbox209="" textbox210="0" />
                        <Detail textbox208="" textbox209="" textbox210="0" />
                        <Detail textbox208="" textbox209="" textbox210="0" />
                    </Detail_Collection>
                </table24>
            </Tablix26>
        </Tablix13>
    </Tablix3>
</Report>

I would like to delete for example all the empty (with no value or value of 0 in the attributes) detail nodes, and only if all detail lines were empty, also delete the detail collection node.

I would like to keep the tables in place as it occasionally has attribute data that I need.

I have tried using Xpath but having difficulty with my limited knowledge.

Can someone please point me in the right direction?

Thanks

Upvotes: 1

Views: 1993

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

In XSLT, start with an identity template that copies things unchanged:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

Then add empty template rules for the nodes you want to delete.

"I would like to delete for example all the detail nodes"

that would be

<xsl:template match="Detail"/>

In your revised question:

I want to delete all detail lines IF the attributes are empty or 0, e.g. if Detail textbox198=""

that becomes

<xsl:template match="Detail[@*[not(. = '' or . = 0)]]"/>

"and only if all detail lines were empty, also delete the detail collection node."

With the same definition of "empty", that would be

<xsl:template match="Detail_Collection[not(Detail[@*[not(. = '' or . = 0)]])]"/>

Upvotes: 5

Related Questions