Table2mediawiki.xslt

From TEIWiki
Jump to navigation Jump to search

This is a fast-and-lousy proof-of-concept stylesheet that reads in a TEI (P5) document and writes out a MediWiki representation of each <table> therein. (Although it deliberately ignores tables that are inside other tables.)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
  xmlns:tei="http://www.tei-c.org/ns/1.0">

  <xd:doc scope="stylesheet">
    <xd:desc>
      <xd:p><xd:b>Written</xd:b>: 2009-12-12</xd:p>
      <xd:p><xd:b>Author</xd:b>: Syd Bauman</xd:p>
      <xd:p><xd:b>Status</xd:b>: Copyleft 2009 Syd Bauman and the Brown University Women Writers
        Project</xd:p>
      <xd:p><xd:b>Input</xd:b>: A TEI (P5) document</xd:p>
      <xd:p><xd:b>Output</xd:b>: a rudimentary representation of each table in the input (that is
        not itself inside a table) in the MediaWiki language</xd:p>
      <xd:p><xd:b>Known issues</xd:b>: <xd:ul>
        <xd:li>ignores all attributes except role= of <row></xd:li>
          <xd:li>only knows about "label" and "data" values of role= of <row></xd:li>
          <xd:li>no processing of phrase-level elements (yet :-) — after all, this is really
          intended as a proof-of-concept at this point</xd:li>
          <xd:li>should be parameratized a bit better (e.g., user should be able to specify border=
            and cellpadding=)</xd:li>
          <xd:li>someday should handle multiple wiki-languages</xd:li>
        </xd:ul>
      </xd:p>
    </xd:desc>
  </xd:doc>

  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="/">
    <xsl:apply-templates select="//tei:table[not( ancestor::tei:table )]"/>
  </xsl:template>

  <xsl:template match="tei:table">
    <xsl:apply-templates select="tei:head"/>
    <!-- ignore elements from model.global -->
    <!-- start: -->
    <xsl:text>{| border="1" cellpadding="2"</xsl:text>
    <xsl:text>&#x0A;</xsl:text>
    <!-- process rows: -->
    <xsl:apply-templates select="tei:row"/>
    <!-- finish up: -->
    <xsl:text>|}</xsl:text>
    <xsl:text>&#x0A;</xsl:text>
    <xsl:text>&#x0A;</xsl:text>
  </xsl:template>

  <xsl:template match="tei:row">
    <xsl:variable name="sepChar">
      <xsl:choose>
        <xsl:when test="@role='label'">!</xsl:when>
        <xsl:when test="@role='data' or not(@role)">|</xsl:when>
        <xsl:otherwise>
          <xsl:message>warning: unrecognized role= of <xsl:number/> &lt;row>; presuming "data"</xsl:message>
          <xsl:text>|</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:text>|-</xsl:text>
    <xsl:text>&#x0A;</xsl:text>
    <xsl:value-of select="$sepChar"/>
    <xsl:for-each select="tei:cell">
      <xsl:apply-templates select="."/>
      <xsl:choose>
        <xsl:when test="following-sibling::tei:cell">
          <xsl:value-of select="$sepChar"/>
          <xsl:value-of select="$sepChar"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>&#x0A;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="tei:head">
    <xsl:text>|+</xsl:text>
    <xsl:value-of select="normalize-space(.)"/>
    <xsl:text>&#x0A;</xsl:text>
  </xsl:template>

  <xsl:template match="tei:cell">
    <xsl:text> </xsl:text>
    <xsl:value-of select="normalize-space(.)"/>
    <xsl:text> </xsl:text>
  </xsl:template>

</xsl:stylesheet>