Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

xslt remove item if subnode doesn't exist

I have the following input xml.

<response>
<customers>
    <customer>
        <id>001</id>
        <addresses>
            <address>
                <id>a01</id>
                <street/>
            </address>
            <address>
                <id>a02</id>
                <street/>
            </address>
        </addresses>
    </customer>
    <customer>
        <id>002</id>
        <addresses/>
    </customer>
</customers>

If the subelement addresses doesn’t exist or if it’s empty (like this ) I need to remove the father element customer.

<response>
<customers>
    <customer>
        <id>001</id>
        <addresses>
            <address>
                <id>a01</id>
                <street/>
            </address>
            <address>
                <id>a02</id>
                <street/>
            </address>
        </addresses>
    </customer>
</customers>

This is the xslt v 1.0 I use but it doesn’t work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="customer[not(descendant::address[not(*)][normalize-space()])]"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

Can you help me?

>Solution :

If you want to exclude the customer elements that do not have a address descendant:

<xsl:template match="customer[not(descendant::address)]"/>

If you want to exclude the customer elements that do not have an address descendant with some text() value descendants:

<xsl:template match="customer[not(descendant::address[normalize-space()])]"/>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading