xsl-list
[Top] [All Lists]

Re: [xsl] Is it possible to set the mode dynamically?

2020-01-07 07:57:29
Hi,

Steering clear of higher-order functions and indeed of the question as posted, 
one can work around the requirement by doing something entirely different.

For example, one could pass that file name in as a tunnel parameter and then 
reference it in templates lower down, without having to switch modes at the top.

Of course, YMMV but in many cases, this approach is more parsimonious of code 
than multiple modes, and it beats mode overloading for maintenance programmers 
who are baffled by modes – but to whom a tunnel parameter might be a Neat Thing.

Cheers, Wendell

From: Michael Kay mike(_at_)saxonica(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Tuesday, January 7, 2020 6:21 AM
To: xsl-list <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject: Re: [xsl] Is it possible to set the mode dynamically?

Another way would be to associate a function with each mode:

<xsl:function name="f:apply-pivot-table">
  <xsl:param name="select"/>
  <xsl:apply-templates select="$select" mode="pivot-table"/>
</xsl:function>

<xsl:function name="f:apply-default">
  <xsl:param name="select"/>
  <xsl:apply-templates select="$select" mode="#unnamed"/>
</xsl:function>

<xsl:variable name="apply" select="if ($table-name = 'pivot-table-payload') 
then f:apply-pivot-table#1 else f:apply-default#1"/>

and then replace the <xsl:apply-templates select="X"/> instruction with 
<xsl:sequence select="$apply(X)"/>

(But having introduced higher order functions, you might then find you can do 
everything with higher order functions and don't need template rule matching at 
all).

Michael Kay
Saxonica


On 7 Jan 2020, at 11:01, Martin Honnen 
martin(_dot_)honnen(_at_)gmx(_dot_)de<mailto:martin(_dot_)honnen(_at_)gmx(_dot_)de>
 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>>
 wrote:

Am 07.01.2020 um 11:51 schrieb rus tle 
profrustyleafiii(_at_)yahoo(_dot_)co(_dot_)uk<mailto:profrustyleafiii(_at_)yahoo(_dot_)co(_dot_)uk>:
Is it possible to set the mode dynamically?

The use case:

1. Setting a variable to a file name

<xsl:variable name="PayloadName" select="/POM/Netflix/@payload" />

2. Using that variable to load the file dynamically into another variable

<xsl:variable name="payload" select="document(concat($PayloadName, 
'.xml'))/payload”/>

3. Calling a template and passing the file name as as a param to be used in 
that called template as the mode.

 <xsl:call-template name="create-html-tables">
      <xsl:with-param name="PayloadName" select="$PayloadName”/>
</xsl:call-template>

 <xsl:template name="create-html-tables">
        <xsl:param name=“PayloadName"/>
       <xsl:apply-templates select="$pom//Event" mode=“$payloadName”/>
</xsl:template>

Is that a possibility in anyway shape or form…? Otherwise I am thinking the 
only alternative would be to use a choose and then hardcode the mode - which 
works, but just wondering if there was a cleaner way?


<xsl:choose>
    <xsl:when test="$PayloadName = 'pivot-table-payload'">
        <xsl:apply-templates select="$pom//Event" mode="pivot-table"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:apply-templates select="$pom//Event" mode="default"/>
    </xsl:otherwise>
</xsl:choose>

Many thanks,

Rusty

Using a shadow attribute in XSLT 3 it might work: 
https://www.w3.org/TR/xslt-30/#shadow-attributes<https://gcc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.w3.org%2FTR%2Fxslt-30%2F%23shadow-attributes&data=02%7C01%7Cwendell.piez%40nist.gov%7Cf04d0c9df108468d1f6a08d79363b2a4%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637139928716709329&sdata=3uq0FJDCG%2Bun4cvi5kh7ipZN4lpGmob9piQr3YDIbh0%3D&reserved=0>

The variable/param would need to be global and static.

Additionally there is fn:transform to run on the fly generated XSLT.

XSL-List info and 
archive<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.mulberrytech.com%2Fxsl%2Fxsl-list&data=02%7C01%7Cwendell.piez%40nist.gov%7Cf04d0c9df108468d1f6a08d79363b2a4%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637139928716714309&sdata=6hadtcL6PAWtO0NZDtULi%2FXVfALHXN3KQQg8mufzZTY%3D&reserved=0>
EasyUnsubscribe<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.mulberrytech.com%2Funsub%2Fxsl-list%2F293509&data=02%7C01%7Cwendell.piez%40nist.gov%7Cf04d0c9df108468d1f6a08d79363b2a4%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637139928716719286&sdata=6C2EkEEIxZSLfxRXkXEveWPC%2FHPE%2BxtLKkK50e3BsT4%3D&reserved=0>
 (by email)

XSL-List info and 
archive<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.mulberrytech.com%2Fxsl%2Fxsl-list&data=02%7C01%7Cwendell.piez%40nist.gov%7Cf04d0c9df108468d1f6a08d79363b2a4%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637139928716724264&sdata=BEId6qBBgQTci1F%2FKwBLxVW86YPFbmRWwC81X7kAWjE%3D&reserved=0>
EasyUnsubscribe<https://gcc01.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.mulberrytech.com%2Funsub%2Fxsl-list%2F3302254&data=02%7C01%7Cwendell.piez%40nist.gov%7Cf04d0c9df108468d1f6a08d79363b2a4%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637139928716729242&sdata=yYDoxb8a2gMSosow2kU%2B26zL1g8%2BAJfwoPVbIhlWBtg%3D&reserved=0>
 (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>