Search code examples
c#xslt-1.0biztalkbiztalk-mapperbiztalk-2016

Inline XSLT 1.0 Count on Repeating Record BizTalk Map


I have a problem in writing a Inline XSLT 1.0 in my BizTalk Project , I am trying to get the count for a field Status if its equal to INactive , below is the Input xml , Expected xml and XSLT what i tried

Input XML :

<ns0:Root xmlns:ns0="http://Test">
    <ns0:Source>EXT</ns0:Source>
    <ns0:Lines>
        <ns0:Code>A</ns0:Code>
        <ns0:Status>Active</ns0:Status>
    </ns0:Lines>
    <ns0:Lines>
        <ns0:Code>A</ns0:Code>
        <ns0:Status>Active</ns0:Status>
    </ns0:Lines>
    <ns0:Lines>
        <ns0:Code>A</ns0:Code>
        <ns0:Status>InActive</ns0:Status>
    </ns0:Lines>
    <ns0:Lines>
        <ns0:Code>A</ns0:Code>
        <ns0:Status>InActive</ns0:Status>
    </ns0:Lines>
        <ns0:Lines>
        <ns0:Code>A</ns0:Code>
        <ns0:Status>InActive</ns0:Status>
    </ns0:Lines>
</ns0:Root>

Expected OutPut :

<ns0:Root xmlns:ns0="http://TestOutPut">
  <Count>3</Count>
</ns0:Root>

Inline XSLT ( Script Functoid ) :

<xsl:element name="Count"><xsl:value-of select = "count(Lines[Status='Inactive'])" /></xsl:element>

Condition : Get the No of Status (Count ) if Status = 'Inactive'

Help me out , don't know where i am doing it wrong


Solution

  • For inline XSLT using the Scripting Functiod you need to have the following where you need to include the local-name as well as using the text() function in the condition.

    <xsl:variable name="count" select="count(/*[local-name()='Root' and namespace-uri()='http://Test']/*[local-name()='Lines' and namespace-uri()='http://Test']/*[local-name()='Status' and namespace-uri()='http://Test'][text()='InActive'])" />
    
    <Count><xsl:copy-of select="$count" /></Count>
    

    You can get the correct XSLT path by clicking on the node in the map and copying the Instance XPath from the Properties window.

    You can remove the namespace-uri to simplify it if there aren't multiple namespaces to cause issues.

    <xsl:variable name="count" select="count(/*[local-name()='Root']/*[local-name()='Lines']/*[local-name()='Status'][text()='InActive'])" />
    
    <Count><xsl:copy-of select="$count" /></Count>
    

    Note: XSLT is case sensitive, so Inactive and InActive are not equal.