Re: [xsl] How to sort based on the number of child elements?

Subject: Re: [xsl] How to sort based on the number of child elements?
From: "David Carlisle d.p.carlisle@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 9 May 2022 17:56:52 -0000
why can't you use xsl:sort?

<xs:choice xmlns:xs="data:,whatever">
    <xs:element name="MilitaryDayTime">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Day" type="xs:string"/>
                <xs:element name="HourTime" type="xs:string"/>
                <xs:element name="MinuteTime" type="xs:string"/>
                <xs:element name="TimeZone" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="DateTimeGroup">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Day" type="xs:string"/>
                <xs:element name="HourTime" type="xs:string"/>
                <xs:element name="MinuteTime" type="xs:string"/>
                <xs:element name="TimeZone" type="xs:string"/>
                <xs:element name="MonthName" type="xs:string"/>
                <xs:element name="Year" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:choice>


sorted with

<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xs="data:,whatever">

 <xsl:template match="xs:choice">
  <xsl:for-each select="*">
   <xsl:sort select="-count(.//xs:element)"/>
   <xsl:text>&#10;</xsl:text>
   <xsl:copy-of select="."/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

On Mon, 9 May 2022 at 18:31, Roger L Costello costello@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Hi Folks,
>
> I have an XML Schema that contains a xs:choice. I want to sort the
> branches of the choice, in longest-to-shortest order.
>
> Here is a xs:choice with two branches:
>
> <xs:choice>
>     <xs:element name="MilitaryDayTime">
>         <xs:complexType>
>             <xs:sequence>
>                 <xs:element name="Day" type="xs:string"/>
>                 <xs:element name="HourTime" type="xs:string"/>
>                 <xs:element name="MinuteTime" type="xs:string"/>
>                 <xs:element name="TimeZone" type="xs:string"/>
>             </xs:sequence>
>         </xs:complexType>
>     </xs:element>
>     <xs:element name="DateTimeGroup">
>         <xs:complexType>
>             <xs:sequence>
>                 <xs:element name="Day" type="xs:string"/>
>                 <xs:element name="HourTime" type="xs:string"/>
>                 <xs:element name="MinuteTime" type="xs:string"/>
>                 <xs:element name="TimeZone" type="xs:string"/>
>                 <xs:element name="MonthName" type="xs:string"/>
>                 <xs:element name="Year" type="xs:string"/>
>             </xs:sequence>
>         </xs:complexType>
>     </xs:element>
> </xs:choice>
>
> The first branch is an element with 4 child elements. The second branch is
> an element with 6 child elements. So sorting the branches
> longest-to-shortest will result in reversing the order of the branches.
>
> I cannot use xsl:sort for this, right?
>
> Is there an easy solution to this task?
>
> /Roger

Current Thread