RE: [xsl] A few questions to the experts: functions, templates, loops

Subject: RE: [xsl] A few questions to the experts: functions, templates, loops
From: Jarno.Elovirta@xxxxxxxxx
Date: Thu, 5 Sep 2002 07:37:56 +0300
Hi,

> 1.) How do I write my own functions with return values? For example, I
> have a function that traverses the tree, searches a page by id and
> delivers the corresponding path-name. My solution up until 
> now has been
> to create a template (please, don't laugh at me - I know this is most
> probably the worst possible approach) which delivers the name as text.
> This works, but... it's sort of.. akward

"It is not a laughing matter". (No one ever recognizes the quotes I use, sigh.) You can't define functions in XSLT 1.0, unless you use an extension mechanism, e.g. one described by the friendly folk at EXSLT <http://exslt.org>. Saxon 7.x partially implements XSLT 2.0 if you really really really want to define your own functions (in XSLT).

> 2.) I would like to have several templates matching e.g. "/test", but
> for different purposes. Since I have some apply-templates they tell me
> it's ambigous (which it of course is). Is there a way to elegantly
> parameterize this?

<http://www.w3.org/TR/xslt#modes>

> 3.) Is there a way to pass parameters when calling a xml-file? Like
> calling "http://...file.xml?param=1"; and the xslt-script can evaluate
> that parameter? I only found a way to do it via an xslt-processor, and
> since I can't use scripts/cgis/processors... on my server it has to be
> done via the xml/xslt purely.

Well... no, you have to use the API that your XSLT processor offers you.

> 4.) Sometimes I couldn't use templates, e.g. because of 
> ambigouty. Then
> I usually used the xsl:for-each statement to select the corresponding
> node (e.g. select="/pages/page"). But I don't need the
> loop-functionality - is there a simpler statement to do so?

To select nodes and process them? You could try to use different priorities <http://www.w3.org/TR/xslt#section-Defining-Template-Rules> on you templates, if that would work for you. Anyhow, xsl:for-each is no more a "loop" than using xsl:apply-templates and xsl:template is.

> 5.) Is there a way to "break out" of a loop?

If the "loop" is a recursive template, yes. If you're talking about xsl:for-each, no; xsl:for-each is not a loop. Try only selecting the nodes you want to process instead.

> 6.) How do I enter new data in an existing xml-file using saxon? I
> didn't find any short, easy examples on that. Can anyone point me to a
> source, please?

Use a buffer to output you serialized results and overwrite the original file.

Jarno

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread