简体   繁体   中英

Xpath issue using lxml library in an xml file with namespaces

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="{&quot;is_category&quot;: true, &quot;#extra#&quot;: 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?

If you just add the opf prefix to your xpath statement

//opf:meta[@name='calibre:user_metadata:#origfieldvalue']

That seems to do the trick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM