none
XSLT Variable increment

    Question

  • I want to assign a variable in start "0" before to start processing of L_Transaction and as control goes to <L_Transaction> want to inceremnt
    the value of that variable need to be get increase ,up to the number of times the For-Each loop executes for <L_Transaction>.
    and finally want to assign that variable in <BatchTrailer>--><Factors>
    Below is my XSLT structure.

    <MessageData>
    <For-each...>
        <L_Transaction>

     INCREMENT VARIABLE

        </L_Transaction>

    <For-each...>

    </MessageData>

     <BatchTrailer>

     <Factors>
                <xsl:value-of select="$INCREMENTED VARIABLE" />
            </Factors>  

       </BatchTrailer>

    Kindly suggest how implement above scenario in xslt.

    Thanks

    Tuesday, September 11, 2012 9:35 AM

Answers

  • Prashant,

    The point is that, your are checking a condition in the "for-each". The condition that you match will be the same for your Factors also.

    And you dont have to use a variable, just take the count using "value-of ".

    Sample Input:

    <Input>
    <Patient>
    <Id>1</Id>
    </Patient>
    <Patient>
    <Id>2</Id>
    </Patient>
    <Patient>
    <Id>3</Id>
    </Patient>
    </Input>

    XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
    <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:template match="/"	>
    	<MessageData>
    		<xsl:for-each select="/Input/Patient">
    			<L_Transaction>
    				<PatientNumber>
    					<xsl:value-of select="./Id"/>
    				</PatientNumber>
    			</L_Transaction>
    		</xsl:for-each>
    	</MessageData>
    	<BatchTrailer>
    	<Factors><xsl:value-of select="count(/Input/Patient)"/></Factors>	
    	</BatchTrailer>
    	</xsl:template>
    </xsl:stylesheet>
    

    Output:

    <?xml version="1.0" encoding="UTF-8"?>
    <MessageData>
    	<L_Transaction>
    		<PatientNumber>1</PatientNumber>
    	</L_Transaction>
    	<L_Transaction>
    		<PatientNumber>2</PatientNumber>
    	</L_Transaction>
    	<L_Transaction>
    		<PatientNumber>3</PatientNumber>
    	</L_Transaction>
    </MessageData>
    <BatchTrailer>
    	<Factors>3</Factors>
    </BatchTrailer>

    Lets us know if your expecting something else. Thanks.

    Sriram N

    Please mark as answer if it helps.


    Sriram N http://srirambiztalks.wordpress.com/

    Wednesday, September 12, 2012 7:40 AM

All replies

  • Hey Prashanth,

    You cannot increment a variable in xslt. you need to use xslt templates to achieve it. give a sample input and expected output. Thanks.


    Sriram N http://srirambiztalks.wordpress.com/

    Tuesday, September 11, 2012 10:22 AM
  • Add a cumulative sum functiod to a map and then look at the script code generated in the map, by right clicking on the map file and validate, then in the output window ctrl-click on the xsl file.

    They use scripting to sum a node.


    Bill Chesnut | BizTalk Server MVP | Mexia Consulting | Melbourne Australia
    http://www.biztalkbill.com
    Please indicate "Mark as Answer" if this post has answered the question.

    Tuesday, September 11, 2012 11:02 AM
  • I might be missing something, but don't you simply need to count the number of patients?

    You can do this simply by using the XPath count() function.

    For example, if your loop is

    <xsl:for-each select="/abc/Patients/Patient">

    Then your Factors node will be:

    <Factors><xsl:value-of select="count(/abc/Patients/Patient)" /></Factors>

    And if you need to use the current iteration number inside the loop, you can do so using the position() function:

    <CurrentIteration><xsl:value-of select="position()" /></CurrentIteration>


    Matthew.

    Tuesday, September 11, 2012 3:18 PM
  • Hi ,

    I can take the count of node in loop <L_transaction> ,but as you see my "Factor" node is outside, hence if I used the variable having value of count,Its giving me error:

    "Variable Outof Scope or not declaried"

    Wednesday, September 12, 2012 5:01 AM
  • Prashant,

    The point is that, your are checking a condition in the "for-each". The condition that you match will be the same for your Factors also.

    And you dont have to use a variable, just take the count using "value-of ".

    Sample Input:

    <Input>
    <Patient>
    <Id>1</Id>
    </Patient>
    <Patient>
    <Id>2</Id>
    </Patient>
    <Patient>
    <Id>3</Id>
    </Patient>
    </Input>

    XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
    <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:template match="/"	>
    	<MessageData>
    		<xsl:for-each select="/Input/Patient">
    			<L_Transaction>
    				<PatientNumber>
    					<xsl:value-of select="./Id"/>
    				</PatientNumber>
    			</L_Transaction>
    		</xsl:for-each>
    	</MessageData>
    	<BatchTrailer>
    	<Factors><xsl:value-of select="count(/Input/Patient)"/></Factors>	
    	</BatchTrailer>
    	</xsl:template>
    </xsl:stylesheet>
    

    Output:

    <?xml version="1.0" encoding="UTF-8"?>
    <MessageData>
    	<L_Transaction>
    		<PatientNumber>1</PatientNumber>
    	</L_Transaction>
    	<L_Transaction>
    		<PatientNumber>2</PatientNumber>
    	</L_Transaction>
    	<L_Transaction>
    		<PatientNumber>3</PatientNumber>
    	</L_Transaction>
    </MessageData>
    <BatchTrailer>
    	<Factors>3</Factors>
    </BatchTrailer>

    Lets us know if your expecting something else. Thanks.

    Sriram N

    Please mark as answer if it helps.


    Sriram N http://srirambiztalks.wordpress.com/

    Wednesday, September 12, 2012 7:40 AM
  • Thanks a ton Sriram for the details explanation.I modifed some of ur code and its working for me.

    Wednesday, September 12, 2012 7:57 AM