<!-- I would like to create a variable which contains a node from external xml -->
<xsl:variable name="tags">
<!-- change scope
for-each do one iteration-->
<xsl:for-each select="$tagsDoc">
<!-- in debug mode it's ok, key returns one node -->
<xsl:value-of select="key('tags','some_name')"/>
</xsl:for-each>
<!-- change scope
key() doesn't return any, because scope is changed
-->
</xsl:variable>
<!--variable is not initialized -->
</xsl:template>
Why cannot I define variable by this way? And does some way exist to define variable with node from external document by key()?
Subject:xsl:key, scope and creating of variable Author:Siarhei Barysiuk Date:01 Nov 2006 08:42 AM
Hello Ivan,
For first thanks for reply.
I create small example:
- processor.xsl
- main.xml
- tags.xml
In example creating of variable will occur one time but in my project I need read extenal file many times and I decide that use of key() increases performance (I think it will be faster than direct select particular node through select="document('some_name')/tags/tag[.....]/@args" ).
Subject:xsl:key, scope and creating of variable Author:James Durning Date:01 Nov 2006 11:23 AM
The answer to your problem depends exactly on what you want to do.
If you want to loop through each of the tags in the external file, I suggest using <xsl:apply-templates select="document('tags.xml')"/>
--
If you only want to access the external file once, it may be better to use the variable like you have here: <xsl:variable name="$tagsDoc" select="document('external.xml')"/>
and use an node-set function when you want to access it later.
eg. with msxsl:
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
<xsl:value-of select="msxsl:node-set($tagsDoc)/tags/tag[@name='somevalue']"/>
or with exslt:
xmlns:exslt="http://exslt.org/common"
<xsl:value-of select="exslt:node-set($tagsDoc)/tags/tag[@name='somevalue']"/>
Using a node-set function is not necessary if you use XSLT 2.0.
Subject:xsl:key, scope and creating of variable Author:Siarhei Barysiuk Date:01 Nov 2006 11:39 AM
Hi,
Yes, if I would access to external file once or few times. With $tagsDoc variable and select from it all work perfect. But it's slow (I think slower than it would be with key). I would like to increase speed of processing.
Subject:xsl:key, scope and creating of variable Author:Siarhei Barysiuk Date:04 Nov 2006 05:29 AM
Hello,
I've resolve this problem by oneself. The main problem is xsl:value-of.
xsl:value-of return STRING but not NODE SET. In this example need to use xsl:copy-of which retun NODE SET.