Search code examples
xmlxsltxslt-2.0

In XSL check if filename is not existing in another file is not working?


I have written code to check if Document_Name__c from merge1.xml is not existing in Document_Name__c from merge2.xml then i need all the data in that case only from merge1 as per expected output. XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="XMLMerge2" select="document('merge2.xml')"/>
<xsl:template match="objects">
    <objects>
        <xsl:for-each select="Alert__c">
            <xsl:variable name="Email_from_merge1" select="Document_Name__c"/>
            <xsl:if
                test="
                    exists($XMLMerge2/objects/Data__c[Document_Name__c
                    != $Email_from_merge1])">
                <xsl:copy>
                    <xsl:apply-templates select="@* | node()"/>
                </xsl:copy>
            </xsl:if>
        </xsl:for-each>
    </objects>
</xsl:template>
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

Input: Merge1.xml--- in XSL, I am using from here.

<objects>
    <Alert__c>
        <Document_Name__c>aaa.pdf</Document_Name__c>
        <CreatedDate>2017-06-19T10:55:56.000Z</CreatedDate>
    </Alert__c>
    <Alert__c>
        <Document_Name__c>file 1.pdf</Document_Name__c>
        <CreatedDate>2017-06-15T10:55:56.000Z</CreatedDate>
    </Alert__c>
    <Alert__c>
        <Document_Name__c>VICS_810_004010_US.pdf</Document_Name__c>
        <CreatedDate>2017-06-09T06:24:56.000Z</CreatedDate>
    </Alert__c>    
    <Alert__c>
        <Document_Name__c>aa.csv</Document_Name__c>
        <CreatedDate>2017-06-14T14:26:49.000Z</CreatedDate>
    </Alert__c>   
</objects>

Merge2.xml--- in XSL, I am using from here.

<?xml version="1.0" encoding="UTF-8"?>
<objects>
<Data__c>
<Id>a0J3900000KNEqTEAX</Id>
<Document_Name__c>VICS_810_004010_US.pdf</Document_Name__c>
</Data__c>

<Data__c>
<Id>a0J3900000KNEqVEAX</Id>
<Document_Name__c>file 1.pdf</Document_Name__c>
</Data__c>
</objects>

Expected output:

<objects>
    <Alert__c>
        <Document_Name__c>aaa.pdf</Document_Name__c>
        <CreatedDate>2017-06-19T10:55:56.000Z</CreatedDate>
    </Alert__c>
    <Alert__c>
        <Document_Name__c>aa.csv</Document_Name__c>
        <CreatedDate>2017-06-14T14:26:49.000Z</CreatedDate>
    </Alert__c>
</objects>

Solution

  • I think it's a basic flaw in your logic. You want to test if D2 does not contain an element equal to X, but you are actually testing whether D2 contains an element that is not equal to X. These are different conditions. You want

    test="!exists(X=Y)"
    

    not

    test="exists(X!=Y)"