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

Time:01-11

I was trying to remove one of the XML element based on attribute value, but I couldnt succeed. I checked all the other posts before asking, but the answer I am trying is almost similar, but not working. Can some one you correct me please?

Data:

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

I am trying with this:

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

Expected Output is:

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

CodePudding user response:

Please try the following XSLT.

The xmlns:jc="urn:com.workday/JournalConnector" namespace was missing.

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>
  •  Tags:  
  • Related