Subject: Re: [xsl] object-oriented XSL From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> Date: Sun, 25 Aug 2002 16:34:03 +0100 |
Hi Martin, > what if defining a new XPath function was as easy as writing a named > template: > > <xsl:function name="fib"> > <!-- calculates fibonacci numbers recursively --> > <xsl:param name ="n" /> > <xsl:choose> > <xsl:when test ="$n <= 2" > > <xsl:value-of select="1" /> > </xsl:when> > <xsl:otherwise> > <xsl:value-of select="fib($n - 1) + fib($n - 2)"/> > </xsl:otherwise> > </xsl:choose> > </xsl:function> You'll be able to do this in XSLT 2.0. The functions have to be in a namespace, and the result of the function is indicated through a xsl:result function; you're only allowed parameter and variable definitions before the xsl:result element, so the above will look something like: <xsl:function name="my:fib"> <!-- calculates fibonacci numbers recursively --> <xsl:param name="n" /> <xsl:result select="if ($n <= 2) then 1 else my:fib($n - 1) + my:fib($n - 2)" /> </xsl:function> or: <xsl:function name="my:fib"> <!-- calculates fibonacci numbers recursively --> <xsl:param name="n" /> <xsl:variable name="result"> <xsl:choose> <xsl:when test ="$n <= 2" > <xsl:value-of select="1" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="fib($n - 1) + fib($n - 2)"/> </xsl:otherwise> </xsl:choose> </xs:variable> <xsl:result select="number($result)" /> </xsl:function> [I'm not sure that the algorithm's right, mind you ;) ] You can do something similar today with func:function/func:result from EXSLT, which are supported by several processors, including Saxon, Xalan and 4XSLT. See http://www.exslt.org/func/elements/function. > and what if you could define types and associate functions with > them, wouldn't that be pretty cool? XPath would have to be extended > to allow type function invokation, eg: > > <xsl:type name="User"> > <xsl:variable name="name"/> > > <xsl:function name="User"> > <!-- this is a constructor, it is defined as a global XPath function and returns a User object --> > <xsl:param name="name"/> > </xsl:function> > > <xsl:function name="speak"> > <!-- this is a type function, can only be invoked on an object of type User --> > <xsl:value-of select="concat('Hello, my name is ',$name)"/> > </xsl:function> > </xsl:type> > > <xsl:variable name="newUser" select="User('Fred')"/> > <xsl:value-of select="$newUser.speak()"/> <!-- uses '.' for type function invokation --> The path that XSLT 2.0 and XPath 2.0 is going down is that you define "types" using W3C XML Schema. Types in W3C XML Schema are obviously different from types in normal programming languages, since they're oriented around XML structures, but a User type might look something like: <xs:complexType name="User"> <xs:attribute name="name" type="xs:token" /> </xs:complexType> If this schema is imported into the stylesheet, then you can create an element of the type User with something like: <xsl:variable name="newUser"> <user name="Fred" /> </xsl:variable> [I can't work out how to say that the user element is of type User; there's xsl:type-annotation but that only works for elements with simple types...] and get the name of $newUser with: $newUser/user/@name Having said that, I like the idea of having "type functions". The current XPath 2.0 Functions and Operators draft is littered with functions called things like: get-years-from-yearMonthDuration($dur) get-local-name-from-QName($name) I think that it would be great if instead we could do things like: $dur.years() $name.local-name() to get hold of the property (though there are some syntax problems with that, since '.' is a name character in XML/XPath/XSLT.) Cheers, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] object-oriented XSL, martin | Thread | Re: [xsl] object-oriented XSL, martin |
[xsl] Dynamic column in table, Shabbir Rahman | Date | RE: [xsl] object-oriented XSL, martin |
Month |