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: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 9 May 2022 17:55:46 -0000
This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform";
 xmlns:xs="http://www.w3.org/2001/XMLSchema";>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates select="xs:element">
         <xsl:sort
select="count(xs:complexType[1]/xs:sequence[1]/xs:element)"
order="descending"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

When applied on the provided XML document:

<xs:choice xmlns:xs="http://www.w3.org/2001/XMLSchema";>
  <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>

produces the wanted result:

<xs:choice xmlns:xs="http://www.w3.org/2001/XMLSchema";>
   <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: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:choice>

Hope this helps.

Dimitre

On Mon, May 9, 2022 at 10:31 AM 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
> 
>
>

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