CUSTOM SORT XSL?
This is my XML Structure like this
input :-
<MYDATA>
<DETAILS>
<DESCRIPTION>EASE</DESCRIPTION>
</DETAILS>
<DETAILS>
开发者_StackOverflow<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 know what all the possible values you could do, you could do a series of condition-specific apply-templates
calls:
<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 what order you want them in and the potential options aren't going to change, it will output them in whatever order your apply-templates
rules are set.
Of course, if it's more complicated than that, you might consider storing sort order on the data side and include that in your XSL so you can just sort by that.
精彩评论