Nested IF XSLT

Viewing 1 reply thread
  • Author
    Posts
    • #16090

      Hi All,

      I am working on a scenario where i have a custom XSLT for the Map to loop through some field elements and sum them up. what i am looking for is i need to check two or three conditions before i sum up the field element. I am trying to use nested IF, generating id for each field element on the expression and sum up the field element if all are same. how to do that? i haven't done much with XSLT. if anybody have done that please let me know.

      Thanks

      Mani

    • #16097

      You may be able to use the sum function and put the conditions in the xpath

      e.g.. sum(/root/record[@id = '27']/*[local-name()='subtotal'])

      Really need to see what input and output look like and what sort of rules are required.

      • #16098

        Hi Greg,

         what i am exactly looking for is i have an array of messages General Ledgers and it has lot of elements couple of them are glaccount and categorycode. I receive of array of general ledger in general ledgers. if i find same glaccount and categorycode then i need to sum up amount field in the message like

        <GLs><GL><glaccount>10</glaccount><categorycode>PO</categorycode><amount>100</amount></GL>GL><glaccount>10</glaccount><categorycode>PO</categorycode><amount>100</amount></GL>

        <GLs>

         and what i need is if <glaccount> and <categorycode> are same then sum the <amount> node otherwise just leave it. Right now i am doing something like creating a key for <glaccount> and using my if as <xsl:if test=generate-id($glaccountgroup[1]=generate-id()> and it is working now can i use something like this <xsl:if test="generate-id($glaccountgroup[1])=generate-id() and generate-id($categorycode[1])=generate-id()"/>. please let me know.

        Thanks

        -Mani

        • #16099

          The generate-id() function is used to ensure only one node is output.

          In the example below, you generate a key using glaccount. <xsl:key name="glkey" match="/ns0:GLs/GL" use="glaccount)"/>
          Loop thru the GL records  <xsl:for-each select="GL">
          Create a group of all records with the same glaccount value as the current GL. <xsl:variable name="group" select="key('glkey', glaccount))"/>
          If the current GL record is the same as the first record in the group then output a result <xsl:if test="generate-id($group[1]) = generate-id()">

          If you wish to then group items by categorycode you will probably need another key.

           Can you email your two schemas, and an example input and out xml file

           

           

          e.g. <?xml version="1.0" encoding="utf-16"?>
          <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:ns0="http://TestLooping.first" exclude-result-prefixes="msxsl" version="1.0">
          <xsl:output indent="yes" omit-xml-declaration="yes" version="1.0" method="xml" />
          <xsl:key name="glkey" match="/ns0:GLs/GL" use="glaccount)"/>
          <xsl:template match="/">
          <xsl:apply-templates select="/ns0:GLs" />
          </xsl:template>
          <xsl:template match="/ns0:GLS">
           <ns0:GLS>
            <xsl:for-each select="GL">
             <xsl:variable name="group" select="key('glkey', glaccount))"/>
             <xsl:if test="generate-id($group[1]) = generate-id()">
              <GL>
               <totalamount>
                <xsl:value-of select="sum($group/amount)"/>
               </totalamount>
               <number>
                <xsl:value-of select="number" />
               </number>
              </GL>
             </xsl:if>
            </xsl:for-each>
           </ns0:GLS>
          </xsl:template>
          </xsl:stylesheet>

          • #16130

            Hi Greg,

            I did created an another key for the category code and everything is fine but how i have to create the if statement like

            <xsl:if test="generate-id($categorygroup[1])=generate-id() and generate-id($group[1])=generate-id()">

            but it is not giving me the result i want, if i use one by one it is working but i need to put in same if statement with and operator. i mailed my xsl and input instance file.

Viewing 1 reply thread
  • The forum ‘BizTalk 2004 – BizTalk 2010’ is closed to new topics and replies.