Amara&4Suite in XML
Version 1.1.7 (2005-12-13) ftp://ftp.4suite.org/pub/cvs-snapshots/Amara-CVS.tar.bz2 * Deprecate xml_text_content property * Add xml_child_text property that concatenates all immediate child text nodes (no recursive descent) * Change unicode coercion for documents and elements to recurse through all descendant text (now analogous to XPath's string() coercion) * Update allinone bundle to 4Suite 1.0b3 * Packaging fixes Amara 1.1.7 requires Python 2.4 or more recent. If you do not have 4Suite XML 1.0b2 or better, grab the Amara-allinone package. If you already have 4Suite XML installed, grab the stand along Amara package.
為了要更瞭解xml@python,以及XPath, XQuery,所以才找到最好的解決方案:
作出了下列的Xml類別,但是會出現一些的問題
from optparse import OptionParser
from sys import argv
from os.path import isdir, splitext
from os import system, getuid
from amara import parse
from amara import domtools
class XmlImpl:
def __init__(self, XmlFile):
self.XmlFile=XmlFile
self.XmlDoc=parse(self.XmlFile)
def Node(self):
self.Nodes=[]
for node in self.XmlDoc.xml_xpath(u'//*'):
XmlNode=node.nodeName
if not self.Nodes.count(XmlNode):self.Nodes.append(XmlNode)
#self.Nodes.sort()
print 'The nodes in %s are:\n%s'%(self.XmlFile,self.Nodes)
def RootNode(self):
for RootNode in self.XmlDoc.xml_xpath('*'):self.RootNode=RootNode.nodeName
print 'The RootNode in %s is: %s'%(self.XmlFile,self.RootNode)
從XmlImpl中,利用amara所提供的xml_xpath,可以得到RootNode以及所有的NodeName,但是因為amara要求必需要用 doc.ClinicalDocument.legalAuthenticator.xml()的方式,才能得到xml segments的內容,所以不可能直接寫在此XmlImpl類別之中,另類解法是在XmlImpl中,另訂方法,將找到的NodeName,轉而輸出成文件檔,然後再用exec的方式,讀入並且在原程式內執行,不過這樣的方式有點難懂,且有點ugly。