Ravi
Ravi

Reputation: 601

Diff between two different XML variables in XSLT

I have a scenario where I have two variables (of different schemas, but hold content relating to the same object) and I need those values from Variable1 that are not there in Variable2

Here,
One Variable holds existing users and the other one holds users from a flat file

Here are the two variables

Users from flat file input:

<ReadUsersResponse>
    <tns:User>
        <tns:Name>aa1</tns:Name>
        <tns:EmailAddress>bb1</tns:EmailAddress>
    </tns:User>
    <tns:User>
        <tns:Name>aa2</tns:Name>
        <tns:EmailAddress>bb2</tns:EmailAddress>
    </tns:User>
</ReadUsersResponse>

Existing Users:

<ReadProjectCodesResponse>
    <ProjectCode>
        <CodeValue>aa3</CodeValue>
        <Description>bb3</Description>
        <ObjectId>1418</ObjectId>
    </ProjectCode>
    <ProjectCode>
        <CodeValue>aa1</CodeValue>
        <Description>bb1</Description>
        <ObjectId>1419</ObjectId>
    </ProjectCode>
</ReadProjectCodesResponse>

If you observe, Name in Variable1 corresponds to CodeValue in Variable2. Similarly, Email in Variable1 to Description in Variable2.

I need to produce a list of the users that do not exist in variable2, meaning

And transform the result into a different format:

<tns:CreateActivityCodes>
    <tns:ActivityCode>
      <tns:CodeTypeObjectId>SomeConstantNumber(1280)</tns:CodeTypeObjectId>
      <tns:CodeValue>aa2</tns:CodeValue>
      <tns:Description>bb2</tns:Description>
    </tns:ActivityCode>
</tns:CreateActivityCodes>

I have been doing some work on this, but could not figure out a solution. Would someone provide me a solution?

Upvotes: 0

Views: 156

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66723

I defined a namespace-uri for the tns namespace prefix and applied it to both the "Variable1" XML and the stylesheet. You will need to adjust it to match your actual namespace.

The following stylesheet assumes that the "Variable2" file was saved as "ReadProjectCodesResponse.xml" and reads it with the document() function in order to compare the "Variable1" and "Variable2" element values.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tns="tns">
    <xsl:output indent="yes"/>

    <xsl:variable name="ProjectCodes"
                  select="document('ReadProjectCodesResponse.xml')/*/*"/>

    <xsl:template match="ReadUsersResponse">
        <tns:CreateActivityCode>
            <xsl:apply-templates select="tns:User"/>
        </tns:CreateActivityCode>
    </xsl:template>

    <xsl:template match="tns:User">
      <xsl:if test="not(tns:Name = $ProjectCodes/CodeValue)">
        <tns:ActivityCode>
          <tns:CodeTypeObjectId>SomeConstantNumber(1280)</tns:CodeTypeObjectId>
             <xsl:apply-templates select="@*|node()"/>
        </tns:ActivityCode>
      </xsl:if>
    </xsl:template>

    <xsl:template match="tns:Name">
        <tns:CodeValue>
            <xsl:apply-templates />
        </tns:CodeValue>
    </xsl:template>

    <xsl:template match="tns:EmailAddress">
        <tns:Description>
            <xsl:apply-templates />
        </tns:Description>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions