Re: [xsl] Is XPath and/or XSLT designed such that I should never have to write special case code?

Subject: Re: [xsl] Is XPath and/or XSLT designed such that I should never have to write special case code?
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 22 Jun 2021 19:50:09 -0000
> Question #1: is XPath and/or XSLT designed such that I should never have
to write special case code?
>
> Question #2: If I find myself writing special case code, should I stop
and say, "How can I modify this XPath and/or XSLT so that I do not have to
write special case code?

Reminds me of:


   - Sentinel programming (See for example Programming Pearls, by J
   Bentley)
   - Null object pattern

Not sure support for sentinels is provided by any programming language.
This is more of an algorithmic feature.

Example: finding all (X)Html elements that have class $myClass:

      //*[contains(concat(' ', @class, ' '), concat(' ', $myClass, ' ')]

This avoids having to handle special cases like $myClass at the start, end
or middle of a space+ -separated classnames.

Cheers,
Dimitre

On Tue, Jun 22, 2021 at 10:03 AM Roger L Costello costello@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Hi Folks,
>
> I think special case code is evil.
>
> To explain what I mean by "special case code" let's take its opposite:
> code selects what is desired under any condition.
>
> So, by "special case code" I mean extra code that is written for dealing
> with special conditions.
>
> Last week I posted the following XPath expression to fetch the <Row>
> element where Cell[1]/Data equals $element and Cell[2]/Data equals $parent.
>
> $document/Row[Cell[1]/Data eq $element][Cell[2]/Data eq $parent]
>
> However, when $parent is empty, the XPath expression fails.
>
> I was all set to write special case code:
>
> <xsl:if test="empty(Cell[2]/Data)"> do something </xsl:if>
>
> Bad, bad, bad.
>
> But then Mukul showed me an XPath expression that works correctly --
> whether $parent is empty or not -- without any special case code:
>
> $document/Row[Cell[1]/Data eq $element][Cell[2]/string(Data) eq $parent]
>
> Awesome!
>
> Today I was writing some XSLT to generate a bunch of rows showing, for
> each element in an XML document, its name, the name of its parent, the name
> of its grandparent, and the name of its great-grandparent:
>
> <xsl:template match="*">
>     <row>
>         <element><xsl:value-of select="name(.)"/></element>
>         <parent-element><xsl:value-of select="name(..)"/></parent-element>
>         <grandparent-element><xsl:value-of
> select="name(../../..)"/></grandparent-element>
>         <great-grandparent-element><xsl:value-of
> select="name(../../../..)"/></great-grandparent-element>
>     </row>
>     <xsl:apply-templates select="*" />
> </xsl:template>
>
> Obviously as the XSLT traverses through an XML document some elements
> don't have a great-grandparent or a grandparent or even a parent. But I
> didn't need to write special case code to check those conditions. That is
> terrific!
>
> Question #1: is XPath and/or XSLT designed such that I should never have
> to write special case code?
>
> Question #2: If I find myself writing special case code, should I stop and
> say, "How can I modify this XPath and/or XSLT so that I do not have to
> write special case code?
>
> /Roger
> 
>
>

-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Current Thread