<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> </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) ) )
source share