You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Peter Nabbefeld <pe...@gmx.de> on 2008/04/17 15:48:00 UTC

Problem copying a result tree fragment

Hello!

I'm trying to convert a result tree fragment into a nodeset using
xsl:copy-of. From the w3c spec, if I understand it correct, this should be
possible:

"When a result tree fragment is copied into the result tree (see [11.3 Using
Values of Variables and Parameters with xsl:copy-of]), then all the nodes
that are children of the root node in the equivalent node-set are added in
sequence to the result tree."

I'm using the following piece of code: <xsl:variable
name="node"><node><xsl:copy-of select="$fragment"/></node></xsl:variable>

The following error occurs in this line: "#RTREEFRAG kann nicht in NodeList
konvertiert werden!"

Where's the problem?

Kind regards

Peter Nabbefeld




Re: Problem copying a result tree fragment

Posted by ke...@us.ibm.com.
Classic point of confusion. In XSLT 1.0, a Result Tree Fragment is not a 
Nodeset, so xsl:copy is the wrong operation to perform.

Just reference the variable's value to output the fragment, rather than 
trying to perform an xsl:copy from it.

If you need to actually process it as a nodeset (for example, if you need 
to navigate into it as a tree of nodes), you'll need to use the exslt 
node-set function to convert it first.


XSLT 2.0 removes the distinction between RTFs and Nodesets... but alas, 
Xalan does not support 2.0.


______________________________________
"... Three things see no end: A loop with exit code done wrong,
A semaphore untested, And the change that comes along. ..."
  -- "Threes" Rev 1.1 - Duane Elms / Leslie Fish (
http://www.ovff.org/pegasus/songs/threes-rev-11.html)

Re: Problem copying a result tree fragment

Posted by Mukul Gandhi <ga...@gmail.com>.
Thanks, Henry for sharing this point. It clears my doubts ...

On 4/18/08, Henry Zongaro <zo...@ca.ibm.com> wrote:
> That's exactly the reason for using something like <xsl:for-each
> select="$external"> as in Peter's example - even though it iterates over
> exactly one node, it's the only convenient way of setting the context node
> to be the root of another document so that the key() function or id()
> function will select nodes in that document.


-- 
Regards,
Mukul Gandhi

Re: Problem copying a result tree fragment

Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, Mukul.

"Mukul Gandhi" <ga...@gmail.com> wrote on 2008-04-18 04:16:12 AM:
> So you have this code ...
> 
> <xsl:variable name="external" select="document('ext.xml')"/>
> <xsl:variable name="fragment">
>   <xsl:for-each select="$external">
>     <xsl:copy-of select="key(...)"/>
>   </xsl:for-each>
> </xsl:variable>
> 
> Here, <xsl:for-each select="$external"> will not be much useful, as it
> represents the root node of the external document (ext.xml). This
> actually selects 1 node (which is the root node).
> 
> Something like this could be useful:
> <xsl:for-each select="$external/a/b/c">
> 
> Also, they key() function at this position will operate on the tree
> formed by ext.xml and not on the primary document. Is this what you
> need?

That's exactly the reason for using something like <xsl:for-each 
select="$external"> as in Peter's example - even though it iterates over 
exactly one node, it's the only convenient way of setting the context node 
to be the root of another document so that the key() function or id() 
function will select nodes in that document.

Thanks,

Henry
------------------------------------------------------------------
Henry Zongaro
XML Transformation & Query Development
IBM Toronto Lab   T/L 313-6044;  Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com


Re: Problem copying a result tree fragment

Posted by Mukul Gandhi <ga...@gmail.com>.
Thanks for the clarification.

So you have this code ...

<xsl:variable name="external" select="document('ext.xml')"/>
<xsl:variable name="fragment">
  <xsl:for-each select="$external">
    <xsl:copy-of select="key(...)"/>
  </xsl:for-each>
</xsl:variable>

Here, <xsl:for-each select="$external"> will not be much useful, as it
represents the root node of the external document (ext.xml). This
actually selects 1 node (which is the root node).

Something like this could be useful:
<xsl:for-each select="$external/a/b/c">

Also, they key() function at this position will operate on the tree
formed by ext.xml and not on the primary document. Is this what you
need?

Without seeing more of the code, it's difficult to say what could be wrong.

Also if you could tell what you are trying to accomplish, we could
probably suggest better ways to solve the problem.

On 4/18/08, Peter Nabbefeld <pe...@gmx.de> wrote:
> Sorry, I should have written
> ...
> <xsl:for-each select="$external">


-- 
Regards,
Mukul Gandhi

Re: Problem copying a result tree fragment

Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, Peter.

news <ne...@ger.gmane.org> wrote on 2008-04-17 09:48:00 AM:
> I'm trying to convert a result tree fragment into a nodeset using
> xsl:copy-of. From the w3c spec, if I understand it correct, this should 
be
> possible:
> 
> "When a result tree fragment is copied into the result tree (see [11.3 
Using
> Values of Variables and Parameters with xsl:copy-of]), then all the 
nodes
> that are children of the root node in the equivalent node-set are added 
in
> sequence to the result tree."
> 
> I'm using the following piece of code: <xsl:variable
> name="node"><node><xsl:copy-of 
select="$fragment"/></node></xsl:variable>
> 
> The following error occurs in this line: "#RTREEFRAG kann nicht in 
NodeList
> konvertiert werden!"

As Joe was pointing out, there are restrictions on the operations that may 
be performed on a result tree fragment (nodes constructed by the 
stylesheet, rather than read from the input document).  Section 11.1 of 
XSLT 1.0 [1] has this to say, "An operation is permitted on a result tree 
fragment only if that operation would be permitted on a string (the 
operation on the string may involve first converting the string to a 
number or boolean). In particular, it is not permitted to use the /, //, 
and [] operators on result tree fragments."  However, it goes on to say, 
"When a result tree fragment is copied into the result tree (see [11.3 
Using Values of Variables and Parameters with xsl:copy-of]), then all the 
nodes that are children of the root node in the equivalent node-set are 
added in sequence to the result tree," so the xsl:copy-of operation should 
not be a problem (unless there's a bug in the processor, but I've not been 
able to reproduce that.

So, the first question must be, are you attempting to use / or // or [] 
operators on a result tree fragment?

and later...

news <ne...@ger.gmane.org> wrote on 2008-04-17 05:02:33 PM:
> Hm, might it have to do with building the fragment using a key on an 
> external document?
> 
> I've been using sth like this:
> <xsl:variable name="external" select="document('ext.xml')"/>
> <xsl:variable name="fragment">
>    <xsl:for-each select="$fragment">
>      <xsl:copy-of select="key(...)"/>
>    </xsl:for-each>
> </xsl:variable>

Probably not.  Doing an xsl:copy-of on a result tree fragment is always 
permitted - it doesn't matter how it was constructed.

As I said, the usual reason for this error is that you are attempting to 
apply a path expression (/, // or [] operators) to a result tree fragment. 
 If that's not the case, please produce a complete, stand-alone stylesheet 
with input that demonstrates the problem - otherwise, we're all just 
making guesses at what might be going wrong.

Thanks,

Henry
[1] http://www.w3.org/TR/xslt#section-Result-Tree-Fragments
------------------------------------------------------------------
Henry Zongaro
XML Transformation & Query Development
IBM Toronto Lab   T/L 313-6044;  Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com


Re: Problem copying a result tree fragment

Posted by Peter Nabbefeld <pe...@gmx.de>.
Mukul Gandhi <gandhi.mukul <at> gmail.com> writes:

> 
> This looks a bit odd ...
> 
> You have:
> 
> <xsl:variable name="fragment">
>    <xsl:for-each select="$fragment">
>       ...
>    </xsl:for-each>
> </xsl:variable>
> 
> You have a variable 'fragment'. You assign some contents to it (using
> xsl:for-each). Since the variable still isn't assigned a value, how
> can you do <xsl:for-each select="$fragment"> at this position?
> 
> This would have been right:
> 
> <xsl:variable name="fragment">
>    <xsl:copy-of select="key(...)"/>
> </xsl:variable>
> 
> 
Sorry, I should have written 
...
<xsl:for-each select="$external">
...
(didn't paste)

Regards
Peter



Re: Problem copying a result tree fragment

Posted by Mukul Gandhi <ga...@gmail.com>.
On 4/18/08, Peter Nabbefeld <Pe...@gmx.de> wrote:
> I've been using sth like this:
> <xsl:variable name="external" select="document('ext.xml')"/>
> <xsl:variable name="fragment">
>  <xsl:for-each select="$fragment">
>    <xsl:copy-of select="key(...)"/>
>  </xsl:for-each>
> </xsl:variable>

This looks a bit odd ...

You have:

<xsl:variable name="fragment">
   <xsl:for-each select="$fragment">
      ...
   </xsl:for-each>
</xsl:variable>

You have a variable 'fragment'. You assign some contents to it (using
xsl:for-each). Since the variable still isn't assigned a value, how
can you do <xsl:for-each select="$fragment"> at this position?

This would have been right:

<xsl:variable name="fragment">
   <xsl:copy-of select="key(...)"/>
</xsl:variable>

But I don't know if this is what you want.

Re: Problem copying a result tree fragment

Posted by Peter Nabbefeld <Pe...@gmx.de>.
Hm, might it have to do with building the fragment using a key on an 
external document?

I've been using sth like this:
<xsl:variable name="external" select="document('ext.xml')"/>
<xsl:variable name="fragment">
   <xsl:for-each select="$fragment">
     <xsl:copy-of select="key(...)"/>
   </xsl:for-each>
</xsl:variable>

xalan is 2.7.1 (downloaded today)

Kind regards

P.N.


Mukul Gandhi wrote:
> When I tried this sample with Xalan-J 2.7.1:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>                        version="1.0">
> 
>  <xsl:output method="xml" indent="yes" />
> 
>  <xsl:template match="/">
>    <xsl:variable name="fragment">
>      <x>
>        <y/>
>      </x>
>    </xsl:variable>
> 
>    <xsl:variable name="node">
>      <node>
>        <xsl:copy-of select="$fragment" />
>      </node>
>    </xsl:variable>
> 
>    <xsl:copy-of select="$node" />
>  </xsl:template>
> 
> </xsl:stylesheet>
> 
> I get the output:
> <?xml version="1.0" encoding="UTF-8"?>
> <node>
>   <x>
>     <y/>
>   </x>
> </node>
> 
> So my finding does'nt match with your report ...
> 
> Could you please let us know, which version of Xalan you are using.
> 
> On Thu, Apr 17, 2008 at 7:18 PM, Peter Nabbefeld <pe...@gmx.de> wrote:
>> Hello!
>>
>>  I'm trying to convert a result tree fragment into a nodeset using
>>  xsl:copy-of. From the w3c spec, if I understand it correct, this should be
>>  possible:
>>
>>  "When a result tree fragment is copied into the result tree (see [11.3 Using
>>  Values of Variables and Parameters with xsl:copy-of]), then all the nodes
>>  that are children of the root node in the equivalent node-set are added in
>>  sequence to the result tree."
>>
>>  I'm using the following piece of code: <xsl:variable
>>  name="node"><node><xsl:copy-of select="$fragment"/></node></xsl:variable>
>>
>>  The following error occurs in this line: "#RTREEFRAG kann nicht in NodeList
>>  konvertiert werden!"
>>
>>  Where's the problem?
>>
>>  Kind regards
>>
>>  Peter Nabbefeld
> 
> 


Re: Problem copying a result tree fragment

Posted by Mukul Gandhi <ga...@gmail.com>.
When I tried this sample with Xalan-J 2.7.1:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                       version="1.0">

 <xsl:output method="xml" indent="yes" />

 <xsl:template match="/">
   <xsl:variable name="fragment">
     <x>
       <y/>
     </x>
   </xsl:variable>

   <xsl:variable name="node">
     <node>
       <xsl:copy-of select="$fragment" />
     </node>
   </xsl:variable>

   <xsl:copy-of select="$node" />
 </xsl:template>

</xsl:stylesheet>

I get the output:
<?xml version="1.0" encoding="UTF-8"?>
<node>
  <x>
    <y/>
  </x>
</node>

So my finding does'nt match with your report ...

Could you please let us know, which version of Xalan you are using.

On Thu, Apr 17, 2008 at 7:18 PM, Peter Nabbefeld <pe...@gmx.de> wrote:
> Hello!
>
>  I'm trying to convert a result tree fragment into a nodeset using
>  xsl:copy-of. From the w3c spec, if I understand it correct, this should be
>  possible:
>
>  "When a result tree fragment is copied into the result tree (see [11.3 Using
>  Values of Variables and Parameters with xsl:copy-of]), then all the nodes
>  that are children of the root node in the equivalent node-set are added in
>  sequence to the result tree."
>
>  I'm using the following piece of code: <xsl:variable
>  name="node"><node><xsl:copy-of select="$fragment"/></node></xsl:variable>
>
>  The following error occurs in this line: "#RTREEFRAG kann nicht in NodeList
>  konvertiert werden!"
>
>  Where's the problem?
>
>  Kind regards
>
>  Peter Nabbefeld


-- 
Regards,
Mukul Gandhi