GWorking
GWorking

Reputation: 4341

XSLT-1.0 can a variable be used to access to other nodes?

With a simple XML like this

<value>
    <num>
        <accession>111</accession>
        <sequence>AAA</sequence>
        <score>4000</score>
    </num>
</value>

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/value/num">
        <xsl:variable name="node">
            <xsl:copy-of select="current()"/>
        </xsl:variable>
        <root>
          <xsl:copy-of select="$node"/>
        </root>
    </xsl:template>
</xsl:stylesheet>

So I store the node in the variable "node". Then I can print the contents of the node with $node.

(EDIT) XML output

<root>
    <num>
        <accession>111</accession>
        <sequence>AAA</sequence>
        <score>4000</score>
    </num>
</root>

What I want to do is to print the contents of a sub-node, like this

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/value/num">
        <xsl:variable name="node">
            <xsl:copy-of select="current()"/>
        </xsl:variable>
        <root>
          <xsl:copy-of select="$node/accession"/>
        </root>
    </xsl:template>
</xsl:stylesheet>

But it gives an error (Component returned failure code: 0x80600008 [nsIXSLTProcessor.transformToFragment]) (check here)

(EDIT) The XML that I would want is

<root>
    <accession>111</accession>
</root>

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.

(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 here)

Thanks!

Upvotes: 0

Views: 1239

Answers (1)

Zolt&#225;n
Zolt&#225;n

Reputation: 22166

Try this:

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/value">
        <root>
            <xsl:for-each select="num">
                <xsl:variable name="node" select="current()" />
                <xsl:copy-of select="$node/accession" />
           </xsl:for-each>
        </root>
    </xsl:template>
</xsl:transform>

Note that I used xsl:transform instead of xsl:stylesheet. Also, consider using version 2.0 instead of 1.0 if you have a compliant processor, it adds a lot of useful features.

I still don't see your need for a variable, though.

Upvotes: 1

Related Questions