I am trying to read an XML file from a command line argument. I am new to using libxml2 and XPath in general. I want to query using XPath.
XML:
<?xml version="1.0"?>
<xmi:XMI xmlns:cas="http:///text/cas.ecore" xmlns:audioform="http:something" xmlns:xmi="http://blahblah" xmlns:lib="http://blahblah" xmlns:solr="http:blahblah" xmlns:tcas="http:///blah" xmi:version="2.0">
<cas:NULL xmi:id="0"/>
<cas:Sofa xmi:id="9" Num="1" ID="First" Type="text" String="play a song"/>
<cas:Sofa xmi:id="63" Num="2" ID="Second" Type="text" String="Find a contact"/>
<cas:Sofa xmi:id="72" Num="3" ID="Third" Type="text" String="Send a message"/>
<lib:Confidence xmi:id="1" sofa="9" begin="0" end="1" key="context" value="" confidence="1.0"/>
</xmi:XMI>
Code:
def main(argv):
try:
xmlfile=argv[0]
doc=libxml2.parseFile(xmlfile)
root2=doc.children
print root2 # This prints everything but <?xml version="1.0"?>
result= root2.xpathEval("//*")
for node in result:
print node
print node.nodePath(), node.name, node.content
I want to go further and do some kind of processing using this file.
- How do I get values like 63 using xpath ? from
xmi:id="63". - Find String where
xmi:id = "72". Result should be "Send a message" - Find string where
xmi:id = 72 and ID= "Third". Result should be "Send a message" I tried using
node.Path(),node.nameandnode.contentfor this node:<cas:Sofa xmi:id="9" Num="1" ID="First" Type="text" String="play a song"/>The results are:
/xmi:XMI/cas:Sofa[1]asnodePath(), Sofa as name and prints no content
How do I go about getting 1 and 2 and 3?