如何在 XML 中使用 XSLT 根据属性值删除 XML 元素

How to remove the XML element based on attribute value using XSLT in XML

我试图根据属性值删除 XML 元素之一,但未能成功。在提问之前我检查了所有其他帖子,但我尝试的答案几乎相似,但没有用。有谁能指正一下吗?

数据:

<JournalConnectorFileData xmlns="urn:com.workday/JournalConnector"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jc="urn:com.workday/JournalConnector" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    jc:AddOnly="false" jc:CreateJournalwithErrors="true">
    <AccountingJournalData>
        <JournalEntryLineReplacementData>
            <LineOrder>1</LineOrder>
            <LineCompanyReferenceID jc:type="Company_Reference_ID">SS042</LineCompanyReferenceID>
            <CreditAmount>179.62</CreditAmount>
            <Currency>USD</Currency>
            <Memo>Kincentric Balances</Memo>
            <WorktagsReferenceID jc:type="Cost_Center_Reference_ID">CC666</WorktagsReferenceID>
            <WorktagsReferenceID jc:type="Company_Reference_ID">SS023</WorktagsReferenceID>
            <!--I want to remove this element-->
            <ExternalCode jc:name="LedgerAccount_kincentric">2325</ExternalCode>
        </JournalEntryLineReplacementData>
    </AccountingJournalData>
</JournalConnectorFileData>

我正在尝试这样做:

<xsl:stylesheet xmlns="urn:com.workday/JournalConnector"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jc="urn:com.workday/JournalConnector" version="1.0">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="WorktagsReferenceID[@jc:type = 'Company_Reference_ID']"/>
</xsl:stylesheet>

预期输出为:

<JournalConnectorFileData xmlns="urn:com.workday/JournalConnector"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jc="urn:com.workday/JournalConnector" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    jc:AddOnly="false" jc:CreateJournalwithErrors="true">
    <AccountingJournalData>
        <JournalEntryLineReplacementData>
            <LineOrder>1</LineOrder>
            <LineCompanyReferenceID jc:type="Company_Reference_ID">SS042</LineCompanyReferenceID>
            <CreditAmount>179.62</CreditAmount>
            <Currency>USD</Currency>
            <Memo>Kincentric Balances</Memo>
            <WorktagsReferenceID jc:type="Cost_Center_Reference_ID">CC666</WorktagsReferenceID>
            
            <ExternalCode jc:name="LedgerAccount_kincentric">2325</ExternalCode>
        </JournalEntryLineReplacementData>
    </AccountingJournalData>
</JournalConnectorFileData>

请尝试以下 XSLT。

缺少 xmlns:jc="urn:com.workday/JournalConnector" 命名空间。

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:jc="urn:com.workday/JournalConnector">
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="jc:WorktagsReferenceID[@jc:type = 'Company_Reference_ID']"/>
</xsl:stylesheet>