xsl-list
[Top] [All Lists]

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

2021-07-15 19:50:43
You could also express or transform the expression into Content MathML and
use a processor for that?
https://www.w3.org/TR/MathML3/chapter4.html

On Thu, Jul 15, 2021 at 8:43 AM Dimitre Novatchev 
dnovatchev(_at_)gmail(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

You can short-circuit the process by generating the lexical analyser and
syntax analyser directly from a BNF specification
using a parser generator; a popular choice in this community is Rex from
Gunther Rademacher
(though this does an excellent job, it suffers from (a) not being
published as open source, and (b) being poorly documented).
A benefit of Rex is that you can generate the parser components as XSLT
or XQuery code.


And a general LR-1 parser written entirely in XSLT has been implemented
long ago (around 2007 if I remember well) and is part of the FXSL 2.0
library:

https://github.com/dnovatchev/FXSL-XSLT2/blob/master/f/func-lrParse.xsl

I have used it to parse XPath 2.0 and a subset of JSON:


https://github.com/dnovatchev/FXSL-XSLT2/blob/master/newWork/XPathParse1.xsl


https://github.com/dnovatchev/FXSL-XSLT2/blob/master/newWork/xpath-parse-test.xsl


https://github.com/dnovatchev/FXSL-XSLT2/blob/master/newWork/testFunc-jason-document.xsl

Cheers,
Dimitre

On Thu, Jul 15, 2021 at 8:28 AM Michael Kay mike(_at_)saxonica(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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.


As Dimitre points out, this is an expression (or formula), not an
equation.

Writing a simple expression interpreter is a common exercise on
undergraduate computer science courses, and it's not clear from your
question whether you are familiar with the basic principles. The typical
solution would be to write a lexical analyser that splits the expression
into tokens, then add a syntax analyser to build a syntax tree that
represents the grammatical structure of the expression, and then (using the
interpreter design pattern) write a depth-first recursive tree walker that
evaluates the expression nodes in this tree; the interpreter would have
access to a context object that contains the bindings of variables to
values, typically as an XDM map.

If this description is too terse, then there are plenty of textbooks that
explain it in more detail.

You can short-circuit the process by generating the lexical analyser and
syntax analyser directly from a BNF specification using a parser generator;
a popular choice in this community is Rex from Gunther Rademacher (though
this does an excellent job, it suffers from (a) not being published as open
source, and (b) being poorly documented). A benefit of Rex is that you can
generate the parser components as XSLT or XQuery code.

Many people in this community would choose to use XPath as the expression
language rather than inventing your own. That has the benefit that you
don't need to specify and implement the language yourself, it's already
been done; and you can then use xsl:evaluate directly for the evaluation.

Michael Kay
Saxonica




--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/965995> (by
email <>)

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>