Difference between revisions of "Bibl2struct.xslt"

From TEIWiki
Jump to navigation Jump to search
(Stylesheet)
m (small fix on capitalization of element name in description text)
Line 1: Line 1:
 
== Summary ==
 
== Summary ==
Transforms TEI <nowiki><bibl>s into <biblstruct> bibliographies.
+
Transforms TEI <nowiki><bibl>s into <biblStruct> bibliographies.
 
Ignores all other content. Produces a <listBibl></nowiki>.
 
Ignores all other content. Produces a <listBibl></nowiki>.
  
Line 6: Line 6:
  
 
This document will take any XML file, not just TEI XML, and processes all
 
This document will take any XML file, not just TEI XML, and processes all
''complete'' <nowiki><bibl> bibliographies into equivalent <biblStructs>.
+
''complete'' <nowiki><bibl> bibliographies into equivalent <biblStruct>s.
  
 
The transform differentiates five major cases from
 
The transform differentiates five major cases from
Line 36: Line 36:
 
<?xml version="1.0"?>
 
<?xml version="1.0"?>
 
<!-- bibl2struct.xsl
 
<!-- bibl2struct.xsl
   Transforms TEI <bibl>s into <biblstruct> bibliographies. Version 08-01.
+
   Transforms TEI <bibl>s into <biblStruct> bibliographies. Version 08-01.
 
   Author: (c) 2008 Ralf Stephan <ralf@ark.in-berlin.de>
 
   Author: (c) 2008 Ralf Stephan <ralf@ark.in-berlin.de>
 
   Published under GPL 2.0, see http://www.gnu.org/licenses/gpl-2.0.html
 
   Published under GPL 2.0, see http://www.gnu.org/licenses/gpl-2.0.html

Revision as of 15:31, 20 February 2008

Summary

Transforms TEI <bibl>s into <biblStruct> bibliographies. Ignores all other content. Produces a <listBibl>.

Required Input

This document will take any XML file, not just TEI XML, and processes all complete <bibl> bibliographies into equivalent <biblStruct>s. The transform differentiates five major cases from the (non)-existence of <title>s of certain @level (a,j,m,s). The level in parens, e.g. (a) means 'level a or without/blank level'.

  • <bibl> has (a)+m+s titles ==> excerpt from work in series
  • <bibl> has (m)+s titles ==> work in series
  • <bibl> has (a)+j titles ==> journal article
  • <bibl> has (a)+m titles ==> excerpt from monograph
  • <bibl> has (m) titles ==> monograph

When an element contains @corresp the text is taken from that id. When the author is missing and <bibl> has an id, then we look for the id in <link>s and, when found, for authors therein. When the document contains a <join result='bibl' />, we build that bibl and process it like the others.

Expected Output

a <listBibl>

Known Restrictions or Problems

See TODO in the stylesheet.

Please add any comments to the 'discussion' tab. For payed support, please mail the author (address within stylesheet).

Stylesheet

<?xml version="1.0"?>
<!-- bibl2struct.xsl
  Transforms TEI <bibl>s into <biblStruct> bibliographies. Version 08-01.
  Author: (c) 2008 Ralf Stephan <ralf@ark.in-berlin.de>
  Published under GPL 2.0, see http://www.gnu.org/licenses/gpl-2.0.html

  TODO:
  =====
  o  if the bibl has an @id and an author, we don't look further but
     there could be more authors <link>ed
  o  <title type='alt'> with <editor role='translator'>
  o  assume from existing <author /> that author of ref is author of work?
  o  handle <bibl>...<series>...</series>...</bibl>
  o  what do we do we with refs without title?
  o  what if we have <author><corr>...</corr></author> ?
  -->
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">

    <xsl:output method="xml" encoding="UTF-8"
	    doctype-public="-//TEI P5//DTD Main Document Type//EN" 
	    doctype-system="tei2.dtd" />

    <!-- Include a link to CSS -->
    <xsl:template match="/">
      <xsl:processing-instruction name="xml-stylesheet">href="your.css" type="text/css"</xsl:processing-instruction>
      <listBibl>
        <xsl:apply-templates />
      </listBibl>
    </xsl:template>

    <!-- remove all default attributes -->
    <xsl:template match="@status"></xsl:template>
    <xsl:template match="@default"></xsl:template>
    <xsl:template match="@org[.='uniform']"></xsl:template>
    <xsl:template match="@sample[.='complete']"></xsl:template>
    <xsl:template match="@part[.='N']"></xsl:template>
    <xsl:template match="@direct[.='unspecified']"></xsl:template>
    <xsl:template match="@targOrder[.='U']"></xsl:template>
    <xsl:template match="@from[.='ROOT']"></xsl:template>
    <xsl:template match="@anchored[.='yes']"></xsl:template>
    <xsl:template match="@place[.='unspecified']"></xsl:template>
    <xsl:template match="teiHeader/@type"></xsl:template>

    <!-- do not copy all CDATA, we copy explicitly later -->
    <xsl:template match="text()" />

    <!-- ignore teiHeader -->
    <xsl:template match='teiHeader' />

    <!-- <bibl> without <title> does nothing -->
    <xsl:template match='bibl[not(title)]' />

    <!-- handle <join result='bibl'> here -->
    <xsl:template match='join[@result="bibl"]'>
      <xsl:variable name='joint'>
        <bibl>
        <xsl:call-template name='chomp-targets-recursively'>
          <xsl:with-param name='idstr'>
            <xsl:value-of select='@targets' />
          </xsl:with-param>
        </xsl:call-template>
        </bibl>
      </xsl:variable>
      <xsl:apply-templates select='exsl:node-set($joint)//bibl' />
    </xsl:template>

    <!-- Calls copy-of with all ids in targets string -->
    <!-- Partly stolen from http://wwbota.free.fr/XSLT_models/split-string.xslt-->
    <xsl:template name='chomp-targets-recursively'>
      <xsl:param name='idstr' />
      <xsl:variable name="first">
        <xsl:variable name="first0" select="substring-before($idstr,' ')"/>
        <xsl:choose>
          <xsl:when test="$first0">
            <xsl:copy-of select="$first0"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:copy-of select="$idstr"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:if test="string-length($first)">
        <xsl:call-template name='chomp-targets-recursively'>
          <xsl:with-param name="idstr">
            <xsl:value-of select="substring-after($idstr,' ')"/>
          </xsl:with-param>
        </xsl:call-template>
        <xsl:copy-of select='//*[@id=$first]' />
      </xsl:if>
    </xsl:template>

    <!--===================================-->
    <!-- complete bibl with titles (a)+m+s -->
    <xsl:template match='bibl[(title[@level="a"] or title[@level=""] or title[not(@level)]) and title[@level="m"] and title[@level="s"]]'>
      <biblStruct>
        <analytic>
          <xsl:choose>
            <xsl:when test='author'>
              <xsl:apply-templates select='author' />
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name='authors' />
            </xsl:otherwise>
          </xsl:choose>
          <xsl:apply-templates select='title[(@level="a" or @level="" or not(@level))]' />
        </analytic>
        <monogr>
          <xsl:apply-templates select='editor' />
          <xsl:apply-templates select='title[@level="m"]' />
        </monogr>
        <series>
          <xsl:apply-templates select='editor' />
          <xsl:apply-templates select='title[@level="s"]' />
          <xsl:if test='date|publisher|pubPlace|biblScope'>
          <imprint>
            <xsl:apply-templates select='publisher' />
            <xsl:apply-templates select='pubPlace' />
            <xsl:apply-templates select='edition' />
            <xsl:apply-templates select='date' />
            <xsl:apply-templates select='biblScope' />
          </imprint>
        </xsl:if>
        </series>
        <xsl:apply-templates select='note' />
      </biblStruct>
    </xsl:template>

    <!-- complete bibl with titles (m)+s -->
    <xsl:template match='bibl[not(title[@level="a"]) and (title[@level="m"] or title[@level=""] or title[not(@level)]) and title[@level="s"]]'>
      <biblStruct>
        <monogr>
          <xsl:choose>
            <xsl:when test='author'>
              <xsl:apply-templates select='author' />
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name='authors' />
            </xsl:otherwise>
          </xsl:choose>
          <xsl:apply-templates select='title[@level="m" or @level="" or not(@level)]' />
          <xsl:if test='date|publisher|pubPlace|biblScope'>
          <imprint>
            <xsl:apply-templates select='publisher' />
            <xsl:apply-templates select='pubPlace' />
            <xsl:apply-templates select='edition' />
            <xsl:apply-templates select='date' />
            <xsl:apply-templates select='biblScope' />
          </imprint>
        </xsl:if>
        </monogr>
        <series>
          <xsl:apply-templates select='editor' />
          <xsl:apply-templates select='title[@level="s"]' />
        </series>
        <xsl:apply-templates select='note' />
      </biblStruct>
    </xsl:template>

    <!-- bibl with title (a)+j -->
    <xsl:template match='bibl[title[not(@level) or @level="a"] and title[@level="j"]]'>
      <biblStruct>
        <analytic>
          <xsl:choose>
            <xsl:when test='author'>
              <xsl:apply-templates select='author' />
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name='authors' />
            </xsl:otherwise>
          </xsl:choose>
          <xsl:apply-templates select='title[not(@level) or @level="a"]' />
        </analytic>
        <monogr>
          <xsl:apply-templates select='title[@level="j"]' />
          <xsl:if test='date|publisher|pubPlace|biblScope'>
          <imprint>
            <xsl:apply-templates select='publisher' />
            <xsl:apply-templates select='pubPlace' />
            <xsl:apply-templates select='edition' />
            <xsl:apply-templates select='date' />
            <xsl:apply-templates select='biblScope' />
          </imprint>
        </xsl:if>
        <xsl:apply-templates select='editor' />
        </monogr>
        <xsl:apply-templates select='note' />
      </biblStruct>
    </xsl:template>

    <!-- complete bibl with title (a)+m -->
    <xsl:template match='bibl[title[not(@level) or @level="a"] and title[@level="m"]]'>
      <biblStruct>
        <analytic>
          <xsl:choose>
            <xsl:when test='author'>
              <xsl:apply-templates select='author' />
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name='authors' />
            </xsl:otherwise>
          </xsl:choose>
          <xsl:apply-templates select='title[not(@level) or @level="a"]' />
        </analytic>
        <monogr>
          <xsl:apply-templates select='editor' />
          <xsl:apply-templates select='title[@level="m"]' />
          <xsl:if test='date|publisher|pubPlace|biblScope'>
          <imprint>
            <xsl:apply-templates select='publisher' />
            <xsl:apply-templates select='pubPlace' />
            <xsl:apply-templates select='edition' />
            <xsl:apply-templates select='date' />
            <xsl:apply-templates select='biblScope' />
          </imprint>
        </xsl:if>
        </monogr>
        <xsl:apply-templates select='note' />
      </biblStruct>
    </xsl:template>

    <!-- bibl with either title m only or blank only, all without a,j,s -->
    <xsl:template match='bibl[(not(title[@level="a"]) and not(title[@level="j"]) and not(title[@level="s"])) and ((title[@level="m"] and not(title[not(@level)])) or (not(title[@level="m"]) and title[not(@level)]))]'>
      <biblStruct>
        <monogr>
          <xsl:choose>
            <xsl:when test='author'>
              <xsl:apply-templates select='author' />
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name='authors' />
            </xsl:otherwise>
          </xsl:choose>
          <xsl:apply-templates select='title' />
          <xsl:if test='date|publisher|pubPlace|biblScope'>
          <imprint>
            <xsl:apply-templates select='publisher' />
            <xsl:apply-templates select='pubPlace' />
            <xsl:apply-templates select='edition' />
            <xsl:apply-templates select='date' />
            <xsl:apply-templates select='biblScope' />
          </imprint>
        </xsl:if>
        </monogr>
        <xsl:apply-templates select='note' />
      </biblStruct>
    </xsl:template>

    <!-- incomplete bibl with title only! the bibl should have an @id -->
    <xsl:template name='authors'>
      <xsl:if test='not(@id)'>
        <xsl:text>ERROR: bibl has neither author nor id! </xsl:text>
      </xsl:if>
      <xsl:if test='@id'>
        <xsl:variable name='ti'>
          <xsl:value-of select='@id' />
        </xsl:variable>
        <xsl:for-each select='//link'>
          <xsl:if test='contains(@targets,$ti)'>
            <xsl:call-template name='recursively-apply-first-of'>
              <xsl:with-param name='idstr'>
                <xsl:value-of select='@targets' />
              </xsl:with-param>
            </xsl:call-template>
          </xsl:if>
        </xsl:for-each>
      </xsl:if>
    </xsl:template>

    <!-- Calls all author/name templates with id in given param string -->
    <!-- Partly stolen from http://wwbota.free.fr/XSLT_models/split-string.xslt-->
    <xsl:template name='recursively-apply-first-of'>
      <xsl:param name='idstr' />
      <xsl:variable name="first">
        <xsl:variable name="first0" select="substring-before($idstr,' ')"/>
        <xsl:choose>
          <xsl:when test="$first0">
            <xsl:copy-of select="$first0"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:copy-of select="$idstr"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:if test="string-length($first)">
        <xsl:call-template name='recursively-apply-first-of'>
          <xsl:with-param name="idstr">
            <xsl:value-of select="substring-after($idstr,' ')"/>
          </xsl:with-param>
        </xsl:call-template>
        <xsl:apply-templates select='//author[@id=$first]|//name[@id=$first]' />
      </xsl:if>
    </xsl:template>

    <!--=========================================-->
    <!-- all elements except title: do deep copy -->
    <xsl:template match="@* | author | title [name(..)='bibl'] | editor | 
      biblScope | edition | date[name(..)='bibl'] | publisher | pubPlace | 
      note[name(..)='bibl']">
      <xsl:choose>
        <xsl:when test='@corresp'>
          <xsl:apply-templates select='id(@corresp)' />
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy>
            <xsl:apply-templates select="@*[not(name()='TEIform')]|node()|text()"/>
          </xsl:copy>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

    <xsl:template match='bibl/series'>
      <xsl:text>abc</xsl:text>
      <xsl:copy-of select='*/node()' />
    </xsl:template>

    <!-- copy CDATA within elements except titles -->
    <xsl:template match="bibl/text()|author/text()|editor/text()|title[name(..)='bibl']/text()|biblScope/text()|date[name(..)='bibl']/text()|edition/text()|pubPlace/text()|publisher/text()|note[name(..)='bibl']/text()">
      <xsl:copy />
    </xsl:template>

</xsl:stylesheet>