如果子属性不包含特定文本`WorkFlow/@name != 'UNS_SMTP_SERVICES'`,则从 XML 中删除完整节点

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

在我的 XSLT 中,我正在检查 WorkFlow/@name = 'UNS_SMTP_SERVICES',如果这不是真的,我想删除 Document,如果 Document 中没有值,我想删除Partner。我可以在一定程度上实现它,但无法获得所需的输出。

我的输入:

<?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>

我的 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>

我的输出:我的 XSLT 没有从 Partner/@name = 'VACMCLEA' 中删除 Document/@name ='SNDMANASN'

<?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>

您可以使用这两个空模板:

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

第一个删除匹配的 Document 个节点,第二个删除 partner 个。