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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:32:56+00:00 2026-05-27T18:32:56+00:00

I am trying to apply constant folding to XML representations of simple numeric expressions.

  • 0

I am trying to apply constant folding to XML representations of simple numeric expressions. Constant folding is the process of replacing constant (sub)expressions with their literal value. Part of constant folding is replacing every binary operation that is applied to two literal values by the computed literal result. So say we have an expression:

1+x+2*(3+1)

and its XML representation:

<addition>
   <addition>
      <value>1</value>
      <variable>x</variable>
   </addition>
   <multiplication>
      <value>2</value>
      <addition>
         <value>3</value>
         <value>1</value>
      </addition>
   </multiplication>
</addition>

we can replace

<addition>
   <value>3</value>
   <value>1</value>
</addition>

with

<value>4</value>

And subsequently we can replace

<multiplication>
   <value>2</value>
   <value>4</value>
</multiplication>

with

<value>8</value>

The final result should look like this (it can be simplified even further, but this is sufficient for now):

<addition>
   <addition>
      <value>1</value>
      <variable>x</variable>
   </addition>
   <value>8</value>
</addition>

Part of my XSLT stylesheet looks like this:

<xsl:template match="addition[count(value) = 2]">
   <value>
      <xsl:value-of select="value[1] + value[2]" />
   </value>
</xsl:template>

<xsl:template match="multiplication[count(value) = 2]">
   <value>
      <xsl:value-of select="value[1] * value[2]" />
   </value>
</xsl:template>

...

<xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
   </xsl:copy>
</xsl:template>

The idea of this template is to recursively replace all operation elements (addition, multiplication, …), for which both operands (children) are value elements, with a single value element containing the computed value. The rest of tree (operations containing variables) is to be kept the same.

However, this stylesheet will only apply constant folding once, only replacing the deepest binary operation elements with their literal value. The only way I can get this stylesheet to work is to keep transforming the output of the last transformation until input and output document are the same. I’m new to XSLT, so I’m pretty sure there should be a more natural way to do this.

  • 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-27T18:32:57+00:00Added an answer on May 27, 2026 at 6:32 pm

    This transformation:

    <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="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="addition[not(descendant::variable)]">
         <xsl:variable name="vArg1">
           <xsl:apply-templates select="*[1]"/>
         </xsl:variable>
         <xsl:variable name="vArg2">
           <xsl:apply-templates select="*[2]"/>
         </xsl:variable>
    
         <value>
           <xsl:value-of select="$vArg1 + $vArg2"/>
      </value>
     </xsl:template>
    
     <xsl:template match="multiplication[not(descendant::variable)]">
         <xsl:variable name="vArg1">
           <xsl:apply-templates select="*[1]"/>
         </xsl:variable>
         <xsl:variable name="vArg2">
           <xsl:apply-templates select="*[2]"/>
         </xsl:variable>
    
         <value>
           <xsl:value-of select="$vArg1 * $vArg2"/>
      </value>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <addition>
       <addition>
          <value>1</value>
          <variable>x</variable>
       </addition>
       <multiplication>
          <value>2</value>
          <addition>
             <value>3</value>
             <value>1</value>
          </addition>
       </multiplication>
    </addition>
    

    produces the wanted, correct result:

    <addition>
       <addition>
          <value>1</value>
          <variable>x</variable>
       </addition>
       <value>8</value>
    </addition>
    

    Explanation: Proper use of templates, xsl:apply-templates and xsl:variable .

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

Sidebar

Related Questions

I'm trying to process XML inner text. I have the following XSLT snippet: <?xml
Im trying to apply different background color to even and odd items in a
I am trying to apply styles to HTML tags dynamically by reading in the
I'm trying to apply a transform to a 3D object in a STL File
I am trying to apply css on my html.dropdownlist with a regular html select
I am trying to apply conditional formatting of certain table cells in my ReportViewer
I'm trying to apply a DataTrigger to a Button and it depends on a
I am trying to apply some transformations on images using a CGContextRef. I am
I am trying to apply a method to an existing object which involves using
I'm trying to apply a specific style to a slider control and I'm having

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.