Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading