首页 > 解决方案 > 将“排版”脚注转换为 docbook 样式

问题描述

我有一堆包含 calstables 的内容,其中脚注是使用排版技术完成的,每个索引都使用<sup/>元素设置,每个脚注都包含在表格末尾的自己的跨行中。

我想将这些转换为使用像这样这样的Docbook脚注标记

示例数据如下(我添加了<from/>元素<to/>来显示它来自哪里以及它需要去哪里)

<testdata>
   <from>
      <table>
         <tgroup cols="3">
            <colspec colname="1" colnum="1" colwidth="39pt" align="left"/>
            <colspec colname="2" colnum="2" colwidth="39pt" align="center"/>
            <colspec colname="3" colnum="3" colwidth="39pt" align="center"/>
            <thead>
               <row valign="bottom">
                  <entry>Item</entry>
                  <entry>ItemA</entry>
                  <entry>ItemB<sup>1</sup></entry>
               </row>
            </thead>
            <tbody>
               <row valign="top">
                  <entry>Entry 1</entry>
                  <entry>60</entry>
                  <entry>3.2</entry>
               </row>
               <row>
                  <entry>Entry A</entry>
                  <entry>150</entry>
                  <entry>3.55<sup>2</sup></entry>
               </row>
               <row>
                  <entry>This entry</entry>
                  <entry>260<sup>3</sup></entry>
                  <entry>3.55<sup>2</sup></entry>
               </row>
               <row>
                  <entry align="left" namest="1" nameend="3"><sup>1</sup> LAT</entry>
               </row>
               <row>
                  <entry align="left" namest="1" nameend="3"><sup>2</sup> Itemvalue &lt;24.5 m. Also see note below.</entry>
               </row>
               <row>
                  <entry align="left" namest="1" nameend="3"><sup>3</sup> Ramp up 19.8 m.</entry>
               </row>
            </tbody>
         </tgroup>
      </table>   
   </from>
   <to>
      <table>
         <tgroup cols="3">
            <colspec colname="1" colnum="1" colwidth="39pt" align="left"/>
            <colspec colname="2" colnum="2" colwidth="39pt" align="center"/>
            <colspec colname="3" colnum="3" colwidth="39pt" align="center"/>
            <thead>
               <row valign="bottom">
                  <entry>Item</entry>
                  <entry>ItemA</entry>
                  <entry>ItemB<footnote id="1">LAT</footnote></entry>
               </row>
            </thead>
            <tbody>
               <row valign="top">
                  <entry>Entry 1</entry>
                  <entry>60</entry>
                  <entry>3.2</entry>
               </row>
               <row>
                  <entry>Entry A</entry>
                  <entry>150</entry>
                  <entry>3.55<footnote id="2">Itemvalue &lt;24.5 m. Also see note below.</footnote></entry>
               </row>
               <row>
                  <entry>This entry</entry>
                  <entry>260<footnote id="3">Ramp up 19.8 m.</footnote></entry>
                  <entry>3.55<footnoteref linkend="2"/></entry>
               </row>
            </tbody>
         </tgroup>
      </table>   
   </to>
</testdata>

逻辑相当简单明了:

1. Copy everthing to output, unless ...
2. it contains a `<sup/>` element, in which case either
    1. if it has a `@nameend` attribute, do nothing, or 
    2. if it is the first instance of this index, create a footnote element with an `@id` attribute, grabbing the content from the matching straddle row, or
    3. if it's not the first instance, create a footnoteref element, with a matching `@linkend` attribute

当然还有一堆错误检查,但我现在不太担心。

我可以使用一堆单独的匹配模式来解决上面的每个#2,比如

<xsl:template match="sup[text() = '1'][1]">
<xsl:template match="sup[text() = '2'][1]">
<xsl:template match="sup[text() = '3'][1]">

但我认为必须有一个更优雅的匹配模式(也许使用键?)来匹配使用的每个索引的第一个实例,但对于我的生活,我想不出它可能是什么。

到目前为止,我已经定义了 2 个键

<xsl:key name="fn-indices" match="sup" use="number(.)"/>
<xsl:key name="fn-text" match="entry[sup][@nameend &gt; @namest]" use="number(sup)"/>

但我不确定如何最好地使用它们?

对于每个索引的第一个实例的优雅匹配模式有什么建议吗?

标签: xsltdocbook

解决方案


sup[. is key('fn-indices', number())[1]]应该匹配每个“组”中的第一项,所以你应该能够替换

<xsl:template match="sup[text() = '1'][1]">
<xsl:template match="sup[text() = '2'][1]">
<xsl:template match="sup[text() = '3'][1]">

<xsl:template match="sup[. is key('fn-indices', number())[1]]">

is操作员检查节点身份。


推荐阅读