[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

problem with XPath count()

Subject: problem with XPath count()
From: "Wolfhart Totschnig wolfhart.totschnig@xxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 1 Nov 2023 04:40:02 -0000
 problem with XPath count()
Dear list,

There is a seemingly simple XPath problem to which I cannot find the solution. I'm hoping (or, rather, certain) that one of you can help me.

I have a list of <text> elements, like so:

<text>foo</text>
<text>foo</text>
<text>foo</text>
<text>foo</text>

I want to count, for each element, the number of preceding <text> elements that satisfy one or both of the following two conditions: 1) the element is the first of the list or 2) its string value begins with "x". Here is an example, with the expected number of the count after the element:

<text>foo</text>B B B B B B 0B B (no preceding <text> element that satisfies either of the conditions)
<text>xfoo</text>B B B B 1B B (one preceding <text> element thatB satisfies condition 1)
<text>foo</text>B B B B B B 2B B (one preceding <text> element thatB satisfies condition 1 + one preceding <text> element thatB satisfies condition 2)
<text>xfoo</text>B B B B B 2 B B (same as with the previous element)


I thought that it would be as simple as the following:

count(preceding::text[position() = 1 or substring(., 1, 1) = 'x'])

But this XPath expression does not give the expected result. It gives the following:

<text>foo</text>B B B  B B B  0
<text>xfoo</text>B B B  B  1
<text>foo</text>B B B  B B B  1
<text>xfoo</text>B B B B B  2

That is, on the third element it gives "1" whereas I would expect "2". What am I misunderstanding?

Thanks in advance for your help!
Wolfhart

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.