  | |  Re: Unable to see source document after using for-each on a converted
node-se | Re: Unable to see source document after using for-each on a converted
node-se 2003-02-07 - By David N Bertoni/Cambridge/IBM
Hi Pete,
I believe Xalan-J is correct. I tried your stylesheet with Saxon and Xalan-C and got the same result as you get with Xalan-J.
When you use exslt:node-set to turn a result tree fragment into a node-set, you're essentially converting it into another document. As a result, within the xsl:for-each instruction, the nodes you're iterating over are in that document, so "/" refers to the converted node-set.
The usual trick is to save the original root in a variable, so you have access to it within the xsl:for-each instruction:
<xsl:template match="root"> <out> <!-- process the specific nodes defined in the variable --> <xsl:variable name="source" select="/"/> <xsl:for-each select="exslt:node-set($tree)/test"> <xsl:variable name="tag" select="."/> <results group="{$tag}"> <xsl:for-each select="$source/root/*[name() = $tag]"> <xsl:copy-of select="."/> </xsl:for-each> </results> </xsl:for-each> </out> </xsl:template>
I suspect libxslt is using the source document as a factory for result tree fragments, which would give the result you expect without saving the context. However, this is not correct:
http://www.w3.org/TR/xslt#section-Result-Tree-Fragments
"A result tree fragment is treated equivalently to a node-set that contains just a single root node."
Hope that helps...
Dave
"Edwards, Peter" <Peter.Edwards@(protected) To: xalan-j-users@(protected) .apache.org zeneca.com> cc: (bcc: David N Bertoni/Cambridge/IBM) Subject: Unable to see source document after using for-each on a converted node-set 02/07/2003 03:02 AM
I have a simple document: <?xml version="1.0" encoding="UTF-8"?> <root> <name>Test</name> <description>D1</description> <number>42</number> <description>D2</description> </root>
And a stylesheet that groups certain elements together: <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" extension-element-prefixes="exslt" exclude-result-prefixes="exslt" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:variable name="tree"> <test>name</test> <test>description</test> </xsl:variable> <xsl:template match="root"> <out> <!-- process the specific nodes defined in the variable --> <xsl:for-each select="exslt:node-set($tree)/test"> <xsl:variable name="tag" select="."/> <results group="{$tag}"> <xsl:for-each select="/root/*[name() = $tag]"> <xsl:copy-of select="."/> </xsl:for-each> </results> </xsl:for-each> </out> </xsl:template> </xsl:stylesheet>
The expected output is: <?xml version="1.0"?> <out> <results group="name"> <name>Test</name> </results> <results group="description"> <description>D1</description> <description>D2</description> </results> </out>
In the case of Xalan (v2.4.1 with JDK1.4.1) the output is: <?xml version="1.0" encoding="UTF-8"?> <out> <results group="name"/> <results group="description"/> </out>
I ran the same stylesheet with XSLTProc and got the output I expected. It is as though the Xalan processor cannot see the root element when it is operating within a for-each clause walking a converted nodeset. Does anyone know if this is a know fault or is it behaving according to spec and my expectations were wrong. Obviously there are better ways of grouping things and I have changed the whole stylesheet but suspect I have found a problem.
Pete Edwards
|
|
 |