I hate to even ask this as there are plenty of examples with regard to the xsl:sort functionality however I’ve followed them and am still, inexplicably, stuck.
I have a sort within a for-each, as seems to be the requirement. I then wish to take all the sorted data and apply a template.
Here is a sample XML. I have a group of log elements and result elements, at separate paths, that I need sorted together based on a numeric time stamp value. In this example everything is already naturally sorted except the last element which has a numerictimestamp value greater than the three result elements meaning it should appear last in the output. As written, the output will display all log elements followed by all result elements in the order they appear in the input XML.
<output>
<logs>
<log logtext="Username = " loglevel="DEBUG" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428014" />
<log logtext="Password = " loglevel="DEBUG" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428027" />
<log logtext="ServerURI = " loglevel="DEBUG" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428042" />
<log logtext="TRACE TEST" loglevel="TRACE" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428084" />
<log logtext="DEBUG TEST" loglevel="DEBUG" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428096" />
<log logtext="INFO TEST" loglevel="INFO" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428109" />
<log logtext="WARNING TEST" loglevel="WARN" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428120" />
<log logtext="ERROR TEST" loglevel="ERROR" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428133" />
<log logtext="Post-Result INFO Test" loglevel="INFO" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428353" />
</logs>
<results>
<result name="Passed Test" resultformat="TEXT" resulttype="PASS" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428352" />
<result name="Failed Test" resultformat="TEXT" resulttype="FAIL" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428352" />
<result name="Running Processes" resultformat="TABLE" resulttype="NONE" timestamp="1/23/2012 5:04:28 PM" numerictimestamp="20120123170428352">
</result>
</results>
</output>
Here are the relevant parts of the XSL:
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="//logs/log | //results/result">
<xsl:sort data-type="number" select="@numerictimestamp"/>
<xsl:apply-templates select="self::node()" />
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template match="log">
.... handle formatting of the log elements
</xsl:template>
<xsl:template match="result">
.... handle formatting of the result elements
</xsl:template>
I guess this is a scope issue, that the sort is being applied only to the single item in the for-each, but I’m not sure what to change to resolve it and achieve the same result. With the exception of the sorting this XSL gives me exactly the output I desire.
EDIT1:
Updating question to include modified working code incorporating answers & comments below. The sort was failing due to the fact the numbers to be sorted were too large. They’re timestamp values that include milliseconds, specifically to be precise, so it is ironic that that precision was the cause of the issue. That said even with shorter numbers placing the sort within a for-each wasn’t actually sorting more than one item.
Most examples using xsl:sort do so within a for-each. I was unaware you could include it within the apply-templates.
The following modification works correctly: (Note that I explicitly specified the data type as text for clarity, I realize it is the default)
<xsl:apply-templates select="//logs/log | //results/result">
<!--The data-type value for the sort MUST be text, due to rounding errors introduced
attempting to sort the numerictimestamp-->
<xsl:sort data-type="text" select="@numerictimestamp"/>
</xsl:apply-templates>
I was able to keep the rest of the XSL unchanged.
Does this do what you want?
I think that you were correct that the for-each was reducing the scope to just the current item
The other thing that is happening is the converting your numerictimestamp attributes to numbers is causing rounding.
If I do this:
I see this:
As I understand things, the XSLT number data type is according to IEEE 754 and wikipedia tells me that double precision is accurate to 15.95 digits. Your numbers are 17 digits