[Top] [All Lists]

Re: [xsl] How do I pass the mode as a string?

2020-03-03 10:32:06
The two things that come closest to polymorphism in XSLT (3.0) are (a) the 
template rule despatch mechanism, and (b) higher order functions. Both of these 
can be used to achieve dynamic selection of different functionality based on 
the input data encountered. Exactly how you would apply either mechanism in 
your scenario depends on having a deeper understanding of your specific 
transformation requirement. Modes weren't designed for this requirement and so 
it's hard to give you a forwards directions that doesn't start "If I were you, 
I wouldn't start from here". If you do have to start from here (i.e. you've got 
a collection of interchangeable modes and want to select one of them 
dynamically) then I'm afraid you're stuck with some kind of xsl:choose 

Michael Kay

On 3 Mar 2020, at 15:38, Kerry, Richard 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Thanks all for the explanations.  I'll look at the alternatives.
If this was C++, which is what I use most of the time (XSL is infrequent, 
just when I want to manipulate some XML) I would use some kind of 
polymorphism to achieve this.  I'd define a couple of one-line functions with 
the different variables, or calls, and pass the appropriate one to the outer 
function so they could be run on its results (somewhat garbled, abbreviated 
explanation, sorry).  I'm not aware of anything in XSL that looks like 
polymorphism, but I would be interested to know if there was a construct that 
might be similar, or usable for cases like this one.
Granted I can't construct variable names dynamically, but I can have a number 
of variables existing simultaneously and select one and pass it around by 
address if I need to.  I don't see an analogous construct in XSL, except to 
use choose/when to select a line of code based on an actual variable.

I could send my examples if that would be best, but I think that would 
provide a lot of unnecessary code just to show a small bit of context.
What I am actually trying to do is to get a number of XML files processed.  
The files are in similar folders, called 'input' and 'output'.  The files' 
names follow a couple of name patterns, acq_(\d{4}) and out_(\d{4}). There is 
identical code to scan through the folders and identify the files I want to 
process (and extract the numbers from the filenames).  They are then passed 
to apply-templates with the different modes as mentioned.
So the file scanning, while not especially complicated, is not entirely 
trivial and I'd like to have it written just once.  And then, because the 
contents of the files differs in structure, they are handled by different 
templates, with different modes.

Richard Kerry
BNCS Engineer, SI SOL Telco & Media Vertical Practice
M: +44 (0)7812 325518
2nd Floor, MidCity Place, 71 High Holborn, London, WC1V 6EA


This e-mail and the documents attached are confidential and intended solely 
for the addressee; it may also be privileged. If you receive this e-mail in 
error, please notify the sender immediately and destroy it. As its integrity 
cannot be secured on the Internet, the Atos group liability cannot be 
triggered for the message content. Although the sender endeavours to maintain 
a computer virus-free network, the sender does not warrant that this 
transmission is virus-free and will not be liable for any damages resulting 
from any virus transmitted.

From: Michael Kay mike(_at_)saxonica(_dot_)com 
Sent: 03 March 2020 11:48
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com 
Subject: Re: [xsl] How do I pass the mode as a string?
Section §6.6.2 in the XSLT 3.0 spec states:

The xsl:apply-templates 
 element also has an optional mode attribute. The value of this attribute 
must be one of the following:

an EQName 
 which is expanded as described in 5.1.1 Qualified Names 
 to define the name of a mode
the token #default, to indicate that the default mode for the stylesheet 
 is to be used
the token #unnamed, to indicate that the unnamed mode 
 is to be used
the token #current, to indicate that the current mode 
 is to be used
It does not allow an expression, and it does not allow an attribute value 

It's similar to trying to refer to a variable by name: you can't construct 
variable names dynamically either, however much you feel it would make your 
life easier.

You haven't described your problem so it's difficult to suggest a different 
approach to solving it. Higher-order functions might be the right way.

Michael Kay

XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by 
email <>)
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
<Prev in Thread] Current Thread [Next in Thread>