Re: [xsl] How to count the distinct values where elements with missing child are counted as one of the distinct values?

Subject: Re: [xsl] How to count the distinct values where elements with missing child are counted as one of the distinct values?
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 28 May 2022 01:43:53 -0000
Hi Wendell,

> <xsl:for-each-group select="/test/row" group-by="transition">
 >  <xsl:sequence select="count(current-group())"/>
> </xsl:for-each-group>


Roger was asking for XPath code, not for XSLT.

On Fri, May 27, 2022 at 11:09 AM Wendell Piez wapiez@xxxxxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Hi,
>
> Another question is whether one assumes that row elements with no
> transition should be counted together as a single value, or separately as
> distinct values (because undefined).
>
> The original spec I think may have been ambiguous as to this question. ("I
> want a count of the number of distinct transition values, where row
> elements with no transition element are counted as 1".)
>
> let $test := /test/row return count(distinct-values($test/transition)) +
> count($test[not(transition)])
>
> Also, although XPath was requested XSLT ain't so hard either
>
> <xsl:for-each-group select="/test/row" group-by="transition">
>   <xsl:sequence select="count(current-group())"/>
> </xsl:for-each-group>
>
> or some variant depending on your needs.
>
> Cheers, Wendell
>
>
>
>
>
>
> On Fri, May 27, 2022 at 11:10 AM Dimitre Novatchev dnovatchev@xxxxxxxxx <
> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
>> > Use count(distinct-values(/test/row/string(transition)))
>>
>> I would use the more obvious and self-explanatory:
>>
>> let $test := /test/row
>>   return
>>      count(distinct-values($test/transition)) + 1[$test[not(transition)]]
>>
>> If this XPath evaluation is initiated from an XSLT transformation and
>> XSLT 1.0 compatibility set to true (Specifying version = "1.0" but using an
>> XSLT 2.0 or later processor), one can use even:
>>
>>  count(distinct-values(/test/row/transition)) +
>> boolean(/test/row[not(transition)])
>>
>>
>> Thanks,
>> Dimitre
>>
>> On Thu, May 26, 2022 at 7:33 AM Michael Kay mike@xxxxxxxxxxxx <
>> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>>
>>> Use count(distinct-values(/test/row/string(transition)))
>>>
>>> string() applied to an empty sequence returns the zero-length string,
>>> whereas atomization applied to an empty sequence returns an empty sequence.
>>>
>>> Michael Kay
>>> Saxonica
>>>
>>> > On 26 May 2022, at 15:08, Roger L Costello costello@xxxxxxxxx <
>>> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>>> >
>>> > Hi Folks,
>>> >
>>> > The following XML document has two row elements with no transition
>>> element and two row elements with a transition element :
>>> >
>>> > <test>
>>> >    <row>
>>> >    </row>
>>> >    <row>
>>> >    </row>
>>> >    <row>
>>> >        <transition>ABC</transition>
>>> >    </row>
>>> >    <row>
>>> >        <transition>DEF</transition>
>>> >    </row>
>>> > </test>
>>> >
>>> > I want a count of the number of distinct transition values, where row
>>> elements with no transition element are counted as 1.
>>> >
>>> > So for the above XML document the count should return 3.
>>> >
>>> > This XPath seems to work:
>>> >
>>> > count(distinct-values(/test/row/transition)) +
>>> count(/test/row[not(transition)][1])
>>> >
>>> > But I am wondering if there is a better (simpler, less kludgy) XPath
>>> expression?
>>> >
>>> > /Roger
>>> >
>>> >
>>>
>>>
>>>
>>
>> --
>> 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/174322> (by
>> email)
>>
>
>
> --
> ...Wendell Piez... ...wendell -at- nist -dot- gov...
> ...wendellpiez.com... ...pellucidliterature.org... ...pausepress.org...
> ...github.com/wendellpiez... ...gitlab.coko.foundation/wendell...
> 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