Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
XSLT stylesheet in XPL to get request parameters and construct a SPARQL query for generating a count for distribution in Nomisma.org
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- request parameters -->
<xsl:param name="filter" select="doc('input:filter')/query"/>
<xsl:param name="dist" select="doc('input:request')/request/parameters/parameter[name='dist']/value"/>
<xsl:param name="format" select="doc('input:request')/request/parameters/parameter[name='format']/value"/>
<!-- config variables -->
<xsl:variable name="sparql_endpoint" select="/config/sparql_query"/>
<xsl:variable name="query" select="doc('input:query')"/>
<!-- parse query statements into a data object -->
<xsl:variable name="statements" as="element()*">
<statements>
<!-- parse filters -->
<xsl:for-each select="tokenize($filter, ';')">
<xsl:variable name="property" select="substring-before(normalize-space(.), ' ')"/>
<xsl:variable name="object" select="substring-after(normalize-space(.), ' ')"/>
<xsl:choose>
<xsl:when test="$property = 'portrait' or $property='deity'">
<triple s="?coinType" p="nmo:hasObverse" o="?obv"/>
<triple s="?coinType" p="nmo:hasReverse" o="?rev"/>
<union>
<triple s="?obv" p="nmo:hasPortrait" o="{$object}"/>
<triple s="?rev" p="nmo:hasPortrait" o="{$object}"/>
</union>
</xsl:when>
<xsl:otherwise>
<triple s="?coinType" p="{$property}" o="{$object}"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!-- parse dist -->
<xsl:choose>
<xsl:when test="$dist='portrait' or $dist='deity'">
<xsl:variable name="distClass" select="if ($dist='portrait') then 'foaf:Person' else 'wordnet:Deity'"/>
<triple s="?coinType" p="nmo:hasObverse" o="?obv"/>
<triple s="?coinType" p="nmo:hasReverse" o="?rev"/>
<union>
<triple s="?obv" p="nmo:hasPortrait" o="?dist"/>
<triple s="?rev" p="nmo:hasPortrait" o="?dist"/>
</union>
<triple s="?dist" p="a" o="{$distClass}"/>
</xsl:when>
<xsl:otherwise>
<triple s="?coinType" p="{$dist}" o="?dist"/>
<!-- if the dist is mint, then include lat and long, but only for CSV -->
<xsl:if test="$dist='nmo:hasMint' and $format='csv'">
<optional>
<triple s="?dist" p="geo:location" o="?loc"/>
<triple s="?loc" p="geo:lat" o="?lat"/>
<triple s="?loc" p="geo:long" o="?long"/>
</optional>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</statements>
</xsl:variable>
<xsl:variable name="statementsSPARQL">
<xsl:apply-templates select="$statements/*"/>
</xsl:variable>
<xsl:variable name="service">
<xsl:value-of
select="concat($sparql_endpoint, '?query=', encode-for-uri(replace($query, '%STATEMENTS%', $statementsSPARQL)), '&amp;output=xml')"
/>
</xsl:variable>
<xsl:template match="/">
<config>
<url>
<xsl:value-of select="$service"/>
</url>
<content-type>application/xml</content-type>
<encoding>utf-8</encoding>
</config>
</xsl:template>
<xsl:template match="triple">
<xsl:value-of select="concat(@s, ' ', @p, ' ', @o, '.')"/>
<xsl:if test="not(parent::union)">
<xsl:text>&#x0A;</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="optional">
<xsl:text>OPTIONAL {</xsl:text>
<xsl:apply-templates select="triple"/>
<xsl:text>}&#x0A;</xsl:text>
</xsl:template>
<xsl:template match="union">
<xsl:for-each select="triple">
<xsl:if test="position() &gt; 1">
<xsl:text>UNION </xsl:text>
</xsl:if>
<xsl:text>{</xsl:text>
<xsl:apply-templates select="self::node()"/>
<xsl:text>}&#x0A;</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.