Re: [xsl] How to dynamically evaluate an equation in the input XML document?

Subject: Re: [xsl] How to dynamically evaluate an equation in the input XML document?
From: "Mukul Gandhi mukulg@xxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 15 Jul 2021 06:04:49 -0000
On Thu, Jul 15, 2021 at 12:18 AM Roger L Costello costello@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> I have XML documents like this:
> <Convert-to-Celsius>
>     <equation>(Fahrenheit - 32) * (5/9)</equation>
>     <variable>
>         <name>Fahrenheit</name>
>         <value>32</value>
>     </variable>
> </Convert-to-Celsius>
> The document contains an equation which might contain variables. If it
> does contain variables, then I need to fetch their values and replace the
> variables in the equation with their values and then compute the value of
> the equation.
> Have you done this kind of dynamic equation solving using XSLT? If so, how
> did you do it?

In an XSLT 2.0 environment with Saxon, I've attempted to do this as
following (examples that I've run are cited),

XSLT stylesheet,

<xsl:stylesheet xmlns:xsl="";

                         exclude-result-prefixes="xs saxon fn1"

   <xsl:output method="text"/>

   <xsl:template match="Input">

   <xsl:function name="fn1:constructFinalExpression" as="xs:string">
      <xsl:param name="equation" as="xs:string"/>
      <xsl:param name="variable" as="element(variable)"/>

        <xsl:when test="$variable/following-sibling::variable">
           <xsl:sequence select="fn1:constructFinalExpression(
$variable/name, $variable/value),

           <xsl:sequence select="replace($equation, $variable/name,


1) XML document input,

  <equation>(Fahrenheit - 32) * (5 div 9)</equation>

Transformation result,


2) XML document input,

  <equation>(x + 5) * (y + 5) * z</equation>

Transformation result,


I've observed that, for example, if the input equation contains the
division operator as '/' instead of 'div' (as in XPath), Saxon gives me
following errors,

XPTY0019: Required item type of first operand of '/' is node(); supplied
value has item type xs:integer

Mukul Gandhi

Current Thread