Re: [xsl] How to never make mistakes when coding?

Subject: Re: [xsl] How to never make mistakes when coding?
From: "Liam R. E. Quin liam@xxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 1 Mar 2025 20:38:41 -0000
On Sat, 2025-03-01 at 14:49 +0000, Roger L Costello costello@xxxxxxxxx
wrote:
>
> <xsl:for-each select="/root/record[position() gt 2">
>
> See the mistake?
>
> I should have written position() ge 2 or position() gt 1

What i noticed first was a missing ] at the end of the expression :-)

We canbt prevent ourselves from making mistakes. A very few people can
write a program and have it run without flaws immediately; Christopher
Strachey (an early computer scientist) was said to be the first person
on record to do that with a program of any size, which shows how rare
it is.

So we have to try and catch our errors.

One way to do this is with assertions - not to be confused with
xsl:assert instructions, which are one way to implement assertions.

Often, i would write things such as

<xsl:variable name="to-process" as="element(record)*"
   select="/root/record[position() gt 2]" />

<xsl:message>Records to process: {count($to-process out of
{count(//root/record)}</xsl:message>

and of course gradually remove these.

Another approach is to use xsl:if and message terminate="yes", or
xsl:assert.

A danger of using xsl:assert is that the user running the stylesheet
can turn them off, and if the expressions have side-effects in any way
they are running something different from what you tested.

The same is true of using the use-when attribute to disable the tests.

On the other hand, progress messages with embedded values can often be
kept, or you can have a verbosity paramater to control which are
printed (again being careful to avoid side-effects other than printing
the message), and there is a huge advantage to them:

<xsl:assert test="$to-process[1] is $input[3]">
 Second item is not second item
</xsl:assert>

will not detect the error, because it repeats the same mistake.

So it will catch some classes of error and not others.


There are then unit tests, e.x. with xspec, to test a single template.

And then there is checking the output.

It can also help to declare the typoe of the expected context, and of
course use as= attributes everywhere they are allowed.

And it helps to avoid doing what i did this past week or so, and do
things like technical writing or programming or other tasks when youbre
unwell and exhausted :-)

There _is_ (i know webve talked about this) scientific evidence based
on large-scale studies (30,000 people over ten years in one study) that
people do better at mental tasks when not wearing shoes, and earlier
studies regarding drinking water being beneficial. Our environment,
what we wear, whether webre hungry, lots of things affect our
performance and should not be disregarded. As COb levels increase, the
nutritional value of plants is reduced, and vitamin and mineral
supplements may make a bigger different too.

liam

--
Liam Quin,B https://www.delightfulcomputing.com/
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processing/A11Y training, work & consulting.
Barefoot Web-slave, antique illustrations: B http://www.fromoldbooks.org

Current Thread