XSLT Alpha Grouping

jbenoit's Avatar

jbenoit

02 Apr, 2015 04:57 PM

I am trying to alpha-sort and alpha-group my faculty experts list, but each alpha grouping is duplicated the same number of faculty that are in that group, i.e. 4 faculty have last names that start with “B”, and there are 4 grouping for “B” with all 4 faculty in each.

XLST Format:


 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:include href="/CNU Formats/Utility/replace-concat"/>
    <xsl:variable name="site" select="'http://cnu.edu/'"/>
    <xsl:key name="LastNameFirstLetter" match="//system-index-block/system-page/system-data-structure" use="substring(last,1,1)" />
    <xsl:key name="keyLetters" match="//system-index-block/system-page/system-data-structure" use="substring(last,1,1)"/>

    <xsl:template match="system-index-block">
        <p>
        <xsl:for-each select="system-page/system-data-structure[count(. | key('LastNameFirstLetter', last)[1]) = 1]">
        <xsl:sort select="last" />
          <a href="#{substring(last,1,1)}"><xsl:value-of select="substring(last,1,1)" /></a><xsl:text> | </xsl:text>
       </xsl:for-each>
       </p>
       <xsl:for-each select="system-page/system-data-structure[count(. | key('LastNameFirstLetter', last)[1]) = 1]">
          <xsl:sort select="last" />
          <h3><a name="{substring(last,1,1)}"><xsl:value-of select="substring(last,1,1)" /></a></h3>
          <ul>         
          <xsl:for-each select="key('LastNameFirstLetter', substring(last,1,1))">
             <xsl:sort select="last" />
             <li>
             <a>
                 <xsl:attribute name="href">
                     <xsl:call-template name="replace-string-and-concat">
                         <xsl:with-param name="text">
                             <xsl:value-of select="../path"/>
                         </xsl:with-param>
                         <xsl:with-param name="replace" select="'/CNU Site/'"/>
                         <xsl:with-param name="with" select="$site"/>
                         <xsl:with-param name="theEnd" select="'.asp'"/>
                     </xsl:call-template>
                 </xsl:attribute>
                 <xsl:value-of select="name"/>
             </a>
             </li>
          </xsl:for-each>
          </ul>
       </xsl:for-each>
    </xsl:template>
 </xsl:stylesheet>

Example XML:


<?xml version="1.0" encoding="UTF-8"?>
<system-index-block name="Experts Guide" type="content_type" current-time="1427993004178">
    <system-page id="28fc5dc7899b100f5d8f794f7720b347">
        <name>index</name>
        <display-name>Dr. Brian Bradie </display-name>
        <path>/CNU Site/experts/bradie/index</path>
        <system-data-structure definition-path="CNU Data Definitions/Experts Guide">
            <first>Brian</first>
            <last>Bradie</last>
            <name>Dr. Brian Bradie</name>
            <dept>Mathematics</dept>
            <building>Luter Hall</building>
            <roomnum>353</roomnum>
            <email>bbradie</email>
            <website />
            <degree>
                <level>Bachelors Degree</level>
                <ba-type>BS</ba-type>
                <ma-type />
                <phd-type />
                <otherDegree />
                <desc />
                <subject>Mathematics and Computer Science</subject>
                <college>Clarkson University</college>
            </degree>
            <degree>
                <level>Masters Degree</level>
                <ba-type />
                <ma-type>MS</ma-type>
                <phd-type />
                <otherDegree />
                <desc />
                <subject>Applied Mathematics</subject>
                <college>Clarkson University</college>
            </degree>
            <degree>
                <level>Doctorate</level>
                <ba-type />
                <ma-type />
                <phd-type>PhD</phd-type>
                <otherDegree />
                <desc />
                <subject>Applied Mathematics</subject>
                <college>Clarkson University</college>
            </degree>
            <expert>
                <area />
                <otherTopic />
                <topic />
                <publications>
                    <publicationTitle />
                    <publishDate />
                    <relatedInfo />
                </publications>
            </expert>
        </system-data-structure>
    </system-page>
    <system-page id="2921bfe2899b100f5d8f794f088f9fdb">
        <name>index</name>
        <display-name>Prof Mary Best</display-name>
        <path>/CNU Site/experts/best/index</path>
        <system-data-structure definition-path="CNU Data Definitions/Experts Guide">
            <first>Mary</first>
            <last>Best</last>
            <name>Dr. Mary Best</name>
            <dept>Communication</dept>
            <building>Luter Hall</building>
            <roomnum>247</roomnum>
            <email>mdbest</email>
            <website />
            <degree>
                <level>Bachelors Degree</level>
                <ba-type>BA</ba-type>
                <ma-type />
                <phd-type />
                <otherDegree />
                <desc />
                <subject>Speech Communication</subject>
                <college>The Pennsylvania State University</college>
            </degree>
            <degree>
                <level>Masters Degree</level>
                <ba-type />
                <ma-type>MA</ma-type>
                <phd-type />
                <otherDegree />
                <desc />
                <subject>Communication Arts</subject>
                <college>William Paterson University</college>
            </degree>
            <expert>
                <area>Communication Studies</area>
                <otherTopic />
                <topic>Professional presentations for Informative, Commemorative, and Persuasive speeches</topic>
                <topic>Public speaking anxiety</topic>
                <topic>Use of language &amp; nonverbal cues</topic>
                <topic>Incorporation of visual aids</topic>
                <topic>Demographic assessment</topic>
                <topic>Interpersonal communication workforce skills</topic>
                <topic>Overview of self-image &amp; self-esteem</topic>
                <topic>Power &amp; argumentation issues</topic>
                <topic>Conflict resolution</topic>
                <topic>Verbal &amp; nonverbal messages</topic>
                <topic>Environment &amp; use of work space</topic>
                <topic>Optimism/Pessimism assessment</topic>
                <topic>Facilitation of professional survey instrument to determine workforce and/or social levels of optimism vs. pessimism</topic>
                <topic>Sharing of research findings regarding health and productivity</topic>
                <publications>
                    <publicationTitle />
                    <publishDate />
                    <relatedInfo />
                </publications>
            </expert>
        </system-data-structure>
    </system-page>
    <system-page id="2e7dc9fd899b100f5d8f794f913d7a96">
        <name>index</name>
        <display-name>Dr. Russell Burke </display-name>
        <path>/CNU Site/experts/burke/index</path>
        <system-data-structure definition-path="CNU Data Definitions/Experts Guide">
            <first>Russell</first>
            <last>Burke</last>
            <name>Dr. Russell Burke </name>
            <dept>Molecular Biology and Chemistry</dept>
            <building>Forbes Hall</building>
            <roomnum>2065</roomnum>
            <email>russell.burke</email>
            <website />
            <degree>
                <level>Bachelors Degree</level>
                <ba-type>BS</ba-type>
                <ma-type />
                <phd-type />
                <otherDegree />
                <desc />
                <subject>Marine Science and Environmental Science</subject>
                <college>Rider University</college>
            </degree>
            <degree>
                <level>Doctorate</level>
                <ba-type />
                <ma-type />
                <phd-type>PhD</phd-type>
                <otherDegree />
                <desc>Alternative Substrates for Oyster Reef Restoration</desc>
                <subject />
                <college>The College of William and Mary</college>
            </degree>
            <expert>
                <area>Biology, Chemistry and Environmental Science</area>
                <otherTopic />
                <topic>Large-scale native oyster restoration</topic>
                <topic>Living shorelines known as 'Biogenic Breakwaters'</topic>
                <topic>Multi-purpose oyster-fish reefs</topic>
                <publications>
                    <publicationTitle />
                    <publishDate />
                    <relatedInfo>Schulte DM, Burke RP (2014) Recruitment enhancement as an indicator of oyster restoration success in Chesapeake Bay. Ecological Restoration 32(4):434-440</relatedInfo>
                </publications>
                <publications>
                    <publicationTitle />
                    <publishDate />
                    <relatedInfo>Schulte DM, Burke RP, Lipcius RN (2009). Unprecedented restoration of a native oyster metapopulation. Science, 325, 1124-28</relatedInfo>
                </publications>
            </expert>
        </system-data-structure>
    </system-page>
</system-index-block>

Example Output:


<h3>
    <a name="B">B</a>
</h3>
<ul>
    <li>
        <a href="http://cnu.edu/experts/best/index.asp">Dr. Mary Best</a>
    </li>
    <li>
        <a href="http://cnu.edu/experts/bradie/index.asp">Dr. Brian Bradie</a>
    </li>
    <li>
        <a href="http://cnu.edu/experts/burke/index.asp">Dr. Russell Burke </a>
    </li>
</ul>
<h3>
    <a name="B">B</a>
</h3>
<ul>
    <li>
        <a href="http://cnu.edu/experts/best/index.asp">Dr. Mary Best</a>
    </li>
    <li>
        <a href="http://cnu.edu/experts/bradie/index.asp">Dr. Brian Bradie</a>
    </li>
    <li>
        <a href="http://cnu.edu/experts/burke/index.asp">Dr. Russell Burke </a>
    </li>
</ul>
<h3>
    <a name="B">B</a>
</h3>
<ul>
    <li>
        <a href="http://cnu.edu/experts/best/index.asp">Dr. Mary Best</a>
    </li>
    <li>
        <a href="http://cnu.edu/experts/bradie/index.asp">Dr. Brian Bradie</a>
    </li>
    <li>
        <a href="http://cnu.edu/experts/burke/index.asp">Dr. Russell Burke </a>
    </li>
</ul>

What am I missing?

  1. 1 Posted by Ryan Griffith on 02 Apr, 2015 09:18 PM

    Ryan Griffith's Avatar

    Hi,

    Your are definitely on the right track with using <xsl:key>. Here is what I generally use, which makes for a clean and easy to follow Format:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output encoding="UTF-8" indent="yes" method="xml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
    
        <!-- Define our key for lookups while looping. -->
        <xsl:key name="letters" match="system-page" use="substring(system-data-structure/last,1,1)" />
        
        <xsl:template match="/system-index-block">
            <!-- Loop through each location in our key -->
            <xsl:for-each select="//system-page">
                <xsl:variable name="firstLetter" select="substring(system-data-structure/last, 1, 1)" />
                <xsl:variable name="pages" select="key('letters', $firstLetter)" />
                <xsl:if test="generate-id() = generate-id($pages[1])">
                    <h3><a name="{$firstLetter}"><xsl:value-of select="$firstLetter"/></a></h3>
                    <ul>
                      <xsl:apply-templates select="$pages">
                        <xsl:sort select="name" />
                      </xsl:apply-templates>
                    </ul>
                  </xsl:if>
            </xsl:for-each>
        </xsl:template>
        
        <xsl:template match="system-page">
            <li>
                <a href="{path}"><xsl:value-of select="display-name"/></a>
            </li>
        </xsl:template>
    </xsl:stylesheet>
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by jbenoit on 06 Apr, 2015 01:25 PM

    jbenoit's Avatar

    Ryan,

    It worked perfectly! Thanks!

  3. jbenoit closed this discussion on 06 Apr, 2015 01:25 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