Last active
September 22, 2015 06:26
-
-
Save tomschr/e4debf714d2149ede1e8 to your computer and use it in GitHub Desktop.
Very rough XSLT to backlink from DocBook's glossary to the first glossterm|firstterm entry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<article xml:lang="en" version="5.0" | |
xmlns="http://docbook.org/ns/docbook" | |
xmlns:xi="http://www.w3.org/2001/XInclude" | |
xmlns:xlink="http://www.w3.org/1999/xlink"> | |
<title>Test</title> | |
<para xml:id="first"><glossterm>XML</glossterm></para> | |
<para xml:id="second">bla bla bla <glossterm>XML</glossterm></para> | |
<glossary> | |
<title>Glossary</title> | |
<glossentry xml:id="glos.xml"> | |
<glossterm>XML</glossterm> | |
<glossdef> | |
<para>Definition of XML</para> | |
</glossdef> | |
</glossentry> | |
</glossary> | |
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xsl:stylesheet version="1.0" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
xmlns="http://www.w3.org/1999/xhtml" | |
xmlns:d="http://docbook.org/ns/docbook"> | |
<!-- This is our key; we are interested in glossterms and firstterms elements | |
and its (text) contents | |
--> | |
<xsl:key name="glossterm" match="d:glossterm|d:firstterm" use="."/> | |
<xsl:template match="d:article|d:glossary|d:glossentry"> | |
<div class="{local-name()}"> | |
<xsl:if test="@xml:id"> | |
<xsl:attribute name="id"><xsl:value-of select="@xml:id"/></xsl:attribute> | |
</xsl:if> | |
<xsl:apply-templates/> | |
</div> | |
</xsl:template> | |
<xsl:template match="d:glossentry/d:glossdef"> | |
<xsl:param name="node"/> | |
<xsl:variable name="term" select="../d:glossterm"/> | |
<!-- Select the target of the first glossterm or firstterm pointing to | |
this glossterm definition | |
From all found targets, use only the first one. | |
--> | |
<xsl:variable name="target" select="key('glossterm', $term)[1]"/> | |
<!-- Just some debug message --> | |
<xsl:message>d:glossentry/d:glossdef | |
glossterm=<xsl:value-of select="$term"/> | |
target=<xsl:value-of select="count($target)"/> | |
</xsl:message> | |
<xsl:if test="$target"> | |
<!-- Here we use only the a message, however, it can be created anything. | |
ATM we use the ID from its parent element. This can be improved by | |
using the ancestor-or-self axis: | |
$target/ancestor-or-self::*/@xml:id | |
--> | |
<xsl:message>glossterm with <xsl:value-of select="$target/../@xml:id"/></xsl:message> | |
</xsl:if> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment