Rui Barbosa Rui Barbosa - 5 months ago 8
HTML Question

XSLT sort by two elements; use second if first is repeated

Basically I have a XML with the following structure and I want to make a XSL Transformation from XML to HTML. The important thing about that is that I want to have a table with all the information about a program sorted by date. The catch is that, if the

date
elements are the same, you should sort by
time
. I have no idea how to pull that off.

<scheduling>
<program channel="FOX" season="9" episode="2">
<date>2013-06-17</date>
<time>23:50:00</time>
<duration>00:45:00</duration>
<name>CSI</name>
<resume>Something about a murder, idk...</resume>
</program >
... <!-- program can repeat -->
</scheduling>

Answer

The sort key specification allows for multiple xsl:sort elements for precisely this use case:

[Definition: Within a sort key specification, each xsl:sort element defines one sort key component.] The first xsl:sort element specifies the primary component of the sort key specification, the second xsl:sort element specifies the secondary component of the sort key specification and so on.

So, just specify a second xsl:sort for the time element:

<xsl:sort select="date"/>
<xsl:sort select="time"/>