Re: [xsl] create xml from incoming xml and load it into a variable

Subject: Re: [xsl] create xml from incoming xml and load it into a variable
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 25 Jul 2008 17:00:34 -0400
Sudeshna,

At 04:37 PM 7/25/2008, you wrote:
...
Inside the loop, I need to use <xsl:key name="ccc" match="subroot" use="ccc"/>.
But while transforming, I am receiving the following error:


xsl:key is not allowed in this position in the stylesheet!

How do I use "key" tag inside a variable which contains xml?

Keys are declared at the top level, not inside templates or variable declarations. Once a key is declared at the top level (with the xsl:key element), it can be used anywhere an XPath expression is allowed (with the key() function), including inside a variable declaration.


This does not mean, however, that you can't do what you need to do. You just can't do it in the way you are thinking. You say "Inside the loop, I need to use xsl:key..." but this isn't right. Or actually, in the absence of more information, it's meaningless. It's like saying "I need to use a toothbrush" without telling us why. Maybe you think you need to use a toothbrush, but a hair brush is what you really need.

If you explain what you are trying to achieve, preferably with a reduced sample of your input and your desired output, we can explain more. If a solution exists at all (and it undoubtedly does), it will necessarily be one that does not require an illegal construction.

<!--Traverse the variable-->
  <xsl:for-each select="xalan:nodeset($var)/root/subroot">
    <xsl:key name="ccc" match="subroot" use="11"/> ===> ERROR!!!!
  </xsl:for-each>
</xsl:template>

In this case, it appears your concern is declaring a key that will be available for use inside the traversal of your $var variable as a node set. If so, don't worry about it. Declare the key at the top level and use it normally inside the template. The rule (in XSLT 1.0) is that a key retrieves nodes from the document containing the context node. So when your context is inside $var, the key will retrieve nodes from inside $var. (The declaration only says what to retrieve, it does not define a scope for the retrieval.)


If you need to retrieve nodes from another document (maybe your original source document) when inside $var, this can be done too, generally by switching the context back to the other source (in XSLT 1.0, with another xsl:apply-templates or xsl:for-each). But I don't know if this is the case here.

Cheers,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread