簡體   English   中英

如何使用 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>

預期的 Output 是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM