Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:31:31+00:00 2026-05-30T08:31:31+00:00

I am learning xslt and had one question about how can i use xslt

  • 0

I am learning xslt and had one question about how can i use xslt variable in diff. for each loop. I know xslt isn’t a procedural language so variable declared in for loop cannot be accessed in another loop. But is there any way I can just declare global variable then assign some value in first for loop and use that variable in second for loop?

Any ideas would be highly appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:31:32+00:00Added an answer on May 30, 2026 at 8:31 am

    is there any way I can just declare global variable then assign some
    value in first for loop and use that variable in second for loop?

    The way to assign value to an xsl:variable (of course this is only initialization) from within an xsl:for-each, is to include the xsl:for-each in the body of the variable.

    Here is a complete example:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/*">
         <xsl:variable name="vMax">
           <xsl:for-each select="num">
            <xsl:sort data-type="number" order="descending"/>
    
            <xsl:if test="position() = 1">
             <xsl:value-of select="."/>
            </xsl:if>
           </xsl:for-each>
         </xsl:variable>
    
         Values close to the maximum:
    <xsl:text/>
    
           <xsl:for-each select="num">
            <xsl:if test="not($vMax - . > 3) ">
             <xsl:value-of select="."/>
             <xsl:text>&#xA;</xsl:text>
            </xsl:if>
           </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document…

    <nums>
      <num>01</num>
      <num>02</num>
      <num>03</num>
      <num>04</num>
      <num>05</num>
      <num>06</num>
      <num>07</num>
      <num>08</num>
      <num>09</num>
      <num>10</num>
    </nums>
    

    …it first defines a vMax variable that gets its value from the xsl:for-each contained in its body.

    Then the vMax variable is used in the second xsl:for-each to output all numbers that are “close” to the so computed maximum.

    The wanted, correct result is produced:

         Values close to the maximum:
    07
    08
    09
    10
    

    It is also possible to simulate “assigning” a variable with different values by using a recursively called named template and pass the “new value” as parameter to the called template.

    Here is an example showing this technique. Here we are calculating the maximum of values, contained in nodes of a node-set. Every time we access the next node in the node-set, the current maximum is compared to this value and if necessary the new maximum becomes the value of the next node. We then call the same template recursively, passing as the value of the current maximum the new maximum:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/*">
      <xsl:call-template name="max">
        <xsl:with-param name="pList" select="*"/>
      </xsl:call-template>
     </xsl:template>
    
     <xsl:template name="max">
       <xsl:param name="pMax" select="-99999999"/>
       <xsl:param name="pList"/>
    
       <xsl:choose>
         <xsl:when test="$pList[1]">
           <xsl:variable name="vnewMax" select=
           "$pMax * ($pMax >= $pList[1])
           +
            $pList[1] * not($pMax >= $pList[1])
           "/>
    
           <xsl:call-template name="max">
            <xsl:with-param name="pMax" select="$vnewMax"/>
            <xsl:with-param name="pList" select="$pList[position() > 1]"/>
           </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
           <xsl:value-of select="$pMax"/>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied to the same XML document (above), the wanted, correct result is produced:

    10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning xslt, but don't know the best scenarios for use xml +
Learning about notifyAll made me question something about notify: in a typical situation we
Why should we use XSLT to web parts? Can anyone please route me to
Learning about Explicit Cursors and trying to create my frst one.: SET SERVEROUTPUT ON
Learning C# here. I have a monthly process form whereby one can select to
Learning Javascript and have a question about global variables. From my reading, most recommend
Learning about OAuth, I came across a big question. How to call a JavaScript
I've just started learning XML & XSLT and have a quick question regarding Xpath.
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
Still learning magento coding. I wonder is there a way I can print out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.