How to sort xsl ascending with zeros

I want to sort these items in ascending order, but with zero values, not the first one, which is xslt by default. I managed to do this, but I wonder if there is a better way. Here is my example.

<b> <c>2</c> <c>1</c> <c>3</c> <c></c> <c>15</c> <c>11</c> <c></c> <c>43</c> <c>4</c> </b> <xsl:template match="/"> <xsl:for-each select="b/c"> <xsl:sort select="node() = false()"/> <xsl:sort select="." data-type="number"/> Row<xsl:value-of select="position()"/>:<xsl:value-of select="."/> </xsl:for-each> </xsl:template> 

Which gives the required conclusion:

 Row1:1 Row2:2 Row3:3 Row4:4 Row5:11 Row6:15 Row7:43 Row8: Row9: 

I am currently using <xsl:sort select="node() = false()"/> to verify that it is zero, and then using sorting to order the last elements of zero (null will be 1, not null will be 0 , so he arranges them correctly).

Can anyone suggest anything better than this?

+4
source share
1 answer
 <xsl:template match="/"> <xsl:for-each select="b/c"> <xsl:sort select="concat( substring('1', 1, boolean(text())), substring('0', 1, not(boolean(text()))) )" /> <xsl:sort select="." data-type="number"/> <xsl:text>Row</xsl:text> <xsl:value-of select="position()"/> <xsl:text>:</xsl:text> <xsl:value-of select="."/> <xsl:text>&#10;</xsl:text> </xsl:for-each> </xsl:template> 

It:

 concat( substring('1', 1, boolean(text()) ), substring('0', 1, not(boolean(text()))) ) 

Produces either "0" or "1", depending on whether the child text node exists or not. This is a concatenation of two mutually exclusive lines - a poor person if / then / else in XPath 1.0.

boolean(text()) creates true or false , which is then converted to a number for substring() . Boolean values ​​are converted to 1 or 0, respectively.

A more complete version above is as follows:

 concat( substring( $if_str, 1, boolean($condition) * string-length($if_str) ), substring( $else_str, 1, not(boolean($condition)) * string-length($else_str) ) ) 
+4
source

All Articles