Note that there are some explanatory texts on larger screens.

plurals
  1. POSelecting attribute of ancestor; "../.." works, "ancestor::<tagname>" doesn't
    text
    copied!<p>I'm running through an XML document, selecting all the elements, and creating links based on the ancestor which is usually two nodes up in the tree, but occasionally 3 or 4 nodes up. For the majority of the elements, using <code>&lt;xsl:value-of select="translate(../../@name,$uc,$lc)" /&gt;</code> works just fine, but for the cases where the ancestor is 3 or so nodes up, I'd like to use <code>&lt;xsl:value-of select="translate(ancestor::package/@name,$uc,$lc)" /&gt;</code>, but this doesn't work.</p> <p>I'm using xsltproc from Ruby to do my XSL transforms.</p> <p>Sample tree (yes, it has XSLT in it, no, I'm not trying to process it):</p> <pre><code>&lt;package name="blork!" xmlns="http://xml.snapin.com/XBL"&gt; &lt;xsl:template name="doSomething"&gt; &lt;tokens&gt; &lt;token name="text-from-resource" export="public" /&gt; &lt;/tokens&gt; &lt;/xsl:template&gt; &lt;/package&gt; </code></pre> <p>The XSL I'm using:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s4="http://xml.snapin.com/XBL"&gt; &lt;xsl:template match="/"&gt; &lt;xsl:if test="count(//s4:token) &gt;0"&gt; &lt;xsl:text&gt;Tokens!&lt;/xsl:text&gt; &lt;xsl:for-each select="//s4:token"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="@export='global'" /&gt; &lt;xsl:otherwise&gt; &lt;xsl:value-of select="translate(ancestor::s4:package/@name,$uc,$lc)" /&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:for-each&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><em>Edit:</em> Ah, right, forgot the namespace on the select. The parser's finding that ancestor properly for most cases, but it still can't find it when there's an xsl: node in there, and the target file has no namespace for xsl. I'd prefer not to modify the target file, because it's production code---I'm just writing an autodoc tool.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload