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

  • SEARCH
  • Home
  • 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 8585481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:04:38+00:00 2026-06-11T22:04:38+00:00

I am using XSLT 1.0 Suppose I have an xml that has certain num

  • 0

I am using XSLT 1.0

Suppose I have an xml that has certain “num” elements that could be anywhere in an XML document like this :-

<elem1>
  <num>24</num>
</elem1>
<elem2>
  <description>
    <num>18</num>
  </description>
</elem2>
<elem3>
  <elem1>
    <num>36</num>
  </elem1>
</elem3>

I want to convert it into :-

<elem1>
  <gcd multiple="4">6</gcd>
</elem1>
<elem2>
  <description>
    <gcd multiple="3">6</gcd>
  </description>
</elem2>
<elem3>
  <elem1>
    <gcd multiple="6">6</gcd>
  </elem1>
</elem3>

Now I have a template that can compute gcd of two elements like this :-

<xsl:template name="gcd">
  <xsl:param name="x"/>
  <xsl:param name="y"/>
  <!-- snipping code to compute gcd that goes here -->
</xsl:template>

Given this template for a gcd, I’m guessing that I will have to do something like loop over the num elements like this :-

<xsl:variable name="global_gcd">
  <xsl:for-each select="//ns0:num">
    <!-- something probably goes here --> 
    <!-- say a temporary value x=0 initially -->
    <!-- and then x = gcd (x, value of current node) -->
  </xsl:for-each>
</xsl:variable>

And then I can purportedly use this variable in my template for “num” to generate the desired “gcd” element along with attribute. That too I can do.

What should the above loop be like? If not a for-each, how would I actually compute the gcd of all the “num” elements in my XML document assuming I already have a working gcd template for two elements?

I suppose this would be a lot easier if I had all the “num” elements as siblings or something but unfortunately I don’t. The tree relationship between any two of these could potentially be arbitrary. The only thing I know, is that they all have the same name.

  • 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-06-11T22:04:39+00:00Added an answer on June 11, 2026 at 10:04 pm

    XSLT is a functional language, so there’s no concept of updatable variables – a variable has its value set when it is created, and that value cannot be changed subsequently. If you must do this in pure XSLT you’ll need to reformulate the problem as a recursive template instead of a loop

    <xsl:template name="gcd">
      <xsl:param name="nodes" />
      <xsl:choose>
        <xsl:when test="count($nodes) = 0">0</xsl:when>
        <xsl:otherwise>
          <xsl:variable name="gcdOfRest">
            <xsl:call-template name="gcd">
              <xsl:with-param name="nodes" select="$nodes[position() > 1]" />
            </xsl:call-template>
          </xsl:variable>
          <!-- existing logic goes here, calculating GCD between number($nodes[1])
               and number($gcdOfRest) -->
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    

    and call this template passing //ns0:num as the nodes param.

    But if you have the option of writing an extension function and plugging it in, that may be more efficient.

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

Sidebar

Related Questions

Suppose I have some XML like this: <section name=SampleSection> <item name=ScoredItem1> <attributes> <scored data_type=boolean
I know that if I have an XML file like this: <persons> <class name=English>
Suppose I have XML like this: <child_metadata> <metadata> <attributes> <metadata_key value=include/> <metadata_value value=value1/> </attributes>
Is it possible to merge elements using XSLT. If I have the following XML
Suppose you have a Django view that has two functions: The first function renders
Using XSLT 1.0 - I have the following xml and I an trying to
Using xslt/xpath, I need to be able to select the two elements which have
I am using XSLT and XML, I have below XML format with me. <?xml
This is follow-up to: using xslt to create an xml file in c <element1
This is my first time using XSLT. I'm trying to create a file that

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.