[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Best practice for typing?

Subject: Re: Best practice for typing?
From: "Imsieke, Gerrit, le-tex gerrit.imsieke@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 24 Nov 2019 10:40:46 -0000
Re:  Best practice for typing?
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@xxxxxxxxxxxxxxxx wrote:
On Sat, 2019-11-23 at 01:44 +0000, David Birnbaum djbpitt@xxxxxxxxx
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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.