CUSTOM SORT XSL?

This is my XML structure like this

:

<MYDATA> <DETAILS> <DESCRIPTION>EASE</DESCRIPTION> </DETAILS> <DETAILS> <DESCRIPTION>COMPLEX</DESCRIPTION> </DETAILS> <DETAILS> <DESCRIPTION>SIMPLE</DESCRIPTION> </DETAILS> </MYDATA> 

I want to display similar using xsl sort. This means that custom sorting I want to display software simple simple second and third complex.

Exit: -

 <MYDATA> <DETAILS> <DESCRIPTION>SIMPLE</DESCRIPTION> </DETAILS> <DETAILS> <DESCRIPTION>EASE</DESCRIPTION> </DETAILS> <DETAILS> <DESCRIPTION>COMPLEX</DESCRIPTION> </DETAILS> </MYDATA> 
+4
source share
3 answers

There is a general way to solve the problem. You need to define a variable with a sorted list that you want to use. Then you use the call-up call to display the items for this order. Basically, you go through the elements of the sortOrder variable, then applying to the apply-template call that uses this value, selects the node you need.

  <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="sortOrder">EASE|SIMPLE|COMPLEX|</xsl:variable> <xsl:template match="/"> <MYDATA> <xsl:call-template name="SortElements"> <xsl:with-param name="sortList" select="$sortOrder"/> </xsl:call-template> </MYDATA> </xsl:template> <xsl:template name="SortElements"> <xsl:param name="sortList"/> <xsl:variable name="element" select="substring-before ($sortList, '|')"/> <xsl:if test="$element != ''"> <xsl:apply-templates select="/MYDATA/DETAILS [DESCRIPTION = $element]"/> <xsl:call-template name="SortElements"> <xsl:with-param name="sortList" select="substring-after ($sortList, '|')"/> </xsl:call-template> </xsl:if> </xsl:template> <xsl:template match="DETAILS"> <DETAILS> <DESCRIPTION> <xsl:value-of select="DESCRIPTION"/> </DESCRIPTION> </DETAILS> </xsl:template> </xsl:stylesheet> 
+3
source

Starting with Hose's idea, here is something with less code:

 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="DifficultyLevel">EASE|SIMPLE|COMPLEX|</xsl:variable> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="MYDATA"> <xsl:apply-templates select="@* | node()"> <xsl:sort order="ascending" select="string-length(substring-before($DifficultyLevel, DETAILS/DESCRIPTION))"/> </xsl:apply-templates> </xsl:template> </xsl:stylesheet> 
+3
source

If you know what all the possible values ​​you could do, you could make a series of apply-templates calls specific to the conditions:

 <xsl:apply-templates select="Details[Description = 'Simple']" /> <xsl:apply-templates select="Details[Description = 'Ease']" /> <xsl:apply-templates select="Details[Description = 'Complex']" /> 

So, if you know in what order you want them, and potential parameters will not change, it will output them in any order in which your apply-templates rules will be set.

Of course, if it's more complicated, you might consider keeping the sort order on the data side and including it in your XSL so you can just sort it.

+1
source

All Articles