0

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

  • Variable1 - Variable2 (ie. only aa2 as it is not existing)

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?

2
  • See p2p.wrox.com/xslt/… Commented May 27, 2013 at 11:07
  • Where would the value of CodeTypeObjectId come from? The first file doesn't have an ObjectId. Commented May 27, 2013 at 11:21

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I think you need to add a "not()" to the test in xsl:if, because: "I need to produce a list of the users that do not exist in variable2"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.