[XML-SIG] Re: Newbie question -> adding a node from an external file

ramiro@labtie.mmt.upc.es ramiro@labtie.mmt.upc.es
Sat, 29 Mar 2003 17:22:48 +0100 (CET)


 Fred writes: 
 > Ramiro Alba Queipo writes:
 >  > I would like to use the addChild method to inserting a node from another
 >  > document but it trigers an error when I try it. Can anyone give me some
 >  > insight about how to perform this?
  > 
 > I'm just guessing that you're using the DOM and mean the appendChild()
 > method.  If not, you'll need to be specific about what youre doing.
 
 > If the nodes are from different documents, it's entirely reasonable to
 > get an exception: the DOM is specified to do so.  Some DOM
 > implementations are more permissive than others, but you really should
 > be expecting an exception if more than one document is involved.
 
 > Since you didn't tell us what exception you got, though, I can't tell
 > if that's what's happening.  You also didn't mention which DOM
 > implementation you're using.
 
 > If you need more help, you're going to need to tell us more about what
 > you're doing and what behaviour you're actually seeing.
 
Sorry about the undefinition Fred, I'll try to be as specific as posible.

I am using 0.8.2 version and the situation is the following:

I have the following document document (current.xml):
------------------------------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-15"?>
<result>
  <instant>Thu, 27 Mar 2003 22:27:11</instant>
  <environment>
    <system>Linux</system>
    <user>ramiro</user>
    <hostname>buzzn</hostname>
    <version>
      <dpc>3.5.6</dpc>
      <cc>2.95.4</cc>
      <libc>ldd (GNU libc) 2.2.5</libc>
      <kernel>2.4.20-k7</kernel>
      <mpi>LAM 6.5.8/MPI 2 C++/ROMIO - Indiana University</mpi>
      <python>2.2.1</python>
    </version>
  </environment>
  <time real="0:0.0" user="0:0.0"/>
  <output>
    <core>
Q-oest:-3.7185867092e+01
Q-est :-3.7185867092e+01
Q-sud : 1.3526543472e+02
Q-nord:-6.0893700536e+01
    </core>
    <lastline>qtotal= 4.2632564146e-14</lastline>
  </output>
</result>
-------------------------------------------------------------------------

I want to insert it in the following one (history.xml) each time I want to
save a new result:

<?xml version="1.0" encoding="UTF-8"?>
<list>
</list>

------------------------------------------------------------------------------
What I have tried is:

#!/usr/bin/python -u
import sys, os, string

from xml.dom.ext.reader import Sax2
from xml import xpath
from xml.dom.ext import Print, PrettyPrint

# create Reader object
reader = Sax2.Reader()

# Current
input = open("current.xml", "r")
result = reader.fromStream(input)
input.close()

input = open("history.xml", "r")
doc = reader.fromStream(input)
input.close()

node = xpath.Evaluate('/result', result.documentElement)
doc.appendChild(node[0])

output = open("kk.xml", "w")
PrettyPrint(doc,output)
output.close()
------------------------------------------------------------------------------
And the exception  is:

Traceback (most recent call last):
  File "./mail.py", line 21, in ?
    doc.appendChild(node[0])
  File "/usr/lib/python2.2/site-packages/_xmlplus/dom/Document.py", line 222, in appendChild
    self._4dom_addSingle(newChild)
  File "/usr/lib/python2.2/site-packages/_xmlplus/dom/Document.py", line 287, in _4dom_addSingle
    self._4dom_validateNode(node)
  File "/usr/lib/python2.2/site-packages/_xmlplus/dom/FtNode.py", line 387, in _4dom_validateNode
    raise WrongDocumentErr()
xml.dom.WrongDocumentErr: Node is from a different document

-------------------------------------------------------------------------------

This agrees with what you are saying above. The question is:

1) How can I manage to doit with python-xml dom?
2) If the answer is I can not, what are the alternatives (xslt?)

Thank you very much for your answer