January 10, 2009

Bibliographies in XSLT

Filed under: Uncategorized — John Walkup @ 9:25 pm

I made big gains in my understanding of XSLT. One of my biggest stumbling blocks was how to reference bibliography entries cited in one document that referred to a bibliography file. For example, if one had the following LaTeX file main.tex

% main.tex

I am citing Andrew Porter\cite{Porter2004} and Norman Webb.\cite{Webb2007}

The (shortened) bibliography file would look something like:

@BOOK{Porter2004,
AUTHOR = “Porter, A.”,
TITLE = “The Enacted Curriculum”,
YEAR = 2004
}

@BOOK{Webb2007,
AUTHOR = “Webb, N.”,
TITLE = “Depth of Knowledge”,
YEAR = 2007

}

Since the citation of Porter appears first in the text, then it will be denoted as the first citation; the Webb citation would appear as the second. The result would be:

I am citing Andrew Porter[1] and Norman Webb[2].

Notice the numbering scheme: The number 1 is attributed to the first citation that appears in the main text body, not the order in which the entries appear in the bibliography. (This requirement is still giving me some trouble in XSLT.)

To introduce a citation system along these lines, we rely on the count() command in XSL, specifically count(preceding-sibling::attribute_name). This will count the number of sibling elements that correspond to the attribute attribute_name. Let’s take a look at a simple bibliography XML file called bibliography.xml.

<bibliography>
<title>REFERENCES</title>
<biblioentry id=”Webb2007″>Webb, Norman, et al.</biblioentry>
<biblioentry id=”Porter2004″>Porter, Andrew, et al.</biblioentry>
<biblioentry id=”Walkup2000″>Walkup, John, et al.</biblioentry>
<biblioentry id=”Brown1999″>Brown, John</biblioentry>
</bibliography>

Now these entries must be called in another document (which will be the primary document as far as the XSLT stylesheet is concerned), which we will call main.xml.

<para>
We will cite Andrew Porter(<bibref xref=”Porter2004″/>) and Norman Webb(<bibref xref=”Webb2007″/>).
</para>

Here is the XSL stylesheet that will perform the citations:

<xsl:variable name=”secondary_document” select=”document(’bibliography.xml’)”/>

<xsl:template match=”bibref”>
<xsl:variable name=”xref” select=”@xref”/>
<xsl:for-each select=”$secondary_document//bibliography/bibliomixed”>
<xsl:if test=”@id = $xref”>
<xsl:value-of select=”count(preceding-sibling::bibliomixed)+1″/>
</xsl:if>
</xsl:for-each>
</xsl:template>

The result is

We will cite Andrew Porter(2) and Norman Webb(1).

Notice that the reference numbers refer to the place in which the bibliography entries appear in the bibliography file, not the order in which they are cited. This is unfortunate, and something I need to look into further.

As with our examples of using keys, the <xsl:for-each> command forces the stylesheet to open and look into the secondary document, which in this case is bibliography.xml.

If we wanted to cite the entries with the APA style, there is a good (and hard) way to do it, and a cheap (but easy) way to do it. Simply create a node called <abbrev> for each entry and stuff inside it the text that you would want to appear in the output.

Here is the modified version of bibliography.xml.

<bibliography>
<title>REFERENCES</title>
<biblioentry id=”Webb2007″><abbrev>Webb2007</abbrev> Webb, Norman, et al.</biblioentry>
<biblioentry id=”Porter2004″><abbrev>Porter2004</abbrev> Porter, Andrew, et al.</biblioentry>
<biblioentry id=”Walkup2000″><abbrev>Walkup2000</abbrev>Walkup, John, et al.</biblioentry>
<biblioentry id=”Brown1999″><abbrev>Brown1999</abbrev>Brown, John</biblioentry>
</bibliography>

Now we will replace our earlier call to the count() command and simply print the text within the <abbrev> node. That is,

<xsl:if test=”@id = $xref”>
<xsl:value-of select=”abbrev”/>
</xsl:if>

The result is

We will cite Andrew Porter(Porter2004) and Norman Webb(Webb2007).

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress