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

Re: XPath expression to check that there are no inter

Subject: Re: XPath expression to check that there are no intervening elements?
From: "G. Ken Holman g.ken.holman@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 19 Jul 2016 16:16:28 -0000
Re:  XPath expression to check that there are no  inter
I like Mike's answer for XSLT 2.

Your XSLT 1 solution can be improved by reducing the number of counting actions. Your solution has three uses of count() ... this has only two:

  count(B[last()]/preceding-sibling::*[not(self::B)]) =

Avoiding the use of counting you could do the following:

  string( generate-id( B[1]/preceding-sibling::*[not(self::B)][1] ) ) =
  string( generate-id( B[last()]/preceding-sibling::*[not(self::B)][1] ) )

The use of string() is necessary in case the sequence of children starts with B.

Come to think of it, neither your answer, Mike's answer nor my answer will work if there are no B children at all. You will get a false positive. Does your test need to accommodate that situation?

. . . . . . . . Ken

At 2016-07-19 15:44 +0000, Costello, Roger L. costello@xxxxxxxxx wrote:
Hi Folks,

This XML has a solid block of <B> elements:


This XML has an intervening <C> element:


I need an XPath expression to return a Boolean value:

        Return true if there is one solid block of <B> elements
                (no intervening elements).
        Otherwise, return false.

I created a horrendous XPath expression to solve it:

count(B) eq (B[last()]/count(preceding-sibling::*)+1 - B[1]/count(preceding-sibling::*))

Can you provide a better (simpler, more efficient) XPath expression?


Check our site for free XML, XSLT, XSL-FO and UBL developer resources |
Streaming hands-on XSLT/XPath 2 training @US$45: http://goo.gl/Dd9qBK |
Crane Softwrights Ltd. _ _ _ _ _ _ http://www.CraneSoftwrights.com/s/ |
G Ken Holman _ _ _ _ _ _ _ _ _ _ mailto:gkholman@xxxxxxxxxxxxxxxxxxxx |
Google+ blog _ _ _ _ _ http://plus.google.com/+GKenHolman-Crane/posts |
Legal business disclaimers: _ _ http://www.CraneSoftwrights.com/legal |

--- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

Current Thread


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.
First Name
Last Name
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.