Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Pinky 2 posts 72 karma points
    Aug 30, 2021 @ 13:46
    Pinky
    0

    How to use for-loop in xslt ?

    Hi, I'm currently working on existing xslt. I have to follow the same pattern of creating a template name and using call-template. here is my below XML:

      <stores>
      <store>
        <books>
          <book>
            <section>1</section>
            <bookinfo>
              <bookdetails>
                <A>Author</A>
                <B>Dreams</B>
              </bookdetails>
              </bookinfo>
              <bookinfo>
              <bookdetails>
                <A>Author20</A>
                <B>Dreams20</B>
                <loss>
                    <losses>
                        <made>made</made>
                        <amot>amot</amot>
                    </losses>
                     </loss>
                     <loss>
                    <losses>
                        <made>made1</made>
                        <amot>amot1</amot>
                    </losses>
                </loss>
                <deducts>
                    <deduct>
                        <pay>pay</pay>
                        <pay1>pay1</pay1>
                    </deduct>
                </deducts>
                <deducts>
                    <deduct>
                        <pay>pay1</pay>
                        <pay1>pay2</pay1>
                    </deduct>
                </deducts>
              </bookdetails>
            </bookinfo>
          </book>
         </books>
         <books>
          <book>
            <section>2</section>
            <bookinfo>
              <bookdetails>
                <A>Author1</A>
                <B>Dreams1</B>
                <loss>
                    <losses>
                        <made>made1</made>
                        <amot>amot2</amot>
                    </losses>
                     </loss>
                     <loss>
                    <losses>
                        <made>made2</made>
                        <amot>amot2</amot>
                    </losses>
                </loss>
                <deducts>
                    <deduct>
                        <pay>payee</pay>
                        <pay1>pay12</pay1>
                    </deduct>
                </deducts>
                <deducts>
                    <deduct>
                        <pay>payee1</pay>
                        <pay1>pay22</pay1>
                    </deduct>
                </deducts>
            </bookdetails>
           </bookinfo>
          </book>
        </books>
      </store>
    </stores>
    

    Here is my XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
        <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="stores">
              <LocalStore>
          <xsl:for-each select="//books/book">
               <xsl:call-template name="name">
                </xsl:call-template>
            </xsl:for-each>
    
    
                 </LocalStore>
         </xsl:template>
    
        <xsl:template name="name">  
         <xsl:variable name="A" select=".//bookinfo/bookdetails/A"/>
          <xsl:variable name="B" select=".//bookinfo/bookdetails/B"/>
    
          <LocalDetails>
             <xsl:if test="string-length($A) > 0">
             <BookType>
               <xsl:value-of select="$A"/>
                  </BookType>
                  </xsl:if>
    
             <xsl:if test="string-length($B) > 0">
               <BookLPP>
                   <xsl:value-of select="$B"/>
                </BookLPP>
                </xsl:if>
             </LocalDetails>
             </xsl:template>
             <xsl:template name="name1">
                 <xsl:variable name="C" select="../../loss/losses/made"/>   
    
             <losstype>
               <xsl:value-of select="$C"/>
             </losstype>
    
    
    </xsl:template>
    </xsl:stylesheet>
    

    I have tried in different ways. But still I'm not getting desired output. I will be getting multiple repetitive elements under each

    <LocalStore>
         <LocalDetails>
         <section>1</section>
          <BookType>Author</BookType>
           <BookLPP>Dreams</BookLPP>
           <BookType>Author20</BookType>
           <BookLPP>Dreams20</BookLPP>
             <losstype>made</losstype>
             <lossamt>amot1</lossamt>
             <LPPtype>made1</LPPtype>
             <LPPamot>amot1</LPPamot>
             <LPPdeducts>pay</LPPdeducts>
             <LPPdeducts2>pay1</LPPdeducts2>
             <LPPlevel>pay1</LPPlevel>
             <LPPLevel2>pay2</LPPLevel2>
              </LocalDetails>
        <lossdetails>
         <section>2</section>
          <BookType>Author1</BookType>
           <BookLPP>Dreams1</BookLPP>
         </LocalDetails>
          </LocalStore>
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies