Reputation: 3
I am trying to select an xml node from an xml file using namespaces. I already got one selection working, but can't get it to work for the second one.
This is the simplified xml (stored as BookMetaData in the python code):
<?xml version='1.0' encoding='utf-8'?>
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="calibre_id">
<metadata xmlns:opf="http://www.idpf.org/2007/opf"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata">
<dc:title>De blanke masai V2</dc:title>
<meta name="calibre:user_metadata:#origfieldvalue" content="{"is_category": true, "#extra#": null}"/>
</metadata>
</package>
This is the python code which I've written so far:
#!/usr/bin/python
# All imports
import lxml.html
import lxml.etree
# namespaces
theNamespaces = {'opf' : "http://www.idpf.org/2007/opf",
'dc' : "http://purl.org/dc/elements/1.1/",
'calibre' : "http://calibre.kovidgoyal.net/2009/metadata",
'unique-identifier' : "calibre_id" }
# This part is working perfectly
theXMLdoc = lxml.etree.fromstring(BookMetaData)
theElement2 = theXMLdoc.xpath("//dc:title", namespaces = theNamespaces)[0]
print "lxml.html Source Value:"
print( theElement2.text)
print ""
# This part only returns an emtpy list
theOrigValueElement = theXMLdoc.xpath("//meta[@name='calibre:user_metadata:#origfieldvalue']", namespaces = theNamespaces)
print "Original value of OrigFieldValue:"
print( theOrigValueElement)
print ""
Things I've tried which are not working:
how-to-use-xpath-from-lxml-on-null-namespaced-nodes
The namspace "http://www.idpf.org/2007/opf" is used twice, once in "package" without a prefix and once in "metadata" with a prefix. So adding another prefix to the namespace will not help.
Can someone help me with this?
Upvotes: 0
Views: 629
Reputation: 1054
If you just add the opf prefix to your xpath statement
//opf:meta[@name='calibre:user_metadata:#origfieldvalue']
That seems to do the trick
Upvotes: 1