xslt - 将“排版”脚注转换为 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 <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 <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 > @namest]" use="number(sup)"/>
但我不确定如何最好地使用它们?
对于每个索引的第一个实例的优雅匹配模式有什么建议吗?
解决方案
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
操作员检查节点身份。
推荐阅读
- python - Django:必须给出 hex、bytes、bytes_le、fields 或 int 参数之一
- java - 打开主 shell 后立即打开另一个 SWT Shell
- python - 使用 python 进行活动选择
- php - PHP循环查询以执行条件插入或更新
- java - 程序未运行时 Desktop.setOpenFileHandler 不起作用
- sparql - 使用过滤器的简单 SPARQL 查询的性能
- javascript - Slick Carousel + Accordion 格式问题
- c# - IIS 和 C# 线程锁
- swift - 在表格视图中搜索 CNContacts
- c - C:如何找出某个字符串中任意序列中存在一定数量的字符?