[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: [xslt 2.0] Difference betwen functions and templat

Subject: Re: [xslt 2.0] Difference betwen functions and templates
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Thu, 19 Jul 2007 15:07:32 +0100
Re:  [xslt 2.0] Difference betwen functions and templat
On 7/19/07, Abel Braaksma (online) <abel.online@xxxxxxxxx> wrote:
I think I believed the wrong term, I meant method overloading
instead of polymorphism. I don't agree with your statement that
single polymorphism is what is offered by XSLT. I

Not out-of-the box, but you can re-create it. "Polymorphism" is still a suitable term imho.


POLYMORPHISM
If such a thing would exist it might look something like this:

<xsl:module name="mod:cat" implements="animal">
   <xsl:function name="talk">Meow!</xsl:function>
   <xsl:function name="name">Cat</xsl:function>
</xsl:module>

<xsl:module name="mod:dog" implements="animal">
   <xsl:function name="fu:talk">Bark!</xsl:function>
   <xsl:function name="fu:name">Cat</xsl:function>
</xsl:module>

<xsl:module name="mod:animal">
   <xsl:function name="fu:talk" deferred="yes">
   <xsl:function name="fu:name" deferred="yes">
   <xsl:function name="fu:say-name">
      <xsl:sequence select="fu:talk(), ' I am a ', fu:name()" />
   <xsl:function>
</xsl:module>

<!-- suppose that a module can be called like a xpath axis, then -->
<xsl:template match="/">
   <xsl:sequence select="'mod:dog:', mod:dog::say-name()" />
   <xsl:sequence select="'mod:cat:', mod:cat::say-name()" />
</xsl:template>

<!-- output will then be -->
mod:dog: Bark! I am a dog
mod:cat: Meow! I am a cat

Not quite - that should really be "mod:animal::say-name()" called twice to be polymorphism - you're calling the "concrete" method there rather than the abstract.

So to recreate your example using named templates and import precedence:

Animal.xslt
<xsl:template name="sayName">
	<xsl:value-of select="$name"/>
</xsl:template>

And then Dog.xslt:
<xsl:import href="animal.xslt"/>

<xsl:variable name="name" select="'Dog'"/>
	
<xsl:template match="/">
	<xsl:call-template name="sayName"/>
</xsl:template>

And the equivalent Cat.xslt would just have the different variable:
<xsl:variable name="name" select="'Cat'"/>


So why is this polymorphism? Because the common Animal.xslt will use the $name variable depending on how the transform was constructed. You call "sayName" in the Animal stylesheet and that uses the $name variable in the Dog/Cat stylesheet.

The Animal stylesheet is kind-of abstract in that it won't run on it's
own because it lacks a declaration for $name.

Perhaps in the future if "compiled stylesheet modules" become a
reality then abstract and final modifiers for templates and functions
will be included too.

http://www.biglist.com/lists/xsl-list/archives/200506/msg00429.html

--
http://andrewjwelch.com

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.