Skip to content

Instantly share code, notes, and snippets.

@rettinghaus
Created June 8, 2020 14:22
Show Gist options
  • Select an option

  • Save rettinghaus/9508a057c61cf18e6e6ab5271f5fa6e5 to your computer and use it in GitHub Desktop.

Select an option

Save rettinghaus/9508a057c61cf18e6e6ab5271f5fa6e5 to your computer and use it in GitHub Desktop.
This is a little XSLT snippet, that, for a given authority control id, creates a link to a corresponding wikipedia entry in a specified language
<xsl:variable name="authority">
<!-- here goes the authority -->
</xsl:variable><xsl:variable name="identifier">
<!-- here goes the id -->
</xsl:variable><xsl:variable name="isoLang">
<!-- here goes the ISO 639-1 language code -->
</xsl:variable><xsl:variable name="property">
<xsl:choose>
<xsl:when test="$authority = 'viaf'">
<xsl:value-of select="214" />
</xsl:when>
<xsl:when test="$authority = 'gnd'">
<xsl:value-of select="227" />
</xsl:when>
<xsl:when test="$authority = 'lcnaf'">
<xsl:value-of select="244" />
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="query" select="document(concat('https://query.wikidata.org/sparql?query=SELECT%20%3Farticle%20WHERE%20%7B%0A%20%20%20%20%3Fitem%20wdt%3AP', $property,'%20%22', $identifier,'%22%20.%0A%20%20%20%20%3Farticle%20schema%3Aabout%20%3Fitem%20.%0A%20%20%20%20%3Farticle%20schema%3AinLanguage%20%22', $isoLang,'%22%20.%0A%20%20%20%20%3Farticle%20schema%3AisPartOf%20%5B%20wikibase%3AwikiGroup%20%22wikipedia%22%20%5D.%0A%7D'))" />
<xsl:variable name="link" select="$query//*[local-name() = 'uri'][1]" />
<xsl:if test="$link">
<a class="wikilink">
<xsl:attribute name="href">
<xsl:value-of select="$link" />
</xsl:attribute>
<xsl:attribute name="target">
<xsl:value-of select="'_blank'" />
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="'Wikipedia'" />
</xsl:attribute>
<span class="fab fa-wikipedia-w"></span>
</a>
</xsl:if>
@rettinghaus
Copy link
Author

You could do this with JavaScript, but this little query is extremely fast, so no need to do it asynchronously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment