12 Replies Latest reply on Jan 16, 2015 9:44 PM by sorrow

    XSLT LOOP COUNTER

    sorrow

      Hi,

      I am trying to get a Loop# in this stylesheet.  If its on loop 1 than I want a parameter to say 1, if on loop 2 a parameter to say 2.......

       

      This is the coding I am currently using below, but I cant find a way to do this.

       

      Thanks for your help!

       

      Scott

       

       

          <RESULTSET FOUND="">

               <xsl:for-each select="./BTI_OPM_BATCH_EXTRACT/LIST_BATCH_HDR/BATCH_HDR/LIST_SEQUENCE/SEQUENCE/SEQUENCE_LIST/SEQ">

                 <xsl:if test="../../../../ROUT_CLASS='CUOL'">

                <xsl:variable name="LOOP" select="1"/>

                 <ROW MODID="" RECORDID="">

                  <COL><DATA><xsl:value-of select="../../../../CUSTOMER"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../CUSTOMER_NUMBER"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../CUSTOMER_PO_NUMBER"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../ROUT_CLASS"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../ORG"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../BATCH_NUMBER"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../BATCH_ID"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../PARENT_BATCH_ID"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../SALES_ORDER_NUMBER"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../SALES_ORDER_LINE_NUMBER"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../SCHEDULE_SHIP_DATE"/></DATA></COL>

                  <COL><DATA><xsl:value-of select='.'/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PROD_TYPE"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PROD_QTY"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PROD_UOM"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PROD_BATCH_ID"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PROD_ITEM_TYPE"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/MIX_DESIGNATOR"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PLATE_DESIGNATOR"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/ORG_ROW/ROWLIST/ROWLI[$LOOP]"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/ORG_COL/COLLIST/COLLI[$LOOP]"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/SCALE"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PURIFICATION"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/FIVE_MOD_CAT"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/THREE_MOD_CAT"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/OLIGO_FUNCTION"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/MAX_VOL"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/NOTES"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/SHIPPING_VESSEL"/></DATA></COL>

                  <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/EXACT_NMOL"/></DATA></COL>

                 </ROW>

               </xsl:if>

              </xsl:for-each>

            </RESULTSET>

        • 1. Re: XSLT LOOP COUNTER
          user19752

          You may be able to use position() as counter if you omit if statement as

                   <xsl:for-each select="./BTI_OPM_BATCH_EXTRACT/LIST_BATCH_HDR/BATCH_HDR/LIST_SEQUENCE/SEQUENCE/SEQUENCE_LIST/SEQ[../../../../ROUT_CLASS='CUOL']">

          • 2. Re: XSLT LOOP COUNTER
            nicolai

            I agree with  user19752 -  position() is that you need to use. XSLT is more of a query, than a procedural language.

            The only alternatives to the position() is to use an additional template, but this has no advantage and just adds complicity.

             

            The other thing to remember is that variables in XSLT are not real variables - they are constants. Once you set your variable, you can not change it, so you will need to change the line <xsl:variable name="LOOP" select="1"/> to something else.

            What is the reason for using the variable?

            • 3. Re: XSLT LOOP COUNTER
              mbraendle

              I wouldn't say XSLT is more of a query. It's a functional language. As such, it does not allow side effects. Changing the value of a variable such as $x = $x + 1 (the counter sorrow asked for) is a side effect. In XSLT, you solve this by using a recursive template call, where one passes the value of $x + 1 to a new variable defined in the context of the template.

               

              Example:

               

              <!-- start value-->

              <xsl:variable name="mycounter" select="1"/>

              <xsl:call-template name="increase_counter">

              <xsl:with-param name="mycounter" select="$mycounter"/>

              </xsl:call-template>

               

              <xsl:template name="increase_counter">

              <xsl:param name="mycounter">

              <xsl:choose>

                <!-- exit condition -->

                <xsl:when test="$mycounter &gt; 1000">

                    <xsl:value-of select="concat($mycounter, ' finished')"/>

                </xsl:when>

                <xsl:otherwise>

                  <!-- print counter and recursively call template again -->

                  <xsl:value-of select="$mycounter"/>

                  <xsl:call-template name="increase_counter">

                      <xsl:with-param name="mycounter" select="$mycounter + 1"/>

                  </xsl:call-template>

                </xsl:otherwise>

              </xsl:template

               

              (there may be some typos, just wrote the example out of my mind)

               

              However, this not possible in combination the xsl:for-each loop, since each loop step of xsl:for-each changes the context, i.e. variables defined within xsl:for-each loop steps have scope only for the current loop step. This is where the trick of user19752 chimes in: He moves the if-condition within the loop into the XPath expression used in the xsl:for-each (the part that reads [../../../../ROUT_CLASS='CUOL'] ), so that a counter variable having to rely on recursion can be avoided and instead the position() function can be used. That's why he got a like from me.

              • 4. Re: XSLT LOOP COUNTER
                beverly

                See these links for "variables" in XSLT:

                 

                XSLT <xsl:param> Element

                XSLT <xsl:variable> Element

                 

                position() has more to do with the SOURCE elements, so if there are these:

                <elements>

                <element>abc</element>

                <element>def</element>

                <element>ghi</element>

                </elements>

                then the "position()" of the element for the value "abc" is 1

                 

                HTH,

                beverly

                • 5. Re: XSLT LOOP COUNTER
                  mbraendle

                  Good and correct hints, Beverly. In a xsl:for-each loop, the XPath expression defines context, from where the position() of the currently addressed element is determined. The current element and all its descendants (i.e. the XML fragment tree starting from the current element) can be determined using current() .

                   

                  There are various ways to address an element (using the example of Beverly):

                   

                  <xsl:variable name="pointer" select="2"/>

                  <xsl:value-of select="/elements/element[2]"/>      the second element

                  <xsl:value-of select="/elements/element[$pointer]"/>   wrong, does not work

                  <xsl:value-of select="/elements/element[position() = $pointer]"/>  works

                  • 6. Re: XSLT LOOP COUNTER
                    sorrow

                    Hi sorry for the delay, but how do you set pointer to the current loop?  I need it to understand what loop it is on and set pointer to that loop number?

                     

                    Not sure if I am using the right wording for this but I am not an expert on this language.

                     

                    Thanks for all of your replies and help!

                     

                    Scott

                    • 7. Re: XSLT LOOP COUNTER
                      user19752

                      Martin already wrote some examples using position().

                      I'm not sure about what you want, since you wrote only XSLT, need input and output.
                      I guessed you don't need looping all SEQ for counting since the xsl:variable is after xsl:if. Do you need serial number or skipped number ?

                       

                      You are attempting apply same variable $LOOP to ROWLI and COLLI, this may be unusual use, but I don't know of what is really you need.

                      • 8. Re: XSLT LOOP COUNTER
                        sorrow

                        Sorry, didnt mean to offend.  I am less than a novice in this area. Just trying to muddle my way thru this.

                         

                        Thanks for all of the comments!

                         

                        Scott

                        • 9. Re: XSLT LOOP COUNTER
                          beverly

                          here's a rather complex explanation:

                          Tip: Loop with recursion in XSLT

                           

                          an older FM kb post (while there is no longer XSLT web publishing, it might give you examples):

                          Looping in XSLT | FileMaker

                          • 10. Re: XSLT LOOP COUNTER
                            sorrow

                            Thanks Beverly!

                             

                            Filemaker one might be what I am looking for!

                             

                            Scott

                            • 11. Re: XSLT LOOP COUNTER
                              user19752

                              Good link beverly, I mentioned the method 1 in 1st my reply, and mbraendle wrote the method 2.

                               

                              FM made a typo in the link,  last line of "HTML Code Generated" should be 5 ( too clear for everyone so not need to say... )

                              • 12. Re: XSLT LOOP COUNTER
                                sorrow

                                Just wanted to post how I resolved this issue, incase someone else needed help.  I was able to get some help from someone on Stack Overflow.  He gave me just what I needed.

                                 

                                I have modified it to suit our needs but this works great.

                                 

                                <?xml version="1.0" encoding="UTF-8"?>

                                <xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

                                  <xsl:template match="/">

                                   <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">

                                      <METADATA>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="CUSTOMER" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="CUSTOMER_NUMBER" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="CUSTOMER_PO_NUMBER" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ROUT_CLASS" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ORG" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="BATCH_NUMBER" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="BATCH_ID" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="PARENT_BATCH_ID" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="SALES_ORDER_NUMBER" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="SALES_ORDER_LINE_NUMBER" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="SCHEDULE_SHIP_DATE" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="BOMLI" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="SEQNAME" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="PROD_ITEM" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="PROD_QTY" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="PROD_UOM" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="PROD_BATCH_ID" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="PROD_ITEM_TYPE" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="MIX_DESIGNATOR" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="PLATE_DESIGNATOR" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ORG_ROW" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ORG_COL" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="SCALE" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="PURIFICATION" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="FIVE_MOD_CAT" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="THREE_MOD_CAT" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="OLIGO_FUNCTION" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="MAX_VOL" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="NOTES" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="SHIPPING_VESSEL" TYPE="TEXT"/>

                                        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="EXACT_NMOL" TYPE="TEXT"/>

                                      </METADATA>

                                      <RESULTSET>

                                        <xsl:for-each select="./BTI_OPM_BATCH_EXTRACT/LIST_BATCH_HDR/BATCH_HDR/LIST_BOM/BOM/BOM_LIST">

                                         <xsl:for-each select="BOMLI">

                                          <xsl:variable name="i" select="position()"/>

                                           <ROW>

                                            <COL><DATA><xsl:value-of select="../../../../CUSTOMER"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../CUSTOMER_NUMBER"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../CUSTOMER_PO_NUMBER"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../ROUT_CLASS"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../ORG"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../BATCH_NUMBER"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../BATCH_ID"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../PARENT_BATCH_ID"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../SALES_ORDER_NUMBER"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../SALES_ORDER_LINE_NUMBER"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../SCHEDULE_SHIP_DATE"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select='.'/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_SEQUENCE_NAME/SEQUENCENAME/SQNLIST/SQNLI[$i]"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PROD_ITEM"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PROD_QTY"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PROD_UOM"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PROD_BATCH_ID"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PROD_ITEM_TYPE"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/MIX_DESIGNATOR"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PLATE_DESIGNATOR"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/ORG_ROW/ROWLIST/ROWLI[$i]"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/ORG_COL/COLLIST/COLLI[$i]"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/SCALE"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/PURIFICATION"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/FIVE_MOD_CAT"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/THREE_MOD_CAT"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/OLIGO_FUNCTION"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/MAX_VOL"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/NOTES"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/SHIPPING_VESSEL"/></DATA></COL>

                                            <COL><DATA><xsl:value-of select="../../../../LIST_PRD_DTL/PRD_DTL/EXACT_NMOL"/></DATA></COL>

                                           </ROW>

                                        </xsl:for-each>

                                       </xsl:for-each>

                                      </RESULTSET>

                                    </FMPXMLRESULT>

                                   </xsl:template>

                                </xsl:stylesheet>

                                 

                                Thanks for all the help!

                                 

                                Scott