Search code examples
xmlloopsxsltedi

Remove complete node from XML, if the child attribute does not contains specific text `WorkFlow/@name != 'UNS_SMTP_SERVICES'`


In my XSLT I am checking for WorkFlow/@name = 'UNS_SMTP_SERVICES' if that is not true I want to remove the Document and if non of the Document has the value I want to remove the Partner. I could achieve it to some extent but could not get the desired output.

My input:

<?xml version="1.0" encoding="UTF-8"?>
<Partners>
    <Partner name="VACMCLEA">
        <LastUpdate>
            <Status>ACTIVE</Status>
        </LastUpdate>
        <Documents>
            <Document name="SNDMANASN">
                <LastUpdate>
                    <Status>ACTIVE</Status>
                </LastUpdate>
                <BusinessProcess name="">
                    <WorkFlow name="UNS_FILESYSTEM_ARCHIVE">
                        <Parm>ArchiveInfo/FileName=vacmclea_sndmanasn&amp;ArchiveInfo/Dir=/edi/archive/vacmclea</Parm>
                        <LastUpdate>
                            <Status>ACTIVE</Status>
                        </LastUpdate>
                    </WorkFlow>
                </BusinessProcess>
            </Document>
            <Document name="855E">
                <LastUpdate>
                    <Status>ACTIVE</Status>
                </LastUpdate>
                <BusinessProcess name="">
                    <WorkFlow name="UNS_SMTP_SERVICES">
                        <Parm>xport-smtp-mailto=itdi@uco.com,&amp;xport-smtp-mailfrom=IS-PD@uco.com&amp;xport-smtp-mailhost=ma.uco.com&amp;xport-smtp-mailport=25&amp;b2b-raw-message=true&amp;xport-smtp-mailsubject=Needs Attention</Parm>
                        <LastUpdate>
                            <Status>ACTIVE</Status>
                        </LastUpdate>
                    </WorkFlow>
                </BusinessProcess>
            </Document>
        </Documents>
    </Partner>
        <Partner name="ABC">
        <LastUpdate>
            <Status>ACTIVE</Status>
        </LastUpdate>
        <Documents>
            <Document name="SNDMANASN">
                <LastUpdate>
                    <Status>ACTIVE</Status>
                </LastUpdate>
                <BusinessProcess name="">
                    <WorkFlow name="UNS_FILESYSTEM_ARCHIVE">
                        <Parm>ArchiveInfo/FileName=abc_sndmanasn&amp;ArchiveInfo/Dir=/edi/archive/abc</Parm>
                        <LastUpdate>
                            <Status>ACTIVE</Status>
                        </LastUpdate>
                    </WorkFlow>
                </BusinessProcess>
            </Document>
        </Documents>
    </Partner>
        <Partner name="CISCO">
        <LastUpdate>
            <Status>ACTIVE</Status>
        </LastUpdate>
        <Documents>
            <Document name="SNDMANASN">
                <LastUpdate>
                    <Status>ACTIVE</Status>
                </LastUpdate>
                <BusinessProcess name="">
                    <WorkFlow name="UNS_SMTP_SERVICES">
                        <Parm>ArchiveInfo/FileName=cisco_sndmanasn&amp;ArchiveInfo/Dir=/edi/archive/cisco</Parm>
                        <LastUpdate>
                            <Status>ACTIVE</Status>
                        </LastUpdate>
                    </WorkFlow>
                </BusinessProcess>
            </Document>
            <Document name="856">
                <LastUpdate>
                    <Status>ACTIVE</Status>
                </LastUpdate>
                <BusinessProcess name="">
                    <WorkFlow name="UNS_SMTP_SERVICES">
                        <Parm>xport-smtp-mailto=itdi@uco.com,&amp;xport-smtp-mailfrom=IS-PD@uco.com&amp;xport-smtp-mailhost=ma.uco.com&amp;xport-smtp-mailport=25&amp;b2b-raw-message=true&amp;xport-smtp-mailsubject=Needs Attention</Parm>
                        <LastUpdate>
                            <Status>ACTIVE</Status>
                        </LastUpdate>
                    </WorkFlow>
                </BusinessProcess>
            </Document>
        </Documents>
    </Partner>
</Partners>

My XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!-- <xsl:strip-space elements="*"/> -->
    <xsl:output method="xml" indent="yes" media-type="text/xml" omit-xml-declaration="no" encoding="utf-8"/>
    <xsl:param name="lineDelimiter" select="'&#10;'"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Partner[not(.//WorkFlow[not(@name!='UNS_SMTP_SERVICES')])]" />

</xsl:stylesheet>

My output: My XSLT is not removing Document/@name ='SNDMANASN' from Partner/@name = 'VACMCLEA'.

<?xml version="1.0" encoding="utf-8"?>
<Partners>
    <Partner name="VACMCLEA">
        <LastUpdate>
            <Status>ACTIVE</Status>
        </LastUpdate>
        <Documents>
            <Document name="SNDMANASN">
                <LastUpdate>
                    <Status>ACTIVE</Status>
                </LastUpdate>
                <BusinessProcess name="">
                    <WorkFlow name="UNS_FILESYSTEM_ARCHIVE">
                        <Parm>ArchiveInfo/FileName=vacmclea_sndmanasn&amp;ArchiveInfo/Dir=/edi/archive/vacmclea</Parm>
                        <LastUpdate>
                            <Status>ACTIVE</Status>
                        </LastUpdate>
                    </WorkFlow>
                </BusinessProcess>
            </Document>
            <Document name="855E">
                <LastUpdate>
                    <Status>ACTIVE</Status>
                </LastUpdate>
                <BusinessProcess name="">
                    <WorkFlow name="UNS_SMTP_SERVICES">
                        <Parm>xport-smtp-mailto=itdi@uco.com,&amp;xport-smtp-mailfrom=IS-PD@uco.com&amp;xport-smtp-mailhost=ma.uco.com&amp;xport-smtp-mailport=25&amp;b2b-raw-message=true&amp;xport-smtp-mailsubject=Needs Attention</Parm>
                        <LastUpdate>
                            <Status>ACTIVE</Status>
                        </LastUpdate>
                    </WorkFlow>
                </BusinessProcess>
            </Document>
        </Documents>
    </Partner>
    <Partner name="CISCO">
        <LastUpdate>
            <Status>ACTIVE</Status>
        </LastUpdate>
        <Documents>
            <Document name="SNDMANASN">
                <LastUpdate>
                    <Status>ACTIVE</Status>
                </LastUpdate>
                <BusinessProcess name="">
                    <WorkFlow name="UNS_SMTP_SERVICES">
                        <Parm>ArchiveInfo/FileName=cisco_sndmanasn&amp;ArchiveInfo/Dir=/edi/archive/cisco</Parm>
                        <LastUpdate>
                            <Status>ACTIVE</Status>
                        </LastUpdate>
                    </WorkFlow>
                </BusinessProcess>
            </Document>
            <Document name="856">
                <LastUpdate>
                    <Status>ACTIVE</Status>
                </LastUpdate>
                <BusinessProcess name="">
                    <WorkFlow name="UNS_SMTP_SERVICES">
                        <Parm>xport-smtp-mailto=itdi@uco.com,&amp;xport-smtp-mailfrom=IS-PD@uco.com&amp;xport-smtp-mailhost=ma.uco.com&amp;xport-smtp-mailport=25&amp;b2b-raw-message=true&amp;xport-smtp-mailsubject=Needs Attention</Parm>
                        <LastUpdate>
                            <Status>ACTIVE</Status>
                        </LastUpdate>
                    </WorkFlow>
                </BusinessProcess>
            </Document>
        </Documents>
    </Partner>
</Partners>

Solution

  • You can use these two empty templates:

    <xsl:template match="Document[not(BusinessProcess/WorkFlow[@name='UNS_SMTP_SERVICES'])]" />
    <xsl:template match="Partner[not(Documents/Document/BusinessProcess/WorkFlow[@name='UNS_SMTP_SERVICES'])]" />
    

    The first removes the matching Document nodes, and the second the partner ones.