Quercus Live Link Developer’s Guide<>

Appendix 2 — sample XSLT

Course instances and services

This XSLT renders Link Link data as shown in the screenshot Course instances and services

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" encoding="UTF-8"/>
  <xsl:template match="instances">
    <html>
      <head>
        <title>Course instances and services</title>
        <style type="text/css">
        table {font:0.8em sans-serif; width:30em; border-collapse:collapse;
        margin-left: auto; margin-right: auto;}
        td {padding-bottom:1em; text-align:left; vertical-align:top;}
       .instance td {font-weight:bold;padding-top:2em; border-bottom:1px solid #800000;}
       .service td {padding-top:1em; border-bottom:1px dashed #800000;}
        small {color:gray}</style>
      </head>
      <body>
        <table>
          <xsl:for-each select="courseInstance">
            <xsl:sort select="@courseCode" />
            <xsl:sort select="@locationDescription" />
            <xsl:sort select="@session" />
            <xsl:sort select="@instanceCode" />
            <tr class="instance">
              <td colspan="2">
                <xsl:value-of select="@courseDescription" />
                <xsl:value-of select="', '" />
                <xsl:value-of select="@instanceCode" />
                <xsl:if test="@session">
                  <xsl:value-of select="', '" />
                </xsl:if>
                <xsl:value-of select="@session" />
                <xsl:if test="@locationDescription">
                  <xsl:value-of select="', '" />
                </xsl:if>
                <xsl:value-of select="@locationDescription" />
              </td>
            </tr>
            <xsl:for-each select="service">
              <xsl:sort select="@serviceName" />
              <tr class="service">
                <td>
                  <xsl:value-of select="'service cis: '" />
                  <xsl:value-of select="@cis"/>
                </td>
                <td>
                  <a href="{substring-before(@url,'&quot;')}">
                    <xsl:value-of select="@serviceName"/>
                  </a>
                </td>
              </tr>
            </xsl:for-each>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="response"/>
</xsl:stylesheet>

Services grouped by course

These XSLT stylesheets render Link Link data as shown in the screenshot Services grouped by course

Note: the transformations both produce the same HTML output illustrated in Services grouped by course. However, the more straightforward XSLT 2.0 version will not work if using a browser-side XSLT transformation, as current-generation browsers do not support XSLT 2.0. It requires an XSLT 2.0-compatible processor such as Saxon (http://saxon.sourceforge.net/)

XSLT version 1.0 (uses grouping by key)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" encoding="UTF-8"/>
  <xsl:key name="courseInstance-by-courseCode" match="courseInstance" use="@courseCode"/>
  <xsl:template match="instances">
    <html>
      <head>
        <title>Services grouped by course</title>
        <style type="text/css">
        table {font:0.8em sans-serif; width:30em; border-collapse:collapse;
        margin-left: auto; margin-right: auto;}
        th, td {padding-bottom:1em; text-align:left; vertical-align:top;}
        th {padding-top:2em; border-bottom:1px solid #800000;}
        td {padding-top:1em; border-bottom:1px dashed #800000;}
        small {color:gray}</style>
      </head>
      <body>
        <table>
          <xsl:for-each select="courseInstance[count(. | key('courseInstance-by-courseCode',
@courseCode)[1]) = 1]">
            <xsl:sort select="@courseCode" />
            <tr><th colspan="2">
                <xsl:value-of select="@courseCode" />
              </th></tr>
            <xsl:for-each select="key('courseInstance-by-courseCode', @courseCode)">
              <xsl:sort select="@instanceCode" />
              <tr><td width="40%">
                  <xsl:value-of select="@instanceCode" />
                  <xsl:value-of select="' (Yr '"/>
                  <xsl:value-of select="@courseYear" />
                  <xsl:value-of select="')'"/>
                </td>
                <td width="60%">
                  <xsl:for-each select="service">
                    <xsl:sort order="ascending" select="@serviceName"/>
                    <p><a href="{substring-before(@url,'&quot;')}">
                        <xsl:value-of select="@serviceName"/></a><small>
                        <xsl:value-of select="' start:'"/>
                        <xsl:value-of select="concat(substring(@startDate, 9,2), '-',
substring(@startDate, 6,2), '-', substring(@startDate,1,4))"/>
                        <xsl:if test="@endDate">
                          <xsl:value-of select="' end:'"/>
                          <xsl:value-of select="concat(substring(@endDate, 9,2), '-',
substring(@endDate, 6,2), '-', substring(@endDate,1,4))"/>
                        </xsl:if></small>
                    </p>
                  </xsl:for-each>
                </td>
              </tr>
            </xsl:for-each>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="response"/>
</xsl:stylesheet>

XSLT version 2.0 (uses for-each-group)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="html" encoding="UTF-8"/>
  <xsl:template match="instances">
    <html>
      <head>
        <title>Services grouped by course</title>
        <style type="text/css">
        table {font:0.8em sans-serif; width:30em; border-collapse:collapse;
        margin-left: auto; margin-right: auto;}
        th, td {padding-bottom:1em; text-align:left; vertical-align:top;}
        th {padding-top:2em; border-bottom:1px solid #800000;}
        td {padding-top:1em; border-bottom:1px dashed #800000;}
        small {color:gray}</style>
      </head>
      <body>
        <table>
          <xsl:for-each-group select="courseInstance" group-by="@courseCode">
            <xsl:sort select="@courseCode" />
            <tr>
              <th colspan="2">
                <xsl:value-of select="@courseCode"/>
              </th>
            </tr>
            <xsl:for-each select="current-group()">
              <xsl:sort select="@instanceCode" />
              <tr>
                <td width="40%">
                  <xsl:value-of select="@instanceCode" />
                  <xsl:value-of select="' (Yr '"/>
                  <xsl:value-of select="@courseYear" />
                  <xsl:value-of select="')'"/>
                </td>
                <td width="60%">
                  <xsl:for-each select="service">
                    <xsl:sort order="ascending" select="@serviceName"/>
                    <p>
                      <a href="{substring-before(@url,'&quot;')}">
                        <xsl:value-of select="@serviceName"/>
                      </a>
                      <small>
                        <xsl:value-of select="' start:'"/>
                        <xsl:value-of select="concat(substring(@startDate, 9,2), '-',
substring(@startDate, 6,2), '-', substring(@startDate,1,4))"/>
                        <xsl:if test="@endDate">
                          <xsl:value-of select="' end:'"/>
                          <xsl:value-of select="concat(substring(@endDate, 9,2), '-',
substring(@endDate, 6,2), '-', substring(@endDate,1,4))"/>
                        </xsl:if>
                      </small>
                    </p>
                  </xsl:for-each>
                </td>
              </tr>
            </xsl:for-each>
          </xsl:for-each-group>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="response"/>
</xsl:stylesheet>