[Top] [All Lists]

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

2020-03-03 05:48:08
Section §6.6.2 in the XSLT 3.0 spec states:

The xsl:apply-templates 
<https://www.w3.org/TR/xslt-30/#element-apply-templates> element also has an 
optional mode attribute. The value of this attribute must be one of the 

an EQName <https://www.w3.org/TR/xslt-30/#dt-eqname>, which is expanded as 
described in 5.1.1 Qualified Names <https://www.w3.org/TR/xslt-30/#qname> to 
define the name of a mode
the token #default, to indicate that the default mode for the stylesheet module 
<https://www.w3.org/TR/xslt-30/#dt-stylesheet-module> is to be used
the token #unnamed, to indicate that the unnamed mode 
<https://www.w3.org/TR/xslt-30/#dt-unnamed-mode> is to be used
the token #current, to indicate that the current mode 
<https://www.w3.org/TR/xslt-30/#dt-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

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

Normally I would specify apply-template's mode as a literal, eg:
<x:apply-templates mode="inputs" />

I have a number of instances of similar code which need to have different 
modes, eg:
<x:apply-templates mode="outputs" />

I'd like to extract all of this into a separate block that I will call with 
these two values.

<x:variable name="match-mode" >inputs</x:variable>
<x:apply-templates mode="$match-mode" />

Doesn't work.  Saxon reports:
Static error at x:apply-templates on line 47 column 74 of xxx.xsl:
  XTSE0020: Mode name {$match-mode} is not a valid QName

OK, so it's a QName, not a string.

So I try:
<x:variable name="match-mode" as="xs:QName" >inputs</x:variable>
<x:apply-templates mode="$match-mode" />

Again giving:
Static error at x:apply-templates on line 47 column 74 of AlarmsAllToDot.xsl:
  XTSE0020: Mode name {$match-mode} is not a valid QName

(xs is defined as xmlns:xs="http://www.w3.org/2001/XMLSchema"; )

So just telling it to be a QName doesn't seem to work.
I think this syntax for variable means 'inputs' is a text node, so what about:
<x:variable name="match-mode" as="xs:QName" select="'inputs'" />
<x:apply-templates mode="$match-mode" />

So do I need to do something more actively to generate a QName?
There is a QName function, so how about:

<x:variable name="match-mode" as="xs:QName" select="QName('', 'inputs')" />
<x:apply-templates mode="$match-mode" />

Giving me the same error.

So, where am I gong wrong?
The QName function takes two strings, the first being the namespace.  So is 
that wrong to be blank?
Is "inputs" in my namespace?  Or is it XSLT's?
Given that this will be fed directly to apply-templates, presumably it needs 
to be in XSLT's, so how about:

<x:variable name="match-mode" as="xs:QName" select="QName('x', 'inputs')" />
<x:apply-templates mode="$match-mode" />

(x is defined as   xmlns:x="http://www.w3.org/1999/XSL/Transform";)

Giving the same error.

Or is this about how it is presented to apply-templates?
Am I right to use mode="$match-mode", or should that be done differently?

So, I've tried a few options but I suspect there's something important I'm 
missing about QNames and the mode attribute.
Please can someone advise.


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.

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>