Re: [xsl] with XPath 1.0, select all following sibling elements of name "foo" up to the first non-"foo" element

Subject: Re: [xsl] with XPath 1.0, select all following sibling elements of name "foo" up to the first non-"foo" element
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 20 Feb 2021 01:17:06 -0000
Here is one working solution, using a key :half the way":

<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform";>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kPrecedingFoo" match="foo"
use="generate-id(following-sibling::*[not(self::foo)][1])"/>

  <xsl:template match="/*/*">
 Results for: <xsl:copy-of select="."/>
 <xsl:text>&#10;</xsl:text>
 <xsl:copy-of select=
 "
*key('kPrecedingFoo',
generate-id(following-sibling::*[not(self::foo)][1]))
[current()[self::foo] and not( count(current()/preceding-sibling::foo | .)
= count(current()/preceding-sibling::foo ) )]*
       "/>
 <xsl:text>&#10;=======================</xsl:text>
  </xsl:template>
</xsl:stylesheet>

When applied on this XML document, it produces (what I believe based on my
understanding of the question) the correct results:



 Results for: <bar ind="1"/>

=======================

 Results for: <foo ind="1"/>
<foo ind="1"/>
<foo ind="2"/>
<foo ind="3"/>
=======================

 Results for: <foo ind="2"/>
<foo ind="2"/>
<foo ind="3"/>
=======================

 Results for: <foo ind="3"/>
<foo ind="3"/>
=======================

 Results for: <bar ind="2"/>

=======================

 Results for: <foo ind="4"/>
<foo ind="4"/>
=======================

Cheers,
Dimitre

On Fri, Feb 19, 2021 at 5:02 AM Wolfhart Totschnig
wolfhart.totschnig@xxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
wrote:

> Hello,
>
> > Just eyeballing this, Dmitry, I'm not convinced it would work if
> > Wolfhart was sitting at a <foo> at the time he made his request, in
> > which case the <foo> he was at would incorrectly be included.
> >
> > Granted the original requirement is underspecified in this regard, so
> > perhaps my observation is irrelevant. I'm not trying to nit-pick but I
> > discounted the use of keys because of the arbitrary starting point for
> > evaluation.
> >
> > Wolfhart, am I being too pedantic here?
>
> I have never used <xsl:key>, so I cannot evaluate Dimitre's solution.
> But yes, the current element may be a <foo> in my case. Sorry for not
> making that clear in my original request!
>
> Wolfhart
> 
>
>

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