Difference between revisions of "Remove-Default-Attributes.xslt"

From TEIWiki
Jump to navigation Jump to search
m (Spam Removal)
(Required Input)
Line 7: Line 7:
 
== Required Input ==
 
== Required Input ==
  
While this may work on other documents it assumes a TEI Lite P4 XML document.
+
<?xml version="1.0" encoding="ISO-8859-1"?>
 
+
<!DOCTYPE EtfDocumentaryCreditNotification SYSTEM "EtfDocumentaryCreditNotification466_1_0.dtd">
 +
<EtfDocumentaryCreditNotification>  <BaseHeader>      <messageType>CHAETFData_1</messageType>      <messageTypeVersion>        <major>0001</major>        <minor>0000</minor>      </messageTypeVersion>      <baseHeaderVersion>        <major>0002</major>        <minor>0001</minor>      </baseHeaderVersion>      <originalMessageID_B/>      <wireFormat>XML</wireFormat>      <senderID>GTF</senderID>      <clientBez/>      <isaccID>SB01</isaccID>      <traceLevel/>      <originalSendTime/>      <patternType>DSY</patternType>      <errorCode/>      <severity/>      <errorText/>      <completionCode/>      <reasonCode/>      <mqObject/>      <flowNode/>  </BaseHeader>  <AppHeader>      <eventDomain>CHA</eventDomain>      <eventCategory>ONL</eventCategory>      <businessUnit>0009</businessUnit>      <businessDay>ref01</businessDay>      <eventTimestamp>2002-04-18-09.28.04.287551</eventTimestamp>  </AppHeader>  <EtfHeader>      <cifNumber>083503619132</cifNumber>      <tfsc>ZRH</tfsc>      <orgUnit>SGAT 115</orgUnit>      <dossierNumber>SGAT 115-054146</dossierNumber>      <refNumber>SGAT 115-054146</refNumber>      <eventId>ISS</eventId>      <eventSeqNumber>0</eventSeqNumber>      <msgSeqNumber>1</msgSeqNumber>      <product>01</product>      <timestamp>2006-03-17 11:45:28</timestamp>      <entityId>001</entityId>      <channel>B</channel>      <numberOfElements>0</numberOfElements>      <ProcessInformationCustomer>        <subject>SGAT-115-054146_2006-03-17_466</subject>        <line/>      </ProcessInformationCustomer>  </EtfHeader>  <DocumentaryCreditNotification xmlns:dt=" http://www.bolero.net/io/xmlns/datatype/1.0">      <Header xmlns:cmp=" http://www.bolero.net/io/xmlns/header/1.0">        <cmp:DocumentID>            <cmp:RID>CREDIT-SUISSE-USERTEST.IMAP</cmp:RID>            <cmp:GeneralID>SGAT-115-054146A0</cmp:GeneralID>            <cmp:Version>V1.0</cmp:Version>        </cmp:DocumentID>        <cmp:DocType>            <cmp:DocTypeCode>466</cmp:DocTypeCode>        </cmp:DocType>        <cmp:Status>Final</cmp:Status>      </Header>      <Body>        <Party>            <partyType>APP</partyType>            <partyName>Glencore International AG</partyName>            <PartyId>              <partyIdType>INT</partyIdType>              <partyIdValue>083503619132</partyIdValue>            </PartyId>            <PartyAddress>              <line>Glencore International AG</line>              <line>Baarermattstrasse 3</line>              <line>Postfach 777</line>              <line>6341 Baar</line>            </PartyAddress>            <Contact>              <contactType>TEL</contactType>              <contactValue>(41)41-7092947</contactValue>            </Contact>            <Contact>              <contactType>FAX</contactType>              <contactValue>(41)41-7093000</contactValue>            </Contact>        </Party>        <Party>            <partyType>ADB</partyType>            <partyName>Bangkok Bank Public Company</partyName>            <PartyId>              <partyIdType>INT</partyIdType>              <partyIdValue>083509920500</partyIdValue>            </PartyId>            <PartyAddress>              <line>Bangkok Bank Public Company</line>              <line>Limited</line>              <line>333 Silom Road</line>              <line>TH-Bangkok 10500</line>            </PartyAddress>        </Party>        <Party>            <partyType>BEN</partyType>            <partyName>Eximp</partyName>            <PartyId>              <partyIdType>INT</partyIdType>              <partyIdValue/>            </PartyId>            <PartyAddress>              <line>Eximp</line>              <line>Mainstreet</line>              <line>London</line>            </PartyAddress>        </Party>        <Party>            <partyType>ISB</partyType>            <partyName>Credit Suisse</partyName>            <PartyId>              <partyIdType>INT</partyIdType>              <partyIdValue>083510483505</partyIdValue>            </PartyId>            <PartyAddress>              <line>Credit Suisse</line>              <line>Hauptsitz</line>              <line>8070 Zuerich</line>            </PartyAddress>        </Party>        <Date>            <dateType>EXP</dateType>            <dateValue>2006-06-30</dateValue>        </Date>        <Date>            <dateType>ISS</dateType>            <dateValue>2006-03-17</dateValue>        </Date>        <Reference>            <referenceType>999</referenceType>            <referenceValue>RS170306T50</referenceValue>            <ReferenceSource>              <partyType>APP</partyType>            </ReferenceSource>        </Reference>        <Reference>            <referenceType>466</referenceType>            <referenceValue>SGAT 115-054146</referenceValue>            <ReferenceSource>              <partyType>ISB</partyType>            </ReferenceSource>        </Reference>        <Location>            <locationType>SHF</locationType>            <LocationId>              <locationName>Mombasa</locationName>            </LocationId>        </Location>        <Location>            <locationType>SHT</locationType>            <LocationId>              <locationName>Basel</locationName>            </LocationId>        </Location>        <Location>            <locationType>DEL</locationType>            <LocationId>              <locationName>Basel</locationName>            </LocationId>        </Location>        <DocumentaryCreditNotificationParticulars>            <documentaryCreditType>IRREVOCABLE</documentaryCreditType>            <documentaryCreditSubType>NOR</documentaryCreditSubType>            <DocumentaryCreditAmount>              <amount>3000.00</amount>              <currencyCode>CHF</currencyCode>            </DocumentaryCreditAmount>            <DocumentaryCreditAmountVariance>              <documentaryCreditAmountVarianceType>NOT EXCEEDING</documentaryCreditAmountVarianceType>              <PercentageVariance/>            </DocumentaryCreditAmountVariance>            <DocumentaryCreditAvailability>              <DocumentaryCreditAvailableWith>                  <documentaryCreditAvailabilityParty>ISB</documentaryCreditAvailabilityParty>              </DocumentaryCreditAvailableWith>              <documentaryCreditAvailabilityType>BY PAYMENT</documentaryCreditAvailabilityType>            </DocumentaryCreditAvailability>            <DocumentaryCreditValidity>              <documentaryCreditValidityParty>ISB</documentaryCreditValidityParty>            </DocumentaryCreditValidity>            <confirmationInstructionType>WITHOUT</confirmationInstructionType>            <PresentationPeriod>              <line>2 weeks</line>            </PresentationPeriod>            <ShipmentPeriod>              <line>One week</line>            </ShipmentPeriod>            <PartialShipment>              <partialShipmentIndicator>FALSE</partialShipmentIndicator>            </PartialShipment>            <Transhipment>              <transhipmentIndicator>FALSE</transhipmentIndicator>            </Transhipment>            <Incoterms>              <incotermsCode>CIF</incotermsCode>            </Incoterms>            <documentIssueType>SWT</documentIssueType>            <amendmentAdviceRefusalIndicator>FALSE</amendmentAdviceRefusalIndicator>        </DocumentaryCreditNotificationParticulars>        <Goods>            <GoodsDescription>              <line>coffee</line>            </GoodsDescription>        </Goods>        <Documentation>            <line>Invoice</line>        </Documentation>      </Body>  </DocumentaryCreditNotification>
 +
</EtfDocumentaryCreditNotification>
  
 
== Expected Output ==
 
== Expected Output ==

Revision as of 20:42, 20 March 2006

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

<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE EtfDocumentaryCreditNotification SYSTEM "EtfDocumentaryCreditNotification466_1_0.dtd"> <EtfDocumentaryCreditNotification> <BaseHeader> <messageType>CHAETFData_1</messageType> <messageTypeVersion> <major>0001</major> <minor>0000</minor> </messageTypeVersion> <baseHeaderVersion> <major>0002</major> <minor>0001</minor> </baseHeaderVersion> <originalMessageID_B/> <wireFormat>XML</wireFormat> <senderID>GTF</senderID> <clientBez/> <isaccID>SB01</isaccID> <traceLevel/> <originalSendTime/> <patternType>DSY</patternType> <errorCode/> <severity/> <errorText/> <completionCode/> <reasonCode/> <mqObject/> <flowNode/> </BaseHeader> <AppHeader> <eventDomain>CHA</eventDomain> <eventCategory>ONL</eventCategory> <businessUnit>0009</businessUnit> <businessDay>ref01</businessDay> <eventTimestamp>2002-04-18-09.28.04.287551</eventTimestamp> </AppHeader> <EtfHeader> <cifNumber>083503619132</cifNumber> <tfsc>ZRH</tfsc> <orgUnit>SGAT 115</orgUnit> <dossierNumber>SGAT 115-054146</dossierNumber> <refNumber>SGAT 115-054146</refNumber> <eventId>ISS</eventId> <eventSeqNumber>0</eventSeqNumber> <msgSeqNumber>1</msgSeqNumber> <product>01</product> <timestamp>2006-03-17 11:45:28</timestamp> <entityId>001</entityId> <channel>B</channel> <numberOfElements>0</numberOfElements> <ProcessInformationCustomer> <subject>SGAT-115-054146_2006-03-17_466</subject> <line/> </ProcessInformationCustomer> </EtfHeader> <DocumentaryCreditNotification xmlns:dt=" http://www.bolero.net/io/xmlns/datatype/1.0"> <Header xmlns:cmp=" http://www.bolero.net/io/xmlns/header/1.0"> <cmp:DocumentID> <cmp:RID>CREDIT-SUISSE-USERTEST.IMAP</cmp:RID> <cmp:GeneralID>SGAT-115-054146A0</cmp:GeneralID> <cmp:Version>V1.0</cmp:Version> </cmp:DocumentID> <cmp:DocType> <cmp:DocTypeCode>466</cmp:DocTypeCode> </cmp:DocType> <cmp:Status>Final</cmp:Status> </Header> <Body> <Party> <partyType>APP</partyType> <partyName>Glencore International AG</partyName> <PartyId> <partyIdType>INT</partyIdType> <partyIdValue>083503619132</partyIdValue> </PartyId> <PartyAddress> <line>Glencore International AG</line> <line>Baarermattstrasse 3</line> <line>Postfach 777</line> <line>6341 Baar</line> </PartyAddress> <Contact> <contactType>TEL</contactType> <contactValue>(41)41-7092947</contactValue> </Contact> <Contact> <contactType>FAX</contactType> <contactValue>(41)41-7093000</contactValue> </Contact> </Party> <Party> <partyType>ADB</partyType> <partyName>Bangkok Bank Public Company</partyName> <PartyId> <partyIdType>INT</partyIdType> <partyIdValue>083509920500</partyIdValue> </PartyId> <PartyAddress> <line>Bangkok Bank Public Company</line> <line>Limited</line> <line>333 Silom Road</line> <line>TH-Bangkok 10500</line> </PartyAddress> </Party> <Party> <partyType>BEN</partyType> <partyName>Eximp</partyName> <PartyId> <partyIdType>INT</partyIdType> <partyIdValue/> </PartyId> <PartyAddress> <line>Eximp</line> <line>Mainstreet</line> <line>London</line> </PartyAddress> </Party> <Party> <partyType>ISB</partyType> <partyName>Credit Suisse</partyName> <PartyId> <partyIdType>INT</partyIdType> <partyIdValue>083510483505</partyIdValue> </PartyId> <PartyAddress> <line>Credit Suisse</line> <line>Hauptsitz</line> <line>8070 Zuerich</line> </PartyAddress> </Party> <Date> <dateType>EXP</dateType> <dateValue>2006-06-30</dateValue> </Date> <Date> <dateType>ISS</dateType> <dateValue>2006-03-17</dateValue> </Date> <Reference> <referenceType>999</referenceType> <referenceValue>RS170306T50</referenceValue> <ReferenceSource> <partyType>APP</partyType> </ReferenceSource> </Reference> <Reference> <referenceType>466</referenceType> <referenceValue>SGAT 115-054146</referenceValue> <ReferenceSource> <partyType>ISB</partyType> </ReferenceSource> </Reference> <Location> <locationType>SHF</locationType> <LocationId> <locationName>Mombasa</locationName> </LocationId> </Location> <Location> <locationType>SHT</locationType> <LocationId> <locationName>Basel</locationName> </LocationId> </Location> <Location> <locationType>DEL</locationType> <LocationId> <locationName>Basel</locationName> </LocationId> </Location> <DocumentaryCreditNotificationParticulars> <documentaryCreditType>IRREVOCABLE</documentaryCreditType> <documentaryCreditSubType>NOR</documentaryCreditSubType> <DocumentaryCreditAmount> <amount>3000.00</amount> <currencyCode>CHF</currencyCode> </DocumentaryCreditAmount> <DocumentaryCreditAmountVariance> <documentaryCreditAmountVarianceType>NOT EXCEEDING</documentaryCreditAmountVarianceType> <PercentageVariance/> </DocumentaryCreditAmountVariance> <DocumentaryCreditAvailability> <DocumentaryCreditAvailableWith> <documentaryCreditAvailabilityParty>ISB</documentaryCreditAvailabilityParty> </DocumentaryCreditAvailableWith> <documentaryCreditAvailabilityType>BY PAYMENT</documentaryCreditAvailabilityType> </DocumentaryCreditAvailability> <DocumentaryCreditValidity> <documentaryCreditValidityParty>ISB</documentaryCreditValidityParty> </DocumentaryCreditValidity> <confirmationInstructionType>WITHOUT</confirmationInstructionType> <PresentationPeriod> <line>2 weeks</line> </PresentationPeriod> <ShipmentPeriod> <line>One week</line> </ShipmentPeriod> <PartialShipment> <partialShipmentIndicator>FALSE</partialShipmentIndicator> </PartialShipment> <Transhipment> <transhipmentIndicator>FALSE</transhipmentIndicator> </Transhipment> <Incoterms> <incotermsCode>CIF</incotermsCode> </Incoterms> <documentIssueType>SWT</documentIssueType> <amendmentAdviceRefusalIndicator>FALSE</amendmentAdviceRefusalIndicator> </DocumentaryCreditNotificationParticulars> <Goods> <GoodsDescription> <line>coffee</line> </GoodsDescription> </Goods> <Documentation> <line>Invoice</line> </Documentation> </Body> </DocumentaryCreditNotification> </EtfDocumentaryCreditNotification>

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>