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

标签 xml xslt

我试图根据属性值删除其中一个 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>

关于xml - 如何在 XML 中使用 XSLT 根据属性值删除 XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70660029/

相关文章:

android - 加载和查询大型 xml 文件 Android

java - 在 xml 文件中使用字符串变量 (android studio)

android - 在水平时间轴中设置文本

c# - .net 4 xslt 转换扩展功能损坏

java - 将 javaRDD 保存为 XML 文件

xml - 使用 XSLT 2.0 将 CSV 转换为 XML

xslt - 如何使用xpath仅选择paren包围的4位数字?

xslt - 使用 XSLT/XPath 查找元素在其父元素中的位置

xml - 计算 XPath 1.0 中的不同内容

xml_grep 获取节中的相关信息