title: Doing an XSL transform of a branch of a Docbook element tree
body: <p>I'd like to use the docbook XSL stylesheets to render various parts of a document, without transforming the entire thing.</p>
  
  <p>The complication is that some of these parts have <code>&lt;footnoteref&gt;</code> elements whose <code>linkend</code> attributes are not located within the same chunk.  In other words, I want to process a branch of the tree which includes the <code>&lt;footnoteref&gt;</code>s but not the <code>&lt;footnote&gt;</code> elements they reference.</p>
  
  <p>My attempts to do this using the Python <code>lxml</code> package have yielded this error message:</p>
  
  <pre><code>XSLTApplyError                            Traceback (most recent call last)
  
  /var/www/mpd/&lt;ipython console&gt; in &lt;module&gt;()
  
  /var/www/mpd/&lt;ipython console&gt; in &lt;genexpr&gt;((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.
  </code></pre>
  
  <p>This happens in response to, e.g. <code>etree.XSLT(etree.parse('docbook.xsl'))(some_element)</code>.</p>
  
  <p>I'm using the normal <code>xhtml</code> stylesheets.  I wouldn't expect it to matter which stylesheet I'm using, though.</p>
  
  <p>Is there a supported way to do this?  Or am I expected to, for example, apply an XSLT transform on the document to change the <code>&lt;footnoteref&gt;</code> element to a <code>&lt;footnote&gt;</code> element before doing this render?  But then that wouldn't work, because then there would be multiple <code>&lt;footnote&gt;</code> tags with the same ID.  It would have to only do that <code>&lt;footnoteref&gt;</code> to <code>&lt;footnote&gt;</code> transformation if the <code>&lt;footnote&gt;</code> tag was not being included in the resulting tree.  Which I would expect to be happening already.  But hopefully I just missed a switch somewhere.</p>
  
  <p><strong><em>edit</em></strong></p>
  
  <p>Thanks to @Jukka's answer, I've figured out that I can pass the <a href="http://docbook.sourceforge.net/release/xsl/current/doc/html/rootid.html" rel="nofollow noreferrer">rootid</a> parameter to tell the XSLT processor to just render that ID.  However, it does so all too faithfully, producing output that just references the footnote with the same fragment that would be useful if the document were being rendered as a single HTML page.  EG</p>
  
  <pre><code>&gt;&gt;&gt; xsl_url_html = 'http://docbook.sourceforge.net/release/xsl/current/html/docbook.xsl'
  &gt;&gt;&gt; from lxml import etree
  &gt;&gt;&gt; consume_result = etree.XSLT(etree.parse(xsl_url_xhtml))(
          etree.parse(my_xml_file), rootid=etree.XSLT.strparam("command_consume"))
  &gt;&gt;&gt; etree.tostring(consume_result).split('\n')
  ['&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;',
   '&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;head&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=ASCII" /&gt;&lt;title&gt;&lt;/title&gt;&lt;meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /&gt;&lt;/head&gt;&lt;body&gt;&lt;dt&gt;&lt;a id="command_consume"&gt;&lt;/a&gt;&lt;span class="term"&gt;',
   '            &lt;div class="cmdsynopsis"&gt;&lt;p&gt;&lt;code class="command"&gt;consume&lt;/code&gt;  {&lt;em class="replaceable"&gt;&lt;code&gt;STATE&lt;/code&gt;&lt;/em&gt;}&lt;/p&gt;&lt;/div&gt;',
   '          &lt;/span&gt;&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;',
   '              &lt;sup&gt;[&lt;a href="#ftn.since_0_15" class="footnoteref"&gt;2&lt;/a&gt;]&lt;/sup&gt;',
   '              Sets consume state to &lt;code class="varname"&gt;STATE&lt;/code&gt;,',
   '              &lt;code class="varname"&gt;STATE&lt;/code&gt; should be 0 or 1.',
   '\t      When consume is activated, each song played is removed from playlist.',
   '            &lt;/p&gt;&lt;/dd&gt;&lt;/body&gt;&lt;/html&gt;']
  </code></pre>
  
  <p>Maybe there is another parameter that will cause the footnotes to be displayed on the same page, preferably numbered from 1?  I guess it would probably be in <a href="http://docbook.sourceforge.net/release/xsl/current/doc/html/" rel="nofollow noreferrer">this list</a> somewhere.  I'll go through it when I get some more time.</p>