'How to list the parents nodes by the value of child nodes

I'm new to the XSLT and XML, Now I have a example of XML: (I'm trying to use XSLT 1.0 to solve the problem)

<Students>
    <student>
        <name>A</name>
        <class>C1</class>
        <sex>boy</sex>
    </student>
    
    <student>
        <name>B</name>
        <class>C2</class>
        <sex>girl</sex>
    </student>
    
    <student>
        <name>C</name>
        <class>C1</class>
        <sex>girl</sex>
    </student>
    
    <student>
        <name>D</name>
        <class>C2</class>
        <sex>boy</sex>
    </student>

</Students>

I want to divide the students by the value of the child node 'class', the output XML strutrues should belike:

<Students>

    <class name="C1">
        <Sex type="boy">
            <student>
                <name>A</name>
            </student>
        </Sex>
        
        <Sex type="girl">
            <student>
                <name>C</name>
            </student>
        </Sex>
    
    
    </class>
    
    <class name="C2">
        <Sex type="boy">
            <student>
                <name>D</name>
            </student>
        </Sex>
        
        <Sex type="girl">
            <student>
                <name>B</name>
            </student>
        </Sex>
    
    
    </class>

</Students>

Only XSLT1.0 could be used. Could anyone give me some advices? What kind of functions or tags in XSLT1.0 should I used?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source