Note that there are some explanatory texts on larger screens.

plurals
  1. POXSLT-1.0 can a variable be used to access to other nodes?
    text
    copied!<p>With a simple XML like this</p> <pre><code>&lt;value&gt; &lt;num&gt; &lt;accession&gt;111&lt;/accession&gt; &lt;sequence&gt;AAA&lt;/sequence&gt; &lt;score&gt;4000&lt;/score&gt; &lt;/num&gt; &lt;/value&gt; </code></pre> <p>I want to know if it is possible to access to a particular node from a node previously stored in a variable. The XSLT code is very short and explains better what I want to say</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/&gt; &lt;xsl:template match="/value/num"&gt; &lt;xsl:variable name="node"&gt; &lt;xsl:copy-of select="current()"/&gt; &lt;/xsl:variable&gt; &lt;root&gt; &lt;xsl:copy-of select="$node"/&gt; &lt;/root&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>So I store the node in the variable "node". Then I can print the contents of the node with <code>$node</code>.</p> <p>(EDIT) XML output</p> <pre><code>&lt;root&gt; &lt;num&gt; &lt;accession&gt;111&lt;/accession&gt; &lt;sequence&gt;AAA&lt;/sequence&gt; &lt;score&gt;4000&lt;/score&gt; &lt;/num&gt; &lt;/root&gt; </code></pre> <p>What I want to do is to print the contents of a sub-node, like this</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/&gt; &lt;xsl:template match="/value/num"&gt; &lt;xsl:variable name="node"&gt; &lt;xsl:copy-of select="current()"/&gt; &lt;/xsl:variable&gt; &lt;root&gt; &lt;xsl:copy-of select="$node/accession"/&gt; &lt;/root&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>But it gives an error (Component returned failure code: 0x80600008 [nsIXSLTProcessor.transformToFragment]) (check <a href="http://xsltcake.com/slices/lH3JVa/3" rel="nofollow noreferrer">here</a>)</p> <p>(EDIT) The XML that I would want is</p> <pre><code>&lt;root&gt; &lt;accession&gt;111&lt;/accession&gt; &lt;/root&gt; </code></pre> <p>NOTE: The question is not how can I get this output. The question is how, using a variable as in the XSLT provided, can I get this output.</p> <p>(EDIT:SOLVED) Actually it is possible, but as pointed out in the comments, the value of a variable has to be assigned with the "select" attribute if a node-set is required. So this code was not working since the variable had a tree fragment instead of a node-set stored in it (read more information <a href="https://stackoverflow.com/questions/5353668/how-to-store-node-set-in-a-variable-based-on-choose-when-condition">here</a>)</p> <p>Thanks!</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