[xsl] OOXML

Subject: [xsl] OOXML
From: Colin Paul Adams <colin@xxxxxxxxxxxxxxxxxx>
Date: Sat, 28 Jun 2008 07:13:35 +0100
>>>>> "Colin" == Colin Paul Adams <colin@xxxxxxxxxxxxxxxxxx> writes:

>>>>> "Alexander" == Alexander Johannesen <alexander.johannesen@xxxxxxxxx> writes:
    Alexander> On Fri, Jun 27, 2008 at 21:58, John Snelson <john.snelson@xxxxxxxxxx> wrote:
    >>> Maybe you've heard of a language called XSLT? Or XQuery? :-)

    Alexander> Cheap shot. :) Besides, I meant real programming
    Alexander> languages, not a faux functional LISP wannabe in XML
    Alexander> guise, with no OO, so there!

    Colin> :-)

    Colin> So use Eiffel! (with XSLT callouts when desired). 

That was all very amusing (or not, according to your tastes).

But it set me thinking for real. If Dimitre can have FXSL, why can't I
have OOXSL (Dimitre, are you going to expel me now?)?

Given the following file oo_demo.xsl:

<?xml version="1.0" encoding="utf-8"?>
<!-- Demonstration of Object-Oriented programming in XSLT -->

<!--

This demonstrates an OO class named ACCOUNT, which represents a
 bank account equivalent to the following Eiffel class:

class ACCOUNT

feature - - Access

   balance: DOUBLE
   	- - Balance of account in GBP

feature - - Basic operations

   deposit (a_value: DOUBLE) is
   	- - Add `a_value' to `Current'.
      require
         a_value_strictly_positive: a_value > 0.0
      do
      	balance := balance + a_value
      ensure
        deposited: balance = old balance + a_value
      end

    withdraw (a_value: DOUBLE) is
   	- - Withdraw `a_value' from `Current'.
      require
         a_value_strictly_positive: a_value > 0.0
         no_overdrafts: a_value <- balance
      do
      	balance := balance - a_value
      ensure
        deposited: balance = old balance - a_value
      end

invariant

   no_overdraft: balance >= 0.0

end

All operations are encoded in XML requests of the following forms:

<balance-enquiry/>

<deposit>a_positive_number</deposit>

<withdraw>a_positive_number</withdraw>

The document element is named requests. requests may take any number
 of balance-enquiry, deposit and withdraw children.

Contract-violation/error-checking code omitted for now.

-->


<xsl:transform 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
   xmlns:gexslt="http://www.gobosoft.com/eiffel/gobo/gexslt/extension";
   xmlns:exslt="http://exslt.org/system/environment";
   xmlns:account="http://colina.demon.co.uk/ooxsl/demo/account";
   extension-element-prefixes="gexslt"
   version="2.0">

  <xsl:output method="text"/>

  <xsl:template match="/requests">
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="balance-enquiry">
    Balance is : <xsl:value-of select="account:balance()" />
  </xsl:template>

  <xsl:template match="deposit">
    <xsl:variable name="amount" select="text()" />
    <xsl:variable name="balance" select="account:balance()" />
    
    <gexslt:set-environment-variable name="ACCOUNT_BALANCE"
				     value="$amount + $balance"/>
  </xsl:template>
  
  <xsl:template match="withdraw">
    <xsl:variable name="amount" select="text()" />
    <xsl:variable name="balance" select="account:balance()" />
    
    <gexslt:set-environment-variable name="ACCOUNT_BALANCE"
				     value="$amount + $balance"/>
  </xsl:template>
    
  <xsl:function name="account:balance">
    <!-- Logic omitted for treating empty string as zero -->
    <xsl:value-of select="system-property('exslt:ACCOUNT_BALANCE')" />
  </xsl:function>

</xsl:transform>

and oo_demo.xml:

<requests>
 <balance-enquiry/>
 <deposit>3.45</deposit>
 <balance-enquiry/> 
 <withdraw>1.45</withdraw>
 <balance-enquiry/> 
</requests>


Then the command:

gestalt oo_demo.xsl oo_demo.xml 

yields:

 
    Balance is : 
 At line 84 in file:///home/colin/gestalt/oo_demo.xsl:
Fatal error: http://www.w3.org/2005/xqt-errors#XTDE1450: Unknown extension element: gexslt:set-environment-variable

But that's not too difficult to remedy.

The greater difficulty is that is relies on execution order. While
that isn't a problem in practice with the current implementation of
Gestalt, it probably will be in the future. So it becomes a problem of
whether the extension function can be defined in such a way as to
guaranteee the correct execution order (since
http://exslt.org/system/environment is already defined without any
such refinements, this might be difficult).
-- 
Colin Adams
Preston Lancashire

Current Thread