Search code examples
xmlxslt-1.0biztalkbiztalk-2013r2

What wrong with my XSLT to extract a value that correspond to a node value?


I work on a BizTalk mapping and I made a XSLT script which pick up a value from an input which contains two schemas.

The input schema looks like this :

<ns3:Root xmlns:ns3="http://schemas.microsoft.com/BizTalk/2003/aggschema">
    <InputMessagePart_0>
        <ADInput xmlns="http://Export">
            <Employee xmlns="">
                <EmployeeID>456798</EmployeeID>
                <ModifyDate>11/14/2019 8:33:48 PM</ModifyDate>
                <SAMA>SAMA_1</SAMA>
            </Employee>
            <Employee xmlns="">
                <EmployeeID>123456</EmployeeID>
                <ModifyDate>11/18/2019 12:21:16 PM</ModifyDate>
                <SAMA>SAMA_2</SAMA>
            </Employee>
            <Employee xmlns="">
                <EmployeeID>987654</EmployeeID>
                <ModifyDate>11/5/2019 10:54:09 AM</ModifyDate>
                <SAMA>SAMA_3</SAMA>
            </Employee>
        </ADInput>
    </InputMessagePart_0> 
    <InputMessagePart_1>
        <ns0:EmployeeUDM_Response xmlns:ns0="http://Schemas/v2/EmployeeUDM" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <ns0:Header Type="Employee" Source="Biztalk" Date="2019-12-04T17:15:41" />
            <ns0:Return>
                <ns0:Employee>
                    <ns0:Number>123456</ns0:Number>
                    <ns0:SurNameFull>SurNameFull_0</ns0:SurNameFull>
                    <ns0:FirstName>FirstName_0</ns0:FirstName>
                    <ns0:SAMA>SAMA_0</ns0:SAMA>
                </ns0:Employee>
            </ns0:Return>
        </ns0:EmployeeUDM_Response>
    </InputMessagePart_1>
</ns3:Root>

Here is my XSLT in a script functoid:

<xsl:template name="GetSama">
  <xsl:param name="sama" />
  <xsl:param name="number" />

  <xsl:for-each select="//*[local-name()='ADInput']/*[local-name()='Employee']">
    <xsl:if test="*[local-name()='EmployeeID'] = $number">
      <xsl:element name="ns0:SAMA">      
        <xsl:value-of select="$sama" />
      </xsl:element>
    </xsl:if>
  </xsl:for-each>      
</xsl:template>

I would like to have this output:

<ns0:EmployeeUDM_Request xmlns:ns0="http://Securitas/ESB/Schemas/v2/EmployeeUDM">
    <ns0:Header Type="Employee" Source="Securitas.ESB.HR.ADToEmployeeUDM" Date="2019-12-04T14:31:18" />
    <ns0:Write>
        <ns0:Employee>
            <ns0:Number>123456</ns0:Number>
            <ns0:SurNameFull>SurNameFull_0</ns0:SurNameFull>
            <ns0:FirstName>FirstName_0</ns0:FirstName>
            <ns0:SAMAccountName>SAMA_2</ns0:SAMAccountName>
        </ns0:Employee>
    </ns0:Write>
</ns0:EmployeeUDM_Request>

I mean, regarding the value of number node in the InputMessagePart_1, it takes the SAMA node of the employee with the corresponding Number node in the InputMessagePart_0

For now, it always takes the SAMA node of the first employee in the InputMessagePart_0.

<ns0:EmployeeUDM_Request xmlns:ns0="http://Securitas/ESB/Schemas/v2/EmployeeUDM">
        <ns0:Header Type="Employee" Source="Securitas.ESB.HR.ADToEmployeeUDM" Date="2019-12-04T14:31:18" />
        <ns0:Write>
            <ns0:Employee>
                <ns0:Number>123456</ns0:Number>
                <ns0:SurNameFull>SurNameFull_0</ns0:SurNameFull>
                <ns0:FirstName>FirstName_0</ns0:FirstName>
                <ns0:SAMAccountName>SAMA_1</ns0:SAMAccountName>
            </ns0:Employee>
        </ns0:Write>
    </ns0:EmployeeUDM_Request>

I can't find what's wrong in my XSLT.


Solution

  • Finally find a solution: Just modify my xsl:test condition, remove sama parameter (I put the xpath where I need it) and add the string method to it.

      <xsl:for-each select="//*[local-name()='ADInput']/*[local-name()='Employee']">
        <xsl:if test="*[local-name()='EmployeeID' and text() = $number]">
          <xsl:element name="ns0:SAMA">      
            <xsl:value-of select="string(*[local-name()='SAMA'])" />
          </xsl:element>
        </xsl:if>
      </xsl:for-each>      
    </xsl:template>