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: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 26 Jul 2016 15:58:39 -0000
> boolean(string-length(.)[. > 0 and . < 11)])


This can even be shortened further to:

boolean(string-length()[.<11])


Here we use the fact that boolean(0) is false()




On Tue, Jul 26, 2016 at 7:23 AM, Dimitre Novatchev <dnovatchev@xxxxxxxxx> wrote:
> boolean(string-length(.)[. > 0 and . < 11)])
>
> On Tue, Jul 26, 2016 at 6:30 AM, Eliot Kimber ekimber@xxxxxxxxxxxx
> <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>> How about:
>>
>> if (string(.) != '') then string-length(.) le 10 else false()
>>
>> That requires casting the context to a string twice but calculating string-length() once.
>>
>> Or
>>
>> matches(., '..?.?.?.?.?.?.?.?')
>>
>> (Can I do that with '..?{0,10}'? Reviewing the syntax it looked like the quantifier could only be a single digit.)
>>
>> Cheers,
>>
>> Eliot
>>
>>> On July 26, 2016 at 7:01 AM "Wolfgang Laun wolfgang.laun@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>>>
>>>
>>> Just for fun:
>>>
>>> string-length() idiv 2 le 5
>>>
>>> Optimization by obfuscation. Phooey.
>>> -W
>>>
>>> On 26 July 2016 at 13:55, Martin Honnen martin.honnen@xxxxxx <
>>> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>>>
>>> > On 26.07.2016 13:51, Costello, Roger L. costello@xxxxxxxxx wrote:
>>> >
>>> > 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?
>>> >>
>>> >
>>> > Well, in XPath 3 you can use
>>> >
>>> >   let $l := string-length()
>>> >   return ($l gt 0 and $l le 10)
>>> >
>>> >
>>> >
>>>
>> 
>
>
>
> --
> 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.



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