Re: [xsl] XPath expression which checks that a string is between 1 and 10 characters in length?

Subject: Re: [xsl] XPath expression which checks that a string is between 1 and 10 characters in length?
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 26 Jul 2016 15:00:06 -0000
It's really impossible to argue about performance in the abstract. You can
assume a "naive implementation", but it's hard to know even how a naive
implementation will behave. Will it store strings in UTF16, in which case
string-length() takes linear time or in some representation where
string-length() executes in constant time?

(With Saxon, it depends on where the string comes from... See

http://dev.saxonica.com/blog/mike/2015/02/how-long-is-a-piece-of-string.html

)

And then there are optimizations. Some processors might extract the common
expression string-length(.) and only evaluate it once (Saxon doesn't). Some
might turn string-length(.) gt 0 into not-zero-length-string(.) - which avoids
counting characters (Saxon does).

Or as you'll see from the blog entry cited above, if you call string-length()
twice in Saxon on the same string then the second call is much faster.

string-length(.) = 1 to 10 is very dependent on optimization: Saxon turns it
effectively into in-range(string-length(.), 1, 10).

Finally, all these are going to execute in microseconds. It is extremely
unlikely that the performance of this expression has any effect whatsoever on
your bottom line. In fact, one thing we've been learning recently in Saxon is
that the optimizations we perform often take longer than any run-time saving
they achieve.

Michael Kay
Saxonica


> On 26 Jul 2016, at 12:51, Costello, Roger L. costello@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Hi Folks,
>
> I need an XPath expression which returns true if the string in <A> is
between 1 and 10 characters in length, and false otherwise.
>
> For example, the XPath expression should return true on this XML:
>
> 	<A>hi</A>
>
> Here's an inefficient XPath expression:
>
> (string-length(.) gt 0) and (string-length(.) le 10)
>
> It's inefficient because it computes the string length twice.
>
> Is there a more efficient XPath expression to solve this problem?
>
> /Roger

Current Thread