Lambda the Ultimate

inactiveTopic Functions in XSL
started 9/6/2000; 9:48:25 AM - last post 9/6/2000; 2:58:49 PM
Chris Rathman - Functions in XSL  blueArrow
9/6/2000; 9:48:25 AM (reads: 877, responses: 6)
Functions in XSL
Carrying on from a previous post about using XSL as a programming language, I managed to figure out how to create functions in XSL.

The link above points to a short XSL script I created that has a factorial function. Though limited in scope, it shows how the write-once variables are set up, the parameters are passed to the function, and the value is returned. The factorial function was chosen because it's a nice demo of recursion.

This XSL file does not actually use any of the input XML file. It will output the same results for any XML file you transform. It's not hard to apply the factorial function to numbers retrieved from XML elements, but I thought it less complicated to just ignore the specifics of the XML document structure.

BTW, if you're interested in XSL, the Apache Xalan project is the place to start.
Posted to "" by Chris Rathman on 9/6/00; 9:49:42 AM

Ehud Lamm - Re: Functions in XSL  blueArrow
9/6/2000; 2:43:32 PM (reads: 882, responses: 4)
A related language issue started bugging me.

Can you create reusable XSLT? Suppose you want code that takes an element, which may appear multiple times, and replaces it (e.g., for inclusion in HTML) with one instance randomly chosen. For example choosing a random song from a song archive. Can code that does this be even remotely reusable? Any chance of XSLT code libraries?

Ehud Lamm - Re: Functions in XSL  blueArrow
9/6/2000; 2:58:49 PM (reads: 892, responses: 0)
See this example too.

Chris Rathman - Re: Functions in XSL  blueArrow
9/6/2000; 6:29:38 PM (reads: 982, responses: 3)
Can you create reusable XSLT? ... Can code that does this be even remotely reusable? Any chance of XSLT code libraries?
You can xsl:import other stylesheets from within a stylesheet. This would allow you to set up some generic XSL scripts that can be imported for specific application scripts. So, if I wanted to set up a bunch of functions like factorial or erasthones, I would put them into a library stylesheet script and just import them as I needed.

As an aside, the imported templates take a lower precedence than in-line templates, so a name (or match) collision would always be in favor of the local definition (allowing you to override the library). The language even has the facility of calling the shadowed templates if necessary.

Not the best system in the world for re-use, but it's probably better than all them Cobol COPYLIBS. :)

Ehud Lamm - Re: Functions in XSL  blueArrow
9/7/2000; 1:28:23 PM (reads: 1011, responses: 2)
Include files are not enogh for real reusability...

I was thinking about the fact that the XSL is related to the DTD. So if I wanted something like what I outlined (to produce a random selection of some - unknown in advance - element), I guess it could be problematic.

Chris Rathman - Re: Functions in XSL  blueArrow
9/7/2000; 2:26:06 PM (reads: 1037, responses: 1)
Include files are not enogh for real reusability...
I'd agree that include files are the poor man's route to reusability but having done a bunch in T-SQL for Sybase and MS SQLServer which don't even go that far, kind of makes me appreciate them.

On a more general note, I think that with programming going the route of the web (which is where XSL kind of fits in) the quality of the tools and languages has been going down, not up.

I was thinking about the fact that the XSL is related to the DTD. So if I wanted something like what I outlined (to produce a random selection of some - unknown in advance - element), I guess it could be problematic.
Assuming you opt for validating the documents (as opposed to just ensuring well-formedness), then the DTD will always act as the constraint on the XSL output document. Of course, you could make your DTD be fairly open ended to handle as many different cases as possible, but this would not fit your definition of random.

++++++++++++++++++++++++++++++

On a side note, I did manage to write the OO Shape example in XSL to go along with a bunch of other languages. Thought about reporting it on the main page, but the code is quite contrived and really only suitable for recreational uses. (Not to mention that I have two XSL stories already up on the board). :-)

Ehud Lamm - Re: Functions in XSL  blueArrow
9/8/2000; 11:29:45 AM (reads: 1089, responses: 0)
Recreational links are great! I don't see any reason to ban them.


As to our discussion of reuse. I think maybe I am not making myself to clear. Let me be more specific.

Suppose you have an XML file that is basically a list of questions and answers (e.g., multiple choice exam items). You want code to pick a random selection of N questions (N being a parameter).

Can you have code that does this, naturally accepting the tag to match, that can be reused to other DTD (and select other elements)?

If such code can be reused, I'd expect to see XSLT code archives, that would let you do things like this. I can easily think of other examples: sorting, expanding the tree etc. Of course, we have tools doing these - but not pure XSLT.