XSLT Cookbook

This document provides some examples of useful XPath expressions which can be used to process treetriples.

Finding a subject with a given URI

<!-- NOTE this expression is guaranteed to only return one node -->
<xsl:for-each select="/tt:rdf/tt:s[@id = 'http://example.com/thing']">
...
</xsl:for-each>

Getting the literal values of properties of the current statement

<xsl:for-each select="/tt:rdf/tt:s[@id = 'http://example.com/thing']">
  <h1>
    <xsl:value-of select="tt:p[@id = '&dc;title']/tt:o" />
  </h1>
  <p>
    <xsl:value-of select="tt:p[@id = '&dc;description]/tt:o" />
  </p>
</xsl:for-each>

Finding subjects by type

<xsl:for-each select="/tt:rdf/tt:s[tt:p[@id = '&rdf;type' and tt:o/@id = '&foaf;Person' ]]">
...
</xsl:for-each>

This technique can also be used anywhere where it is necessary to find the subject of a triple where the predicate and/or object are known.

Getting information about a related resources

<xsl:for-each select="/tt:rdf/tt:s[@id = 'http://example.com/thing']">
  <ul>
    <xsl:for-each select="tt:p[@id = '&foaf:knows']/tt:o" />
      <xsl:variable name="ref" select="@id" />
      <li>
        <!-- use [1] as we are only interested in the first nickname -->
        <xsl:value-of select="/tt:rdf/tt:s[@id = $ref]/tt:p[@id = '&foaf;nick']/tt:o[1]">
      </li>
    </xsl:for-each>
  </ul>
</xsl:for-each>

...or, more concisely:

<xsl:for-each select="/tt:rdf/tt:s[@id = 'http://example.com/thing']">
  <ul>
    <xsl:for-each select="tt:p[@id = '&foaf:knows']/tt:o" />
      <li>
        <xsl:value-of select="/tt:rdf/tt:s[@id = current()/@id]/tt:p[@id = '&foaf;nick']/tt:o[1]">
      </li>
    </xsl:for-each>
  </ul>
</xsl:for-each>

Processing the elements of a list

<xsl:variable name="list" select="/tt:rdf/tt:s[@id = 'http://example.com/feed' and tt:p[@id = '&rss;items']]/tt:o" />
<xsl:for-each select="/tt:rdf/tt:d[@id = $list and @parse = 'seq']/tt:o">
  <xsl:for-each select="/tt:rdf/tt:s[@id = current()/@id]">
    ...
  </xsl:for-each />
</xsl:for-each>

Processing the elements of a list in reverse order

<xsl:variable name="list" select="/tt:rdf/tt:s[@id = 'http://example.com/feed' and tt:p[@id = '&rss;items']]/tt:o" />
<xsl:for-each select="/tt:rdf/tt:d[@id = $list and @parse = 'seq']/tt:o">
  <xsl:sort data-type="number" select="position()" order="descending" />
  <xsl:for-each select="/tt:rdf/tt:s[@id = current()/@id]">
    ...
  </xsl:for-each />
</xsl:for-each>

Finding the English literal value of a property

<xsl:for-each select="/tt:rdf/tt:s[@id = 'http://example.com/thing']">
  <xsl:value-of select="tt:p[@id = '&dc;title']/tt:o[@xml:lang = 'en']" />
</xsl:for-each>

Finding the XML value of a property

<xsl:for-each select="/tt:rdf/tt:s[@id = 'http://example.com/thing']">
  <xsl:value-of disable-output-escaping="yes" select="tt:p[@id = '&atomrdf;xmlContent']/tt:o/node()" />
</xsl:for-each>

Tips

Use xsl-for-each to set the context to a tt:s node, and then work from there.

Use square brackets [] to select predicates that match your criteria.  You may need to nest square brackets.