Re: [xsl] Is there any benefit to creating a variable and specifying as="xs:string"?

Subject: Re: [xsl] Is there any benefit to creating a variable and specifying as="xs:string"?
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 9 Jun 2024 09:53:16 -0000
Am 09.06.2024 um 11:25 schrieb Martin Honnen martin.honnen@xxxxxx:
Am 09.06.2024 um 11:19 schrieb Roger L Costello costello@xxxxxxxxx:

I have this xsl:param at the top of my XSLT program:

<xsl:param name="ICAO" />

Is there any benefit to adding this "as" clause:

<xsl:param name="ICAO" as="xs:string"/>

That "as" clause says the ICAO param is a string value. Well,
everything is a string. These are all strings -- '123', 'true',
'http://www.example.com', '01101' -- all of which should not be used
to populate the ICAO param. It seems to me that adding the "as" clause
provides zero benefits. In fact, it has disadvantages: it makes the
code more verbose, it requires more typing. Do you agree that adding
the "as" clause provides no benefits?

Bonus question: The value to populate the ICAO param should be a
4-letter, uppercase value, such as KBOS. Is there anyway to express
that requirement/constraint in the "as" clause?

Perhaps in XSLT 4 B B as="enum('KBOS')" Of course an enumeration requires you to be able to list the possible values.


In Saxon 12.4 (tested with EE but should work with PE as well, I think)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="4.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  exclude-result-prefixes="#all"
  expand-text="yes">

<xsl:item-type name="ICAO-type" as="enum('KBOS')"/>

<xsl:param name="ICAO" as="ICAO-type"/>

  <xsl:template match="/" name="xsl:initial-template">
    <test>{$ICAO}</test>
  </xsl:template>

</xsl:stylesheet>


Passing params on the command line (for XSLT 4 I think you need --allowSyntaxExtensions:on) can then be done with e.g.

?ICAO="'KBS'"

giving

Type error on line 13 column 6 of param-as-enum-test3.xsl:
  XPTY0004  The required item type of the value of variable $ICAO is
enum("KBOS"); the
  supplied value "KBS" does not match.
In template xsl:initial-template on line 12 column 4 of
param-as-enum-test3.xsl:
The required item type of the value of variable $ICAO is enum("KBOS");
the supplied value "KBS" does not match.



If you pass e.g.

?ICAO="'KBOS'"

the value is accepted. Of course for a single value you probably don't
need an enum but I hope if you have a list of allowed values this helps.

Current Thread