Skip to content

Instantly share code, notes, and snippets.

@dwcramer
Last active October 11, 2017 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwcramer/84cf822aebf98e42892fd14776e9c9d1 to your computer and use it in GitHub Desktop.
Save dwcramer/84cf822aebf98e42892fd14776e9c9d1 to your computer and use it in GitHub Desktop.
Fixup of DocBook CALS tables to make DeltaXML happy
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://docbook.org/ns/docbook"
xmlns:db="http://docbook.org/ns/docbook"
exclude-result-prefixes="xs db"
version="2.0">
<!--
DeltaXML is much better at diffing tables if the following things are true:
1. All <entry> elements have a colname that maps it to the corresponding
colspec for the ancestor tgroup. This allows DeltaXML figure out which
column was deleted or added if there are fewer columns in one of the two
documents being compared.
2. All <entry> elements contain a block element like a <para>. This allows
DeltaXML to add an extra <para> inside the <entry> if it needs to indicate
that one <para> was added and another deleted. Otherwise, it sometimes needs
to duplicate an <entry> where one is marked added and another deleted. This
throws the count of the columns off and disturbs the integrity of the table.
-->
<xsl:template match="db:entry">
<xsl:variable name="column-number"><xsl:number/></xsl:variable>
<xsl:variable name="column-name">
<xsl:choose>
<xsl:when test="preceding-sibling::db:entry/@nameend">
<!-- If a cell spans columns, figure out the colnum of the end of the most recent span,
then add to that the count of the cells between this one and it. -->
<xsl:variable name="preceding-sibling-nameend" select="(preceding-sibling::db:entry[@nameend])[last()]/@nameend"/>
<xsl:variable name="preceding-sibling-colnum" select="ancestor::db:tgroup/db:colspec[@colname = $preceding-sibling-nameend]/@colnum"/>
<xsl:variable name="preceing-sibling-count" select="count(preceding-sibling::db:entry[not(following-sibling::db:entry[@nameend = $preceding-sibling-nameend])])"/>
<xsl:value-of select="ancestor::db:tgroup/db:colspec[@colnum = $preceding-sibling-colnum + $preceing-sibling-count]/@colname"/>
</xsl:when>
<xsl:otherwise>
<!-- Easy, just count the columns -->
<xsl:value-of select="ancestor::db:tgroup/db:colspec[@colnum = $column-number]/@colname"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="not(@colname) and not(@namest) and $column-name = ''">
<xsl:message terminate="yes">
ERROR: Malformed table. Could not figure out what colname attribute to add to an entry.
</xsl:message>
</xsl:if>
<xsl:copy>
<xsl:if test="not(@namest)">
<xsl:attribute name="colname">
<xsl:value-of select="$column-name"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="processing-instruction()|comment()"/>
<xsl:choose>
<xsl:when test=" (: all the block elements in DocBook that are legal in an entry :)
./db:address or
./db:anchor or
./db:annotation or
./db:bibliolist or
./db:blockquote or
./db:bridgehead or
./db:calloutlist or
./db:caution or
./db:classsynopsis or
./db:cmdsynopsis or
./db:constraintdef or
./db:constructorsynopsis or
./db:destructorsynopsis or
./db:epigraph or
./db:equation or
./db:example or
./db:fieldsynopsis or
./db:figure or
./db:formalpara or
./db:funcsynopsis or
./db:glosslist or
./db:important or
./db:indexterm or
./db:informalequation or
./db:informalexample or
./db:informalfigure or
./db:informaltable or
./db:itemizedlist or
./db:literallayout or
./db:mediaobject or
./db:methodsynopsis or
./db:msgset or
./db:note or
./db:orderedlist or
./db:para or
./db:procedure or
./db:productionset or
./db:programlisting or
./db:programlistingco or
./db:qandaset or
./db:remark or
./db:revhistory or
./db:screen or
./db:screenco or
./db:screenshot or
./db:segmentedlist or
./db:sidebar or
./db:simpara or
./db:simplelist or
./db:synopsis or
./db:table or
./db:task or
./db:tip or
./db:variablelist or
./db:warning
">
<xsl:apply-templates select="*|text()"/>
</xsl:when>
<xsl:otherwise>
<para><xsl:apply-templates select="*|text()"/></para>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
<xsl:template match="db:colspec[not(@colname)]">
<xsl:message terminate="yes">
ERROR: Found a colspec element without a colname attribute. Please fix the markup of your table.
</xsl:message>
</xsl:template>
<xsl:template match="db:colspec[not(@colnum)]">
<xsl:message terminate="yes">
ERROR: Found a colspec element without a colnum attribute. Please fix the markup of your table.
</xsl:message>
</xsl:template>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<appendix xml:id="appendix.ywf_qsy_mbb" xmlns="http://docbook.org/ns/docbook">
<title>foo</title>
<para>lsadfj<table frame="all" xml:id="table.w34_5sy_mbb">
<title/>
<tgroup cols="10">
<colspec colname="c1" colnum="1" colwidth="1*"/>
<colspec colname="c2" colnum="2" colwidth="1*"/>
<colspec colname="c3" colnum="3" colwidth="1*"/>
<colspec colname="c4" colnum="4" colwidth="1*"/>
<colspec colname="c5" colnum="5" colwidth="1*"/>
<colspec colname="c6" colnum="6" colwidth="1*"/>
<colspec colname="c7" colnum="7" colwidth="1*"/>
<colspec colname="c8" colnum="8" colwidth="1*"/>
<colspec colname="c9" colnum="9" colwidth="1*"/>
<colspec colname="c10" colnum="10" colwidth="1*"/>
<thead>
<row>
<entry>a</entry>
<entry>b</entry>
<entry>c</entry>
<entry>d</entry>
<entry>e</entry>
<entry>f</entry>
<entry>g</entry>
<entry>h</entry>
<entry>i</entry>
<entry>j</entry>
</row>
</thead>
<tbody>
<row>
<entry>aa</entry>
<entry namest="c2" nameend="c3">bb</entry>
<entry>dd</entry>
<entry>ee</entry>
<entry namest="c6" nameend="c7">ff</entry>
<entry>hh</entry>
<entry>ii</entry>
<entry>jj</entry>
</row>
<row>
<entry>aa</entry>
<entry>bb</entry>
<entry>cc</entry>
<entry namest="c4" nameend="c5">dd</entry>
<entry>ff</entry>
<entry/>
<entry>hh</entry>
<entry namest="c9" nameend="c10">ii</entry>
</row>
</tbody>
</tgroup>
</table></para>
</appendix>
<?xml version="1.0" encoding="UTF-8"?><appendix xmlns="http://docbook.org/ns/docbook" xml:id="appendix.ywf_qsy_mbb">
<title>foo</title>
<para>lsadfj<table frame="all" xml:id="table.w34_5sy_mbb">
<title/>
<tgroup cols="10">
<colspec colname="c1" colnum="1" colwidth="1*"/>
<colspec colname="c2" colnum="2" colwidth="1*"/>
<colspec colname="c3" colnum="3" colwidth="1*"/>
<colspec colname="c4" colnum="4" colwidth="1*"/>
<colspec colname="c5" colnum="5" colwidth="1*"/>
<colspec colname="c6" colnum="6" colwidth="1*"/>
<colspec colname="c7" colnum="7" colwidth="1*"/>
<colspec colname="c8" colnum="8" colwidth="1*"/>
<colspec colname="c9" colnum="9" colwidth="1*"/>
<colspec colname="c10" colnum="10" colwidth="1*"/>
<thead>
<row>
<entry colname="c1"><para>a</para></entry>
<entry colname="c2"><para>b</para></entry>
<entry colname="c3"><para>c</para></entry>
<entry colname="c4"><para>d</para></entry>
<entry colname="c5"><para>e</para></entry>
<entry colname="c6"><para>f</para></entry>
<entry colname="c7"><para>g</para></entry>
<entry colname="c8"><para>h</para></entry>
<entry colname="c9"><para>i</para></entry>
<entry colname="c10"><para>j</para></entry>
</row>
</thead>
<tbody>
<row>
<entry colname="c1"><para>aa</para></entry>
<entry namest="c2" nameend="c3"><para>bb</para></entry>
<entry colname="c4"><para>dd</para></entry>
<entry colname="c5"><para>ee</para></entry>
<entry namest="c6" nameend="c7"><para>ff</para></entry>
<entry colname="c8"><para>hh</para></entry>
<entry colname="c9"><para>ii</para></entry>
<entry colname="c10"><para>jj</para></entry>
</row>
<row>
<entry colname="c1"><para>aa</para></entry>
<entry colname="c2"><para>bb</para></entry>
<entry colname="c3"><para>cc</para></entry>
<entry namest="c4" nameend="c5"><para>dd</para></entry>
<entry colname="c6"><para>ff</para></entry>
<entry colname="c7"><para/></entry>
<entry colname="c8"><para>hh</para></entry>
<entry namest="c9" nameend="c10"><para>ii</para></entry>
</row>
</tbody>
</tgroup>
</table></para>
</appendix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment