[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Only child test

Subject: Re: [xsl] Only child test
From: Liam R E Quin <liam@xxxxxx>
Date: Thu, 10 Oct 2013 10:20:42 -0400

On Wed, 2013-10-09 at 21:38 -0700, Karl Stubsjoen wrote:
> Just curious how other's might approach a test to determine if the
> current node is *not* an only child. This works fine but it feels a
> little heavy to me.  Thoughts?
>                 <xsl:if test="(count(preceding-sibling::*) +
> count(following-sibling::*)) &gt; 1">
>                     <hr/>
>                 </xsl:if>

I'd write, preceding-sibling::* or following-sibling::*, probably,
because (for me) it's closest to conveying the idea.

I'd avoid "cleverness" like ../*[2] unless there were performance
problems, because I'd come back to that a year later and stare at it
wondering about that two. So rather than that I'd write a function like
has-siblings(.) as xs:boolean that could use count($input/../*[2]) > 1.

But my approach to programming is like a science experiment: try to make
as clear as possible what is done and why, on the grounds that it will
need not be changed later. This is not a mathematician's approach, who
will prove it correct (for the stated requirements and conditions at the
time) and move on with life :-)


Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org freenode/#xml

Current Thread