Remove-Default-Attributes.xslt

From TEIWiki
Jump to navigation Jump to search

Summary

This is a straightforward XSLT stylesheet for removing the default attributes from a TEI document. These are often provided by the DTD or schema, and can clutter up viewing of them.

Add any comments to the 'discussion' tab.

Required Input

While this may work on other documents it assumes a TEI Lite P4 XML document.


Expected Output

Same document, but with default attributes removed if they have the default values.

Known Restrictions or Problems

It only works on TEI P4 documents, and is not namespace-aware, but seems to work fine on these.

Stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
  remove_default_attrs.xslt

  This style-sheet reads in an XML file (presumably a TEI
  Lite P4 XML one) and writes out the same file with
  any attributes that are specified with the same value as
  the default value (as listed in the 2004-07-16 version of
  teixlite.dtd) removed.

  I don't claim for a moment that the method I've used here is the best in
  any sense of the word "best", only that it was the one I happened to create
  using the flattened teixlite.dtd as a starting point.
  
  Written 2005-08-10 by Syd Bauman
  copyleft 2005 by Syd Bauman and the Text Encoding Initiative Consortium
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:tei="http://www.tei-c.org/ns/1.0">
  
  <xsl:output indent="yes" 
              method="xml"
              omit-xml-declaration="no"
              cdata-section-elements="eg"/>
  
  <!-- Generic copy-everything template -->
  <xsl:template match="*|@*|processing-instruction()|comment()">
    <xsl:copy>
      <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/>
    </xsl:copy>
  </xsl:template>   
  
  <!-- get rid of TEIform= attrs whose value match GI -->
  <!-- idea from Sebastian Rahtz's tei2tei.xsl -->
  <xsl:template match="@TEIform">        <!-- if we're on a TEIform= -->
    <xsl:if test="not( . = name(..) )">  <!-- if my value != name of the element I'm on -->
      <xsl:attribute name="TEIform">     <!-- output myself -->
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>                            <!-- thus, if my value == name of element I'm on, -->
  </xsl:template>                        <!-- since we do nothing the entire attr is dropped -->
  
  
  <!-- Now match each possible default, and reporoduce it only if  -->
  <!-- the value is not the default -->

  <!-- Each of these has precisely the same format, and thus it would -->
  <!-- probably be a good idea to make a separate parameterized routine -->
  <!-- instead. Someday. In the meantime, this is how they all work: -->
  <!-- When we hit an element/attribute combination that has a default value -->
  <!-- in the DTD, test to see if the value specified is the same as the -->
  <!-- default value in the DTD. If it is not, write out the attribute as -->
  <!-- it was specified in the instance; if it is, do nothing, thus dropping -->
  <!-- the entire attribute from the output. Note that the names of elements, -->
  <!-- attributes, and the attribute values are just hard-coded here in the -->
  <!-- style-sheet: no clever look-ups being performed here. -->
  
  <xsl:template match="gi/@TEI">
    <xsl:if test="not( . = 'yes' )">
      <xsl:attribute name="TEI">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="note/@anchored">
    <xsl:if test="not( . = 'yes' )">
      <xsl:attribute name="anchored">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="cell/@cols">
    <xsl:if test="not( . = '1' )">
      <xsl:attribute name="cols">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="textClass/@default | bibl/@default | biblFull/@default | editorialDecl/@default | langUsage/@default | listBibl/@default | projectDesc/@default | samplingDecl/@default | sourceDesc/@default">
    <xsl:if test="not( . = 'NO' )">
      <xsl:attribute name="default">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="q/@direct">
    <xsl:if test="not( . = 'unspecified' )">
      <xsl:attribute name="direct">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="refsDecl/@doctype">
    <xsl:if test="not( . = 'TEI.2' )">
      <xsl:attribute name="doctype">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="xref/@from | xptr/@from">
    <xsl:if test="not( . = 'ROOT' )">
      <xsl:attribute name="from">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="lg/@org | div0/@org | div1/@org | div2/@org | div3/@org | div4/@org | div5/@org | div6/@org | div7/@org | div/@org">
    <xsl:if test="not( . = 'uniform' )">
      <xsl:attribute name="org">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="seg/@part | ab/@part | div0/@part | div1/@part | div2/@part | div3/@part | div4/@part | div5/@part | div6/@part | div7/@part | div/@part | l/@part | lg/@part | s/@part">
    <xsl:if test="not( . = 'N' )">
      <xsl:attribute name="part">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="note/@place">
    <xsl:if test="not( . = 'unspecified' )">
      <xsl:attribute name="place">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="editor/@role">
    <xsl:if test="not( . = 'editor' )">
      <xsl:attribute name="role">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="cell/@role | row/@role">
    <xsl:if test="not( . = 'data' )">
      <xsl:attribute name="role">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="cell/@rows">
    <xsl:if test="not( . = '1' )">
      <xsl:attribute name="rows">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="lg/@sample | div0/@sample | div1/@sample | div2/@sample | div3/@sample | div4/@sample | div5/@sample | div6/@sample | div7/@sample | div/@sample">
    <xsl:if test="not( . = 'complete' )">
      <xsl:attribute name="sample">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="del/@statuss">
    <xsl:if test="not( . = 'unremarkable' )">
      <xsl:attribute name="status">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="availability/@status">
    <xsl:if test="not( . = 'unknown' )">
      <xsl:attribute name="status">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="teiHeader/@status">
    <xsl:if test="not( . = 'new' )">
      <xsl:attribute name="status">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="xref/@targOrder | ptr/@targOrder | ref/@targOrder | xptr/@targOrder">
    <xsl:if test="not( . = 'U' )">
      <xsl:attribute name="targOrder">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  
  <xsl:template match="xref/@to | xptr/@to">
    <xsl:if test="not( . = 'DITTO' )">
      <xsl:attribute name="to">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="titlePart/@type">
    <xsl:if test="not( . = 'main' )">
      <xsl:attribute name="type">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="list/@type">
    <xsl:if test="not( . = 'simple' )">
      <xsl:attribute name="type">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="teiHeader/@type">
    <xsl:if test="not( . = 'text' )">
      <xsl:attribute name="type">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
</xsl:stylesheet>