Re: [xsl] XPath expression to convert XSD enumerations into a regex, longest value first

Subject: Re: [xsl] XPath expression to convert XSD enumerations into a regex, longest value first
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 8 Jul 2022 02:48:29 -0000
> string-join(
>                     sort(/*/xs:restriction/xs:enumeration/@value,
>                            (),
 >                          function($s) {- Q{
http://www.w3.org/2005/xpath-functions/math}log10($s)}) ,
 >                   '|'
 >                )
>
>   This produces exactly the wanted result. Note: no reverse(), no
for/return :
>
>   12|11|10|9|8|7|6|5|4|3|2|1

And why not simply:

string-join(
                  sort(/*/xs:restriction/xs:enumeration/@value,
                         (),
                          function($n) {- $n)}) ,
                   '|'
                 )

Obviously, the reverse sort puts the longest numbers first. One needn't be
misled by a requirement such as: "the longest value (not the biggest value,
the string longest value) is listed first".
And if the enumeration's value space is not xs:integer as provided in the
original question, but can be any string, then simply:

string-join(
                  sort(/*/xs:restriction/xs:enumeration/@value,
                         (),
                          function($s) {- string-length($s))}) ,
                   '|'
                 )




On Thu, Jul 7, 2022 at 7:28 PM Dimitre Novatchev dnovatchev@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

>
>
> On Thu, Jul 7, 2022 at 6:57 PM Dimitre Novatchev dnovatchev@xxxxxxxxx <
> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
>> Hi Roger,
>>
>> > Well, I devised a solution, but it sure ain't simple.
>> >
>> >   string-join(for $i in reverse(sort(xs:restriction/xs:enumeration, (),
>> function($enum) {string-length($enum/@value )})) return $i/@value, '|')
>> >
>> >   Is there is a simpler XPath expression to solve this problem?
>>
>> I think this can be written in a good and readable way as below:
>>
>> string-join(
>>                   sort(/*/xs:restriction/xs:enumeration/@value,
>>                          (),
>>                           function($s) {- string-length($s)}) ,
>>                    '|'
>>                  )
>>
>
>
> The above produces:
> 10|11|12|1|2|3|4|5|6|7|8|9
>
>
> In case it is necessary the values to be in reverse order, I would use
> what I think is probably the best, simplest and most direct solution (it's
> a pity there is no default prefix for the "math" namespace):
>
> string-join(
>                   sort(/*/xs:restriction/xs:enumeration/@value,
>                          (),
>                           function($s) {- Q{
> http://www.w3.org/2005/xpath-functions/math}log10($s)}) ,
>                    '|'
>                  )
>
> This produces exactly the wanted result. Note: no reverse(), no for/return
> :
>
> 12|11|10|9|8|7|6|5|4|3|2|1
>
>
>> Thanks,
>> Dimitre
>>
>> On Thu, Jul 7, 2022 at 10:02 AM Roger L Costello costello@xxxxxxxxx <
>> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>>
>>> Hi Folks,
>>>
>>> I have an XSD simpleType with enumeration values:
>>>
>>> <xsd:simpleType name="RunwayLightingType">
>>>   <xsd:restriction base="xsd:string">
>>>    <xsd:enumeration value="1"/>
>>>    <xsd:enumeration value="2"/>
>>>    <xsd:enumeration value="3"/>
>>>    <xsd:enumeration value="4"/>
>>>    <xsd:enumeration value="5"/>
>>>    <xsd:enumeration value="6"/>
>>>    <xsd:enumeration value="7"/>
>>>    <xsd:enumeration value="8"/>
>>>    <xsd:enumeration value="9"/>
>>>    <xsd:enumeration value="10"/>
>>>    <xsd:enumeration value="11"/>
>>>    <xsd:enumeration value="12"/>
>>>   </xsd:restriction>
>>>  </xsd:simpleType>
>>>
>>> I want to turn the values into a regex such that the longest value (not
>>> the biggest value, the string longest value) is listed first:
>>>
>>> 12|11|10|9|8|7|6|5|4|3|2|1
>>>
>>> Seems like a simple problem, right? Should have a simple solution, right?
>>>
>>> Well, I devised a solution, but it sure ain't simple.
>>>
>>> string-join(for $i in reverse(sort(xs:restriction/xs:enumeration, (),
>>> function($enum) {string-length($enum/@value )})) return $i/@value, '|')
>>>
>>> Is there is a simpler XPath expression to solve this problem?
>>>
>>> /Roger
>>>
>>>
>>>
>> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
>> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/782854> (by
>> email)
>>
>
>
> --
> 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.
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/782854> (by
> email <>)
>


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