我想使用 docbook XSL 样式表来呈现文档的各个部分,而无需转换整个内容。
复杂之处在于其中一些部分的<footnoteref>
元素的linkend
属性不在同一个块中。换句话说,我想处理包含<footnoteref>
s 但不<footnote>
包含它们引用的元素的树的分支。
我尝试使用 Pythonlxml
包执行此操作产生了以下错误消息:
XSLTApplyError Traceback (most recent call last)
/var/www/mpd/<ipython console> in <module>()
/var/www/mpd/<ipython console> in <genexpr>((elt,))
/usr/lib/python2.6/dist-packages/lxml/etree.so in lxml.etree.XSLT.__call__ (src/lxml/lxml.etree.c:109204)()
XSLTApplyError: Internal error in xsltKeyFunction(): Could not get the document info of a context doc.
这发生在响应,例如etree.XSLT(etree.parse('docbook.xsl'))(some_element)
。
我正在使用普通的xhtml
样式表。不过,我不希望它对我使用的样式表很重要。
有支持的方法吗?或者,例如,我是否希望在文档上应用 XSLT 转换以在执行此渲染之前将<footnoteref>
元素更改为元素?<footnote>
但是那样就行不通了,因为这样就会有多个<footnote>
标签具有相同的 ID。如果标签未包含在结果树中,则必须仅对转换执行此<footnoteref>
操作。我希望这已经发生了。但希望我只是错过了某个地方的开关。<footnote>
<footnote>
编辑
感谢@Jukka 的回答,我发现我可以传递rootid参数来告诉 XSLT 处理器只呈现该 ID。但是,它非常忠实地执行此操作,生成的输出仅引用具有相同片段的脚注,如果将文档呈现为单个 HTML 页面,这将很有用。例如
>>> xsl_url_html = 'http://docbook.sourceforge.net/release/xsl/current/html/docbook.xsl'
>>> from lxml import etree
>>> consume_result = etree.XSLT(etree.parse(xsl_url_xhtml))(
etree.parse(my_xml_file), rootid=etree.XSLT.strparam("command_consume"))
>>> etree.tostring(consume_result).split('\n')
['<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
'<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ASCII" /><title></title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /></head><body><dt><a id="command_consume"></a><span class="term">',
' <div class="cmdsynopsis"><p><code class="command">consume</code> {<em class="replaceable"><code>STATE</code></em>}</p></div>',
' </span></dt><dd><p>',
' <sup>[<a href="#ftn.since_0_15" class="footnoteref">2</a>]</sup>',
' Sets consume state to <code class="varname">STATE</code>,',
' <code class="varname">STATE</code> should be 0 or 1.',
'\t When consume is activated, each song played is removed from playlist.',
' </p></dd></body></html>']
也许还有另一个参数会导致脚注显示在同一页面上,最好从 1 开始编号?我想它可能会在这个列表中的某个地方。当我有更多时间时,我会完成它。