Code: Select all
<xsl:variable name="SortField" select="WorkData/_Parameters/_Sort/Field"/>
<xsl:variable name="SortOrder" select="WorkData/_Parameters/_Sort/Order"/>
<xsl:variable name="StartIndex" select="WorkData/_Parameters/_Page/FromRecord"/>
<xsl:variable name="EndIndex" select="WorkData/_Parameters/_Page/FromRecord + WorkData/_Parameters/_Page/NoOfRecords"/>
<xsl:variable name="OrderBy">
<xsl:choose>
<xsl:when test="$SortOrder = ''">ascending</xsl:when>
<xsl:when test="$SortOrder = 'ASC'">ascending</xsl:when>
<xsl:when test="$SortOrder = 'asc'">ascending</xsl:when>
<xsl:when test="$SortOrder = 'DESC'">descending</xsl:when>
<xsl:when test="$SortOrder = 'desc'">descending</xsl:when>
<xsl:otherwise>ascending</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test ="$SortField = 'UserName'">
<xsl:for-each select="WorkData/GetUsers/Output/Result">
<xsl:sort select="UserName" order="{$OrderBy}"/>
<xsl:if test="position() >= $StartIndex and position() <= $EndIndex">
<Result>
<UserName><xsl:value-of select="UserName"/></UserName>
<Age><xsl:value-of select="Age"/></Age>
<UserType><xsl:value-of select="UserType"/></UserType>
<Manager><xsl:value-of select="Manager"/></Manager>
<PhoneNumber><xsl:value-of select="PhoneNumber"/></PhoneNumber>
<EmailAddress><xsl:value-of select="EmailAddress"/></EmailAddress>
<Status><xsl:value-of select="Status"/></Status>
</Result>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:when test ="$SortField = 'Age'">
<xsl:for-each select="WorkData/GetUsers/Output/Result">
<xsl:sort select="Age" order="{$OrderBy}"/>
<xsl:if test="position() >= $StartIndex and position() <= $EndIndex">
<Result>
<UserName><xsl:value-of select="UserName"/></UserName>
<Age><xsl:value-of select="Age"/></Age>
<UserType><xsl:value-of select="UserType"/></UserType>
<Manager><xsl:value-of select="Manager"/></Manager>
<PhoneNumber><xsl:value-of select="PhoneNumber"/></PhoneNumber>
<EmailAddress><xsl:value-of select="EmailAddress"/></EmailAddress>
<Status><xsl:value-of select="Status"/></Status>
</Result>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:when test ="$SortField = 'UserType'">
<xsl:for-each select="WorkData/GetUsers/Output/Result">
<xsl:sort select="UserType" order="{$OrderBy}"/>
<xsl:if test="position() >= $StartIndex and position() <= $EndIndex">
<Result>
<UserName><xsl:value-of select="UserName"/></UserName>
<Age><xsl:value-of select="Age"/></Age>
<UserType><xsl:value-of select="UserType"/></UserType>
<Manager><xsl:value-of select="Manager"/></Manager>
<PhoneNumber><xsl:value-of select="PhoneNumber"/></PhoneNumber>
<EmailAddress><xsl:value-of select="EmailAddress"/></EmailAddress>
<Status><xsl:value-of select="Status"/></Status>
</Result>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:when test ="$SortField = 'Manager'">
<xsl:for-each select="WorkData/GetUsers/Output/Result">
<xsl:sort select="Manager" order="{$OrderBy}"/>
<xsl:if test="position() >= $StartIndex and position() <= $EndIndex">
<Result>
<UserName><xsl:value-of select="UserName"/></UserName>
<Age><xsl:value-of select="Age"/></Age>
<UserType><xsl:value-of select="UserType"/></UserType>
<Manager><xsl:value-of select="Manager"/></Manager>
<PhoneNumber><xsl:value-of select="PhoneNumber"/></PhoneNumber>
<EmailAddress><xsl:value-of select="EmailAddress"/></EmailAddress>
<Status><xsl:value-of select="Status"/></Status>
</Result>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:when test ="$SortField = 'PhoneNumber'">
<xsl:for-each select="WorkData/GetUsers/Output/Result">
<xsl:sort select="PhoneNumber" order="{$OrderBy}"/>
<xsl:if test="position() >= $StartIndex and position() <= $EndIndex">
<Result>
<UserName><xsl:value-of select="UserName"/></UserName>
<Age><xsl:value-of select="Age"/></Age>
<UserType><xsl:value-of select="UserType"/></UserType>
<Manager><xsl:value-of select="Manager"/></Manager>
<PhoneNumber><xsl:value-of select="PhoneNumber"/></PhoneNumber>
<EmailAddress><xsl:value-of select="EmailAddress"/></EmailAddress>
<Status><xsl:value-of select="Status"/></Status>
</Result>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:when test ="$SortField = 'EmailAddress'">
<xsl:for-each select="WorkData/GetUsers/Output/Result">
<xsl:sort select="EmailAddress" order="{$OrderBy}"/>
<xsl:if test="position() >= $StartIndex and position() <= $EndIndex">
<Result>
<UserName><xsl:value-of select="UserName"/></UserName>
<Age><xsl:value-of select="Age"/></Age>
<UserType><xsl:value-of select="UserType"/></UserType>
<Manager><xsl:value-of select="Manager"/></Manager>
<PhoneNumber><xsl:value-of select="PhoneNumber"/></PhoneNumber>
<EmailAddress><xsl:value-of select="EmailAddress"/></EmailAddress>
<Status><xsl:value-of select="Status"/></Status>
</Result>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:when test ="$SortField = 'Status'">
<xsl:for-each select="WorkData/GetUsers/Output/Result">
<xsl:sort select="Status" order="{$OrderBy}"/>
<xsl:if test="position() >= $StartIndex and position() <= $EndIndex">
<Result>
<UserName><xsl:value-of select="UserName"/></UserName>
<Age><xsl:value-of select="Age"/></Age>
<UserType><xsl:value-of select="UserType"/></UserType>
<Manager><xsl:value-of select="Manager"/></Manager>
<PhoneNumber><xsl:value-of select="PhoneNumber"/></PhoneNumber>
<EmailAddress><xsl:value-of select="EmailAddress"/></EmailAddress>
<Status><xsl:value-of select="Status"/></Status>
</Result>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="WorkData/GetUsers/Output/Result">
<xsl:if test="position() >= $StartIndex and position() <= $EndIndex">
<Result>
<UserName><xsl:value-of select="UserName"/></UserName>
<Age><xsl:value-of select="Age"/></Age>
<UserType><xsl:value-of select="UserType"/></UserType>
<Manager><xsl:value-of select="Manager"/></Manager>
<PhoneNumber><xsl:value-of select="PhoneNumber"/></PhoneNumber>
<EmailAddress><xsl:value-of select="EmailAddress"/></EmailAddress>
<Status><xsl:value-of select="Status"/></Status>
</Result>
</xsl:if>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
Alternate Suggested Format
I created the following format from the current one that, while a little more advanced, I believe is easier to read and make changes to.
- Summary of Changes:
- Simplified sorting direction choose logic
- EASYProcess:ToLower($SortOrder) could be replaced with translate($SortOrder, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')
- Added data-type to sorting configurable for each column
- I would expect them all to default to "text", but my example includes a "number" to show one of the reasons I added this
- Reworked XML structure as a single for-each and moved sort column choose logic to variable
- Simplified sorting direction choose logic
Code: Select all
<xsl:variable name="SortField" select="WorkData/_Parameters/_Sort/Field"/>
<xsl:variable name="SortOrder" select="WorkData/_Parameters/_Sort/Order"/>
<xsl:variable name="StartIndex" select="WorkData/_Parameters/_Page/FromRecord"/>
<xsl:variable name="EndIndex" select="WorkData/_Parameters/_Page/FromRecord + WorkData/_Parameters/_Page/NoOfRecords"/>
<xsl:variable name="OrderDirection">
<xsl:choose>
<xsl:when test="EASYProcess:ToLower($SortOrder) = 'desc'">descending</xsl:when>
<xsl:otherwise>ascending</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="OrderBy">
<xsl:choose>
<xsl:when test="$SortField = 'UserName'">UserNameValue</xsl:when>
<xsl:when test="$SortField = 'Age'">AgeValue</xsl:when>
<xsl:when test="$SortField = 'UserType'">UserTypeValue</xsl:when>
<xsl:when test="$SortField = 'Manager'">ManagerValue</xsl:when>
<xsl:when test="$SortField = 'PhoneNumber'">PhoneNumberValue</xsl:when>
<xsl:when test="$SortField = 'EmailAddress'">EmailAddressValue</xsl:when>
<xsl:when test="$SortField = 'Status'">StatusValue</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="SortType">
<xsl:choose>
<xsl:when test="$SortField = 'UserName'">text</xsl:when>
<xsl:when test="$SortField = 'Age'">number</xsl:when>
<xsl:when test="$SortField = 'UserType'">text</xsl:when>
<xsl:when test="$SortField = 'Manager'">text</xsl:when>
<xsl:when test="$SortField = 'PhoneNumber'">text</xsl:when>
<xsl:when test="$SortField = 'EmailAddress'">text</xsl:when>
<xsl:when test="$SortField = 'Status'">text</xsl:when>
<xsl:otherwise>text</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:for-each select="WorkData/GetUsers/Output/Result">
<xsl:sort select="*[name()=$OrderBy]" order="{$OrderDirection}" data-type="{$SortType}"/>
<xsl:if test="position() >= $StartIndex and position() <= $EndIndex">
<Result>
<UserName><xsl:value-of select="UserName"/></UserName>
<BirthDate><xsl:value-of select="Age"/></BirthDate>
<UserType><xsl:value-of select="UserType"/></UserType>
<Manager><xsl:value-of select="Manager"/></Manager>
<PhoneNumber><xsl:value-of select="PhoneNumber"/></PhoneNumber>
<EmailAddress><xsl:value-of select="EmailAddress"/></EmailAddress>
<Status><xsl:value-of select="Status"/></Status>
</Result>
</xsl:if>
</xsl:for-each>