Search code examples
xmlxslt-1.0biztalkbiztalk-2013

XSLT - effective way to check if field in repeating record contains another fields value?


Im working on a project in BizTalk where I am receiving HR-data that is expected to be converted into another format before it is routed to its destination.

Here is an example of what I am receiving:

<ns0:Employees xmlns:ns0="http://XXX">
  <Employee>
    <EMPLOYEE_ID>1</EMPLOYEE_ID>
    <BOSS_ID>2</BOSS_ID>
  </Employee>
  <Employee>
    <EMPLOYEE_ID>2</EMPLOYEE_ID>
    <BOSS_ID>3</BOSS_ID>
  </Employee>
  <Employee>
    <EMPLOYEE_ID>3</EMPLOYEE_ID>
    <BOSS_ID>4</BOSS_ID>
  </Employee>
  <Employee>
    <EMPLOYEE_ID>4</EMPLOYEE_ID>
    <BOSS_ID>0</BOSS_ID>
  </Employee>
</ns0:Employees>

And here is an example of what it has to look like (based on above source):

<SyncCXPERSON>
  <CXPERSONSet>
    <PERSON>
        <PERSONID>1</PERSONID>
        <SUPERVISOR>2</SUPERVISOR>
        <BOSS>false</BOSS>
    </PERSON>
    <PERSON>
        <PERSONID>2</PERSONID>
        <SUPERVISOR>3</SUPERVISOR>
        <BOSS>true</BOSS>
    </PERSON>
    <PERSON>
        <PERSONID>3</PERSONID>
        <SUPERVISOR>4</SUPERVISOR>
        <BOSS>true</BOSS>
    </PERSON>
    <PERSON>
        <PERSONID>4</PERSONID>
        <SUPERVISOR></SUPERVISOR>
        <BOSS>true</BOSS>
    </PERSON>
  </CXPERSONSet>
</SyncCXPERSON>

As you can see I have to check the entire list of repeating record 'Employee' to check if the Employee is also someones boss, in which case I set the BOSS-field to true. This is done by below XSLT:

<xsl:template match="Employees">
<SyncCXPERSON>
  <CXPERSONSet>
    <xsl:apply-templates select="Employee" />
  </CXPERSONSet>
</SyncCXPERSON>
</xsl:template>

<xsl:template match="Employee">
 <xsl:variable name="CURRENT_EMPLOYEE_ID" select="EMPLOYEE_ID" />
  <PERSON>
    <PERSONID>
      <xsl:value-of select="$CURRENT_EMPLOYEE_ID"/>
    </PERSONID>
    <SUPERVISOR>
      <xsl:value-of select="BOSS_ID"/>
    </SUPERVISOR>
    <BOSS>
      <xsl:choose>
        <xsl:when test="/Employees/Employee/BOSS_ID = $CURRENT_EMPLOYEE_ID">
          <xsl:value-of select="true"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="false"/>
        </xsl:otherwise>
      </xsl:choose>
    </BOSS>
  </PERSON>
</xsl:template>

This is working. Problem is, I am processing about 35600 posts (message size 3 MB) and it is taking BizTalk about 2 hours to process this XSLT.

So my question is: Am I doing it wrong? Is there a more effective way to do this?


Solution

  • Try using a key, for example:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:key name="emp-by-boss" match="Employee" use="BOSS_ID" />
    
    <xsl:template match="/*">
        <SyncCXPERSON>
            <CXPERSONSet>
                <xsl:apply-templates select="Employee" />
            </CXPERSONSet>
        </SyncCXPERSON>
    </xsl:template>
    
    <xsl:template match="Employee">
        <PERSON>
            <PERSONID>
                <xsl:value-of select="EMPLOYEE_ID"/>
            </PERSONID>
            <SUPERVISOR>
                <xsl:value-of select="BOSS_ID"/>
            </SUPERVISOR>
            <BOSS>
                <xsl:value-of select="boolean(key('emp-by-boss', EMPLOYEE_ID))"/>
            </BOSS>
        </PERSON>
    </xsl:template>
    
    </xsl:stylesheet>