XSLT Sort

jbenoit's Avatar

jbenoit

01 Apr, 2015 04:27 PM

I am trying to sort a faculty experts list by the Area of their expertise. I have found this discussion topic and am 90% there after customizing the provided code:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:key match="area" name="areas" use="."/>
    <xsl:template match="/system-index-block">
        <!-- Loop through each area in our key -->
        <xsl:for-each select="//system-page/system-data-structure/expert/area[count(. | key('areas', .)[1]) = 1]">
            <!-- Sort the areas alphabetically. -->
            <xsl:sort data-type="text" order="ascending" select="."/>
            
            <xsl:variable name="currentArea" select="."/>
            
            <!-- Output the value of the current area -->
            <h3><xsl:value-of select="$currentArea"/></h3>

            <!-- Display the info for each person containing the current area. -->
            <ul>
            <xsl:apply-templates select="//system-page[system-data-structure/expert/area[value=$currentArea]]">
                <xsl:sort data-type="text" select="system-data-structure/last"/>
            </xsl:apply-templates>
            </ul>
        </xsl:for-each>
    </xsl:template>
   
    <xsl:template match="system-page">
        <li><xsl:value-of select="system-data-structure/name"/></li>
    </xsl:template>
</xsl:stylesheet>

An example of the index XML:

<?xml version="1.0" encoding="UTF-8"?>
<system-index-block name="Experts Guide" type="content_type" current-time="1427903498167">
    <system-page id="0f0de39d899b100f5d8f794fb66e7428">
        <name>index</name>
        <display-name>Dr. Jana Adamitis </display-name>
        <path>/CNU Site/experts/adamitis/index</path>
        <system-data-structure definition-path="CNU Data Definitions/Experts Guide">
            <first>Jana</first>
            <last>Adamitis</last>
            <name>Dr. Jana Adamitis </name>
            <dept>Modern and Classical Languages and Literatures</dept>
            <building>- - -</building>
            <roomnum>- - -</roomnum>
            <email>- - -</email>
            <website />
            <degree>
                <level>Bachelors Degree</level>
                <ba-type>BA</ba-type>
                <ma-type />
                <phd-type />
                <otherDegree />
                <desc />
                <subject>Classical Studies</subject>
                <college>University of Pennsylvania</college>
            </degree>
            <degree>
                <level>Masters Degree</level>
                <ba-type />
                <ma-type>MA</ma-type>
                <phd-type />
                <otherDegree />
                <desc />
                <subject>Classical Studies</subject>
                <college>University of Pittsburgh</college>
            </degree>
            <degree>
                <level>Doctorate</level>
                <ba-type />
                <ma-type />
                <phd-type>PhD</phd-type>
                <otherDegree />
                <desc />
                <subject>Classical Studies</subject>
                <college>University of Pittsburgh</college>
            </degree>
            <expert>
                <area>Other</area>
                <otherTopic>Classical Studies</otherTopic>
                <topic>Latin Literature</topic>
                <topic>Ancient Roman Culture</topic>
                <publications>
                    <publicationTitle />
                    <publishDate />
                    <relatedInfo />
                </publications>
            </expert>
        </system-data-structure>
    </system-page>
</system-index-block>

The output I currently get is:

<h3 />
<ul />
<h3>Accounting, Economics and Finance</h3>
<ul />
<h3>Biology, Chemistry and Environmental Science</h3>
<ul />
<h3>Communication Studies</h3>
<ul />
<h3>English</h3>
<ul />
<h3>Fine and Performing Arts</h3>
<ul />
<h3>Government</h3>
<ul />
<h3>History</h3>
<ul />
<h3>Leadership and American Studies</h3>
<ul />
<h3>Management, Marketing and Business Law</h3>
<ul />
<h3>Modern and Classical Languages</h3>
<ul />
<h3>Other</h3>
<ul />
<h3>Philosophy and Religious Studies</h3>
<ul />
<h3>Physics, Computer Science and Engineering</h3>
<ul />
<h3>Psychology</h3>
<ul />
<h3>Sociology and Anthropology</h3>
<ul />
  1. 1 Posted by Eric L. Epps on 01 Apr, 2015 05:11 PM

    Eric L. Epps's Avatar

    I think you just need to take out the "value" in the XPath so this line:

    <xsl:apply-templates select="//system-page[system-data-structure/expert/area[value=$currentArea]]">

    becomes

    <xsl:apply-templates select="//system-page[system-data-structure/expert/area=$currentArea]">

  2. 2 Posted by jbenoit on 01 Apr, 2015 07:15 PM

    jbenoit's Avatar

    Absolutely perfect!

  3. jbenoit closed this discussion on 01 Apr, 2015 07:15 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM