[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] When to use conditional constructions?


Subject: Re: [xsl] When to use conditional constructions?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 31 Mar 2014 20:45:42 -0700

On Mon, Mar 31, 2014 at 8:03 PM, Graydon <graydon@xxxxxxxxx> wrote:
>> Exactly as done in the Balisage paper I quated somewhere at the start
>> of this thread.
>
> And a great light dawns about why maps are worth another fundamental
> type!  (Wasn't seeing what they did that you couldn't do with an XML
> structure.)
>
> Thank you; I shall have to try to comprehend that article.


 To make this understanding easier, here is the short Chapter 7 of the
article, providing a short and complete example:




7. Consuming an XPath function library from XSLT 3.0 and XQuery 3.1

It is possible in XSLT 3.0 to conveniently consume an XPath function
library directly from a text file.

Here is an example:

A simple function library (file SimpleLibraryAsMap.xpath):

 let
         $incr := function($n as xs:integer)
                   {$n +1},
         $mult := function($m as xs:integer, $n as xs:integer)
                   {$m * $n},
         $decr := function($n as xs:integer)
                   {$n -1},
         $idiv := function($m as xs:integer, $n as xs:integer)
                   {$m idiv $n}

 (: Provide the function libary  as a map:)
 return
      map {
           'incr'     := $incr,
           'mult'     := $mult,
           'decr'     := $decr,
           'idiv'     := $idiv
          }

XSLT transformation that uses this function library:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">

  <!-- The Function Library -->
  <xsl:variable name="vMath" as="map(*)">
    <xsl:evaluate xpath="unparsed-text('SimpleLibraryAsMap.xpath')"/>
  </xsl:variable>

  <xsl:sequence select="'incr(3) = ', $vMath('incr')(3)"/>
 </xsl:template>
</xsl:stylesheet>

The result is: "incr(3) = 4"

The "importing" of the library is done via the simple and powerful
combination of the standard XPath 3.0 function unparsed-text() and the
new XSLT 3.0 instruction <xsl:evaluate>

Consuming the function library from a map-cognizant XQuery processor
is even simpler -- we just need to wrap the library as/into an XQuery
module and then import this module in the consuming XQuery.

Cheers,
Dimitre Novatchev


Current Thread
Keywords