title: Tree navigation with XML and XSLT
body: <p>I have this tree structured XML (toc.xml):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;toc&gt;
    &lt;item name="top" key="4294967296" subkey="1"&gt;
      &lt;item name="child1" key="4294967611" subkey=""&gt;
        &lt;item name="child2-1" key="4294961611" subkey=""&gt;
          &lt;item name="child3-1" key="4294967613" subkey=""/&gt;
          &lt;item name="child3-2" key="4294967612" subkey=""/&gt;
        &lt;/item&gt;
        &lt;item name="child2-2" key="4294962611" subkey=""&gt;
          &lt;item name="d" key="4294974806" subkey=""/&gt;
        &lt;/item&gt;
        &lt;item name="child2-3" key="4294963611" subkey=""&gt;
          &lt;item name="d" key="4294967661" subkey=""/&gt;
          &lt;item name="PI" key="4294967659" subkey=""/&gt;
          &lt;item name="q" key="4294967660" subkey=""/&gt;
        &lt;/item&gt;
        &lt;item name="child2-4" key="4294964611" subkey=""/&gt;
        &lt;item name="child2-5" key="4294965611" subkey=""&gt;
          &lt;item name="bb" key="4294967616" subkey=""/&gt;
          &lt;item name="bb" key="4294967620" subkey=""/&gt;
          &lt;item name="f" key="4294967615" subkey=""/&gt;
        &lt;/item&gt;
      &lt;/item&gt;
    &lt;/item&gt;
  &lt;/toc&gt;
  </code></pre>
  
  <p>Each key will be unique in the document.</p>
  
  <p>I have an XSLT that imports the toc XML and tries to output the navigation:</p>
  
  <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    &lt;xsl:output method="html" indent="no" /&gt;
    &lt;xsl:variable name="id" select="/member/@id" /&gt;
  
    &lt;xsl:template match="/member"&gt;
      &lt;html&gt;
        &lt;head&gt;
          &lt;title&gt;&lt;xsl:value-of select="/member/name"/&gt;&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
          &lt;div class="navigation"&gt;
            &lt;xsl:apply-templates select="document('toc.xml')" /&gt;
          &lt;/div&gt;
          &lt;div class="content"&gt;
            &lt;xsl:apply-templates /&gt;
          &lt;/div&gt;
        &lt;/body&gt;
      &lt;/html&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl&gt;
  </code></pre>
  
  <p>And would like to find a node and output the following HTML inside the HTML file:</p>
  
  <pre><code>...html...
  &lt;div class="navigation"&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;top&lt;/a&gt;&lt;ul&gt;
        &lt;li&gt;&lt;a href="#"&gt;child1&lt;/li&gt;&lt;ul&gt;
          &lt;li&gt;&lt;a href="#"&gt;child2&lt;/li&gt;&lt;ul&gt;
              &lt;li&gt;&lt;a href="#"&gt;child3-1&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#"&gt;child3-2&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;&lt;/li&gt;
          &lt;/ul&gt;&lt;/li&gt;
        &lt;/ul&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  ...more html...
  </code></pre>
  
  <p>Basically I want to search for a node: item[@key='4294967611'] and output all of the parent nodes and the direct children.</p>
  
  <p>I feel this should be really easy but I'm struggling to find information about how to do this. My XSLT knowledge isn't great.</p>