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 80573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:19:26+00:00 2026-05-10T21:19:26+00:00

How do I save the iterations that have occurred in an xsl:for-each? (variables in

  • 0

How do I save the iterations that have occurred in an xsl:for-each? (variables in XSL are immutable)

My goal is to find the MAX number of children for any node at a particular level.

For example, I might want to print that there are no more than 2 Response nodes for any Question in this survey:

<?xml version='1.0' encoding='ISO-8859-1'?> <?xml-stylesheet type='text/xsl' href='testing.xsl'?> <Survey>   <Question>     <Response text='Website' />     <Response text='Print Ad' />   </Question>   <Question>     <Response text='Yes' />   </Question> </Survey>  <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='/'>   <html>   <head>   </head>   <body>     <xsl:for-each select='Survey'>       The survey has <xsl:value-of select='count(child::Question)'/> questions.         <br />       <xsl:variable name='counter'>0</xsl:variable>       <xsl:for-each select='Question'>         <!-- TODO: increment the counter ??????? -->       </xsl:for-each>       No more than <xsl:value-of select='$counter'/> responses were returned for any question.     </xsl:for-each>   </body>   </html> </xsl:template> </xsl:stylesheet> 
  • 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. 2026-05-10T21:19:26+00:00Added an answer on May 10, 2026 at 9:19 pm

    One doesn’t ‘save the iterations that have occurred in an xsl:for-each’ because XSLT is a functional language and variables are immutable.

    The following transformation finds the wanted maximum:

     <xsl:stylesheet version='1.0'  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>  <xsl:output method='text'/>      <xsl:template match='/'>       <xsl:call-template name='maximum'>         <xsl:with-param name='pNodes' select='*/Question'/>       </xsl:call-template>     </xsl:template>      <xsl:template name='maximum'>       <xsl:param name='pNodes'/>        <xsl:variable name='vNumNodes' select='count($pNodes)'/>        <xsl:choose>         <xsl:when test='$vNumNodes = 1'>           <xsl:value-of select='count($pNodes[1]/Response)'/>         </xsl:when>         <xsl:otherwise>           <xsl:variable name='vHalf'                 select='floor($vNumNodes div 2)'/>            <xsl:variable name='vMax1'>            <xsl:call-template name='maximum'>             <xsl:with-param name='pNodes'                  select='$pNodes[not(position() > $vHalf)]'/>            </xsl:call-template>           </xsl:variable>            <xsl:variable name='vMax2'>            <xsl:call-template name='maximum'>             <xsl:with-param name='pNodes'                  select='$pNodes[position() > $vHalf]'/>            </xsl:call-template>           </xsl:variable>            <xsl:value-of select=            '$vMax1*($vMax1 >= $vMax2) + $vMax2*($vMax2 > $vMax1)'/>         </xsl:otherwise>       </xsl:choose>     </xsl:template> </xsl:stylesheet> 

    When applied on the provided XML document:

     <Survey>     <Question>         <Response text='Website' />         <Response text='Print Ad' />     </Question>     <Question>         <Response text='Yes' />     </Question> </Survey> 

    the wanted result is produced:

    2

    Do note the following: The template named ‘maximum‘ calls itself recursively and implements the DVC (Divide and Conquer principle) to minimize the recursion stack depth. The list of nodes is split into two, the maximums of the two lists are calculated (recursively) and the bigger of the two is returned.

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

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It's difficult to answer exactly without seeing the structure of… May 11, 2026 at 8:44 am
  • added an answer reduce the problem to its simplest form: you have a… May 11, 2026 at 8:44 am
  • added an answer Behavior by design, per http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=377495. The memory is reserved, but… May 11, 2026 at 8:44 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.