0

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?

1 Answer 1

1

If you just add the opf prefix to your xpath statement

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

That seems to do the trick

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that did it! Although I don't get why it is necessary to add the prefix to the Xpath if it isen't in the xml node I try to match. Can you explain it or give a link where I can start to search for an explanation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.