xsl-list
[Top] [All Lists]

Re: [xsl] Best practice for typing?

2019-11-24 04:40:51
While it has been mentioned that it is generally advisable to add an as attribute to variable and parameter declarations, it might be useful to make novices aware of the fact that adding or omitting type information can change the nature of the beast that they are declaring.

Example:

<xsl:variable name="var1">
  <doc>
    <p/>
  </doc>
</xsl:variable>

$var1/*/name() gives 'doc'

<xsl:variable name="var2" as="element(*)">
  <doc>
    <p/>
  </doc>
</xsl:variable>

$var2/*/name() gives 'p'

This is because of implicit document node creation [1].

I've seen situations where XSLT developers didn't seem to know how to select every p from each of the two variable flavors. That's why they tend to write $var1//p and $var2//p indiscriminately, instead of $var1/doc/p and $var2/p. Not only are the latter expressions more efficient, but they also help avoid selecting nested p elements in this scenario:

<xsl:variable name="var">
  <doc>
    <p>text<fn>
      <p>footnote</p>
    </fn></p>
  </doc>
</xsl:variable>

Because of an uncertainty about what to expect when they select a child element of this variable, these people tend to write something like

$var//p[not(ancestor::p)]

in order to get only the outermost p elements, which is even more inefficient.


I must admit that if I really want to have a document node around the element, for example because I want to use fn:key() on the variable content in XSLT 2.0, I sometimes omit the as attribute deliberately. On other occasions, I would write

<xsl:variable name="var1" as="document-node(element(*))">
  <xsl:document>
    <doc>
      <p/>
    </doc>
  </xsl:document>
</xsl:variable>

instead of the $var1 declaration as seen above, just to make clear that I deliberately wanted the variable to hold a document.

Other people disagree [2], but I always try to teach the importance of the as attribute in the first lessons, and I keep telling novices that they should use xsl:sequence instead of xsl:value-of, unless they want to create a text node.

People with an XSLT1 upbringing frequently use xsl:value-of, and they spread this bad habit when they teach, alas.

Remember, the XDM sequence type system is your friend [3].

Gerrit

[1] https://www.w3.org/TR/xslt-30/#temporary-trees
[2] https://twitter.com/gimsieke/status/756810053613678592
[3] https://twitter.com/gimsieke/status/233936479917846528



On 24.11.2019 09:18, Liam R. E. Quin liam(_at_)fromoldbooks(_dot_)org wrote:
On Sat, 2019-11-23 at 01:44 +0000, David Birnbaum djbpitt(_at_)gmail(_dot_)com
wrote:

Is there a consensus about best practice with respect to possibly
redundant
typing? For example, we can specify a type using @as on corresponding
<xsl:param> and <xsl:with-param> elements, but if the type is
specified on
<xsl:param>, it seems as if that might make it redundant also to
specify it
on <xsl:with-param>,

Something not i think mentioned by others -

It helps to make your stylesheet resilient to change. Of course, it may
also increase the effort of making such changes, but the things you
need to change would in any case need review.

Liam

--~----------------------------------------------------------------
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>