[xquery-talk] RE: [AW] XQuery and Item Orientation

Michael Kay mike at saxonica.com
Thu Jan 22 09:24:40 PST 2009

> XSLT's polymorphism does not breathe object-orientation. It 
> is meant for, as you put it, "dealing with data whose 
> structure is variable", it is less appropriate for creating 
> interfaces whose abstraction hides the variability of 
> concrete behaviour. I perceive templates in the first place 
> as a mechanism for uniform triggering of variable processing 
> - trigger it and get it done. Just compare the ease of using 
> a function call within an expression with the cumbersome 
> voodoo required for catching a template's output and make it 
> input for further processing!

I agree with all that: but you can use XSLT's templates to achieve this kind
of thing if you choose to.

For example

<xsl:function name="geo:area" as="xs:double">
  <xsl:param name="shape" as="schema-element(polygon)"/>
  <xsl:apply-templates select="$shape" mode="get-area"/>

<xsl:template match="rectangle" mode="get-area" as="xs:double">
  <xsl:sequence select="@width*@height"/>

<xsl:template match="circle" mode="get-area" as="xs:double">
  <xsl:sequence select="@radius*@radius*$PI"/>

etc. Providing a function as an interface to a set of templates in this way
eliminates all the voodoo as far as the caller is concerned. In XSLT 2.1
we're hoping to get some more modularity/encapsulation into this so you can
declare, for example, that the mode is private to a module and the function
is public.

> I am just trying to say that polymorphic functions would 
> introduce something into X-technology that XSLT's templates 
> do not well cover:

I agree that XQuery desperately needs some kind of polymorphism, but I'm not
convinced that the traditional type hierarchy of object-oriented systems
maps well to the XML world as the basis for despatching. Patterns with
user-defined priority give more flexibility.

Michael Kay

More information about the talk mailing list