This is my XML Structure like this
input :-
<MYDATA>
<DETAILS>
<DESCRIPTION>EASE</DESCRIPTION>
</DETAILS>
<DETAILS>
<DESCRIPTION>COMPLEX</DESCRIPTION>
</DETAILS>
<DETAILS>
<DESCRIPTION>SIMPLE</DESCRIPTION>
</DETAILS>
</MYDATA>
I want to display like this using xsl sort it mean custom sort i want to display firts simple second ease and third complex
Output :-
<MYDATA>
<DETAILS>
<DESCRIPTION>SIMPLE</DESCRIPTION>
</DETAILS>
<DETAILS>
<DESCRIPTION>EASE</DESCRIPTION>
</DETAILS>
<DETAILS>
<DESCRIPTION>COMPLEX</DESCRIPTION>
</DETAILS>
</MYDATA>
Starting from Jose'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>
There is a generic way to solve the problem. What you need is to define a variable with the sorted list you want to use. Then you use a recusive call to show the elements by that order. Basicly you go by the elements of variable "sortOrder", applying then to a apply-template call that use that value the selects the node that 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With