GenerateDefaultAttributeRemover.xslt
Jump to navigation
Jump to search
If you want to remove default attributes from your TEI P4 XML files, this may be for you. If you just use vanilla TEI Lite P4, then you could just use Remove-Default-Attributes.xslt.
This method is more complicated, but works for any TEI P4 DTD, and probably for lots of non-TEI DTDs as well.
- If you don't have one already, generate a flat version of your DTD with the pizza chef.
- Convert the flat DTD to RELAX NG (XML syntax). This may be done using trang; oXygen gives a nice GUI front-end to trang from the "Tools" menu (it's called "Trang converter…".)
- Resolve references to attribute lists in the resulting RELAX NG schema by applying relaxng_refAtt_resolver.xslt.
- Feed the resulting schema as input to this stylesheet. The output will be a stylesheet that will remove default attributes from instances.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns="http://relaxng.org/ns/structure/1.0" xmlns:rng="http://relaxng.org/ns/structure/1.0"> <!-- Reads in a TEI RELAX NG (XML syntax) file that has been pre-processed such that references to <define> of attribute lists have been resolved (i.e., via relaxng_refAtt_resolver.xslt), and writes out XSLT that removes default attributes from instances. Created 2008-03-20 by Syd Bauman --> <xsl:output indent="yes" method="xml" omit-xml-declaration="no"/> <xsl:template match="/"> <!-- output an XSLT 1.0 stylesheet --> <xsl:element name="xsl:stylesheet"> <xsl:attribute name="version">1.0</xsl:attribute> <!-- would we put namespace decls here, if we needed them? --> <xsl:text> </xsl:text> <xsl:comment> This stylesheet generated by generate_default_attr_remover.xslt </xsl:comment> <xsl:text> </xsl:text> <!-- spit out a generic copy-everything template ala http://www.tei-c.org/wiki/index.php/Copy-All.xsl --> <xsl:comment> Generic copy-everything template </xsl:comment> <xsl:element name="xsl:template"> <xsl:attribute name="match">*|@*|processing-instruction()|comment()</xsl:attribute> <xsl:element name="xsl:copy"> <xsl:element name="xsl:apply-templates"> <xsl:attribute name="select" >*|@*|text()|processing-instruction()|comment()</xsl:attribute> </xsl:element> </xsl:element> </xsl:element> <xsl:text> </xsl:text> <xsl:comment> get rid of TEIform= attrs whose value match GI </xsl:comment> <xsl:comment> see http://www.tei-c.org/wiki/index.php/Remove-Default-Attributes.xslt </xsl:comment> <xsl:comment> for detailed comments. </xsl:comment> <xsl:element name="xsl:template"> <xsl:attribute name="match">@TEIform</xsl:attribute> <xsl:element name="xsl:if"> <xsl:attribute name="test">not( . = name(..) )</xsl:attribute> <xsl:element name="xsl:attribute"> <xsl:attribute name="name">TEIform</xsl:attribute> <xsl:element name="xsl:value-of"> <xsl:attribute name="select">.</xsl:attribute> </xsl:element> </xsl:element> </xsl:element> </xsl:element> <xsl:text> </xsl:text> <xsl:comment> When we hit an element/attribute combination that has a default value </xsl:comment> <xsl:comment> in the schema, test to see if the value specified is the same as the </xsl:comment> <xsl:comment> default value in the schema. If it is not, write out the attribute as </xsl:comment> <xsl:comment> it was specified in the instance; if it is, do nothing, thus dropping </xsl:comment> <xsl:comment> the entire attribute from the output. </xsl:comment> <xsl:text> </xsl:text> <!-- now, for each default attribute found in the input schema ... --> <xsl:apply-templates select="//rng:attribute[@a:defaultValue]"/> </xsl:element> </xsl:template> <!-- ... apply a template --> <xsl:template match="rng:attribute[@a:defaultValue]"> <xsl:if test="not(@name='TEIform')"> <xsl:text> </xsl:text> <xsl:element name="xsl:template"> <xsl:attribute name="match"> <xsl:value-of select="ancestor::rng:element[1]/@name"/> <xsl:text>/@</xsl:text> <xsl:value-of select="@name"/> </xsl:attribute> <xsl:element name="xsl:if"> <xsl:attribute name="test"> <xsl:text>not( . = '</xsl:text> <xsl:value-of select="@a:defaultValue"/> <xsl:text>' )</xsl:text> </xsl:attribute> <xsl:element name="xsl:attribute"> <xsl:attribute name="name"> <xsl:value-of select="@name"/> </xsl:attribute> <xsl:element name="xsl:value-of"> <xsl:attribute name="select">.</xsl:attribute> </xsl:element> </xsl:element> </xsl:element> </xsl:element> </xsl:if> </xsl:template> </xsl:stylesheet>