Subject: Re: [xsl] [xslt 2.0] Difference betwen functions and templates From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx> Date: Thu, 19 Jul 2007 15:07:32 +0100 |
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.
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://andrewjwelch.com
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] [xslt 2.0] Difference bet, Abel Braaksma (onlin | Thread | Re: [xsl] [xslt 2.0] Difference bet, Abel Braaksma (onlin |
Re: [xsl] [Saxon-SA] SAX filter for, Justin Johansson | Date | [xsl] PI for associating a sample i, George Cristian Bina |
Month |