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: "Liam R. E. Quin liam@xxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 18 Feb 2021 21:12:19 -0000
On Thu, 2021-02-18 at 18:44 +0000, Wolfhart Totschnig
wolfhart.totschnig@xxxxxxxxxxx wrote:
> Hello again,
> 
> Thank you, Wendell!
> 
> The proposed solutions leave me with two questions:
> 
> 
> 1) Is there a way to do what I originally had in mind, i.e., go
> forward to the first non-"foo" sibling and from there go backwards,
> taking all the "foo" siblings up to the current node?B 

XPath doesn't actually "go" ...
> Asked differently, can the 
> following XPath expression be fixed?
> 
> following-sibling::*[not(self::source)][1]
OK, now we have either an empty sequence (we're finished, select
nothing) or we have a single element that's not called "source". I'll
call that element E

> /preceding-sibling::*[self::source]

We now have all the elements whose name is source and that come before
E.

E.g. given
  parent
   source id="a"
   sock
   beer
   source id="b"
   source id="c"
   start  (the original context node, let's say)
   source id="d" 
   source  id="e"
   source if="f"
   e
   source id="f"

the first expression found "e" and we have now found a b c d e f

> [preceding-sibling::current()]

I don't know what this is supposed to mean. I think it's the magic "do
the right thing" clause :)

preceding-sibling:: is an XPath axis and in XPath 1 must be followed by
a name.

In XPath 2 you could write [. << current] to mean "comes before".
In XPath 1 there isn't a built-in way to do that, so the normal trick
is to use count()

> 2) Liam's and Wendell's proposals involve the expression
> [name()=...], 
> whereas I have used [self:: ...]. Are these strictly equivalent, or
> is 
> there a reason to prefer the one over the other?

As others have said, you have to watch out for namespaces.
(local-name(.) = 'svg') and (namespace-uri(.) eq
'http://www.w3.org/2000/svg')
is something you see a lot in XPath 1 expressions, especially outside
of XSLT.

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