qt4cg / qtspecs

QT4 specifications
https://qt4cg.org/
Other
28 stars 15 forks source link

add select attribute to xsl:text #323

Open liamquin opened 1 year ago

liamquin commented 1 year ago

Although xsl:text select="socks" would be the same as xsl:value-of select="socks" in implementation terms, users of XSLT 2 and later, even people who have been using XSLT 2 or 3 for some time, are often surprised to learn that xsl:value-of makes a text node, and that they need to use xsl:sequence to return something else.

So it'd be great to have them use xsl:text instead of xsl:value-of, where text nodes are wanted, because then introducing xsl:sequence is a small step.

Of course, beginners also often use value-of where they should be using apply-templates, e.g. to handle mixed content! But again, using xsl:text reduces that temptation.

We do have value templates now, { .... }</xsl:text>, which mitigates the need slightly, but i think only slightly, because the select= analogy is very compelling.

michaelhkay commented 1 year ago

Complementing this proposal, I would also propose addition of an "as" attribute to xsl:value-of.

If the "as" attribute of xsl:value-of is present, the effect is to take the "raw" result of the select expression or contained sequence constructor, and convert it to the required type using the coercion rules. This makes it possible to write

<xsl:function name="increment">
  <xsl:param name="x" as="xs:integer"/>
  <xsl:value-of select="$x + 1" as="xs:integer"/>
</xsl:function>

This avoids the awkwardness of using xsl:sequence for something that is always a singleton.

Note, the effect is NOT to construct a text node and then cast its value to the required type. That wouldn't work, for example if you want to return a sequence, a map, or an array. What the "as" attribute does is to suppress the construction of a text node.

This has strong similarities with its effect in xsl:variable, where the presence of an "as" attribute suppresses the construction of a document node.

The only slight complication is how this plays with the separator and disable-output-escaping attributes. I propose that these be disallowed if the as attribute is present.

We should probably recognise as="text()" as being equivalent to omitting the attribute (thus invoking the current behaviour).

liamquin commented 1 year ago

+1 to all of that last comment from Mike Kay.