[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"?>
  <instant>Thu, 27 Mar 2003 22:27:11</instant>
      <libc>ldd (GNU libc) 2.2.5</libc>
      <mpi>LAM 6.5.8/MPI 2 C++/ROMIO - Indiana University</mpi>
  <time real="0:0.0" user="0:0.0"/>
Q-est :-3.7185867092e+01
Q-sud : 1.3526543472e+02
    <lastline>qtotal= 4.2632564146e-14</lastline>

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"?>

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 = open("history.xml", "r")
doc = reader.fromStream(input)

node = xpath.Evaluate('/result', result.documentElement)

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

Traceback (most recent call last):
  File "./mail.py", line 21, in ?
  File "/usr/lib/python2.2/site-packages/_xmlplus/dom/Document.py", line 222, in appendChild
  File "/usr/lib/python2.2/site-packages/_xmlplus/dom/Document.py", line 287, in _4dom_addSingle
  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