xsl-list
[Top] [All Lists]

Re: [xsl] Is it possible to store XSLT code fragments as data and then dynamically evaluate those code fragments?

2019-06-20 06:49:18
On 20.06.2019 13:38, Costello, Roger L. costello(_at_)mitre(_dot_)org wrote:
Hello XSLT experts!

As you know, XPath expressions can be stored in an XML document and then an 
XSLT program can be written which inputs each expression and dynamically 
evaluates them. For example, I created several XPath expressions to query Book 
data; I stored the XPath expressions this way:

<Queries>
     <Query>
         <XPath>/Bookstore/Book</XPath>
     </Query>
     <Query>
         <XPath>/Bookstore/Book[Binding eq 'paperback']</XPath>
     </Query>
</Queries>

Then I created an XSLT program which loops over each XPath expression and 
evaluates the expression against the Book document:

<xsl:variable name="queries" select="doc('XPath-Queries.xml')" />

<xsl:template match="/">
     <Results>
         <xsl:variable name="book-doc" select="."/>
         <xsl:for-each select="$queries//Query">
             <Query-Result>
                 <xsl:variable name="result" as="element()*">
                     <xsl:evaluate xpath="./XPath" context-item="$book-doc" />
                 </xsl:variable>
                 <xsl:sequence select="$result" />
             </Query-Result>
         </xsl:for-each>
     </Results>
</xsl:template>

The ability to store XPath expressions as data and then dynamically evaluate 
them is terrific!

I would like to take the next step and store XSLT code fragments as data and 
then dynamically evaluate them. For example, I have a code fragment for 
converting the cost of a Book in U.S. Dollars (USD) to the cost in Great 
Britain Pounds (GBP):

<Book currency="GBP">
     <xsl:value-of select="number($book-in-usd/Cost) * 0.79" />
</Book>

I vaguely recall that long ago there was a proposal by the XSLT working group 
to provide an eval() function which would do exactly what I desire; but I 
checked the latest specification and it appears the eval() function wasn't 
accepted. Bummer.

The big picture is this: I want to perform various mapping operations on the Book data. I 
want the mapping operations expressed in a declarative manner so that Subject Matter 
Experts (SMEs) can review the mappings for correctness (e.g., upon reviewing the above 
code fragment the SME says: "Yes, that is the correct way to convert costs in USD to 
GBP"). Ideally, the mapping operations would be stored as data and then dynamically 
evaluated (after the SMEs have certified their correctness). What do you recommend? It 
appears that dynamic evaluation of XSLT code fragments is not possible, so what is the 
next best thing?

In XPath 3.1 there is the
  transform
function to run an XSLT 3 stylesheet from XPath 3 or XSLT 3 or XQuery 3
code, it allows you to use apply-templates or call-template or even run
one of your user-defined functions.

Saxon 9.8/9.9 support that even in the open-source HE edition.
--~----------------------------------------------------------------
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>