Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://lxml.de/" rel="nofollow noreferrer">lxml</a> is namespace-aware.</p> <pre><code>&gt;&gt;&gt; from lxml import etree &gt;&gt;&gt; et = etree.XML("""&lt;root xmlns="foo" xmlns:stuff="bar"&gt;&lt;bar&gt;&lt;stuff:baz /&gt;&lt;/bar&gt;&lt;/root&gt;""") &gt;&gt;&gt; etree.tostring(et, encoding=str) # encoding=str only needed in Python 3, to avoid getting bytes '&lt;root xmlns="foo" xmlns:stuff="bar"&gt;&lt;bar&gt;&lt;stuff:baz/&gt;&lt;/bar&gt;&lt;/root&gt;' &gt;&gt;&gt; et.xpath("f:bar", namespaces={"b":"bar", "f": "foo"}) [&lt;Element {foo}bar at ...&gt;] </code></pre> <p>Edit: On your example:</p> <pre><code>from lxml import etree # remove the b prefix in Python 2 # needed in python 3 because # "Unicode strings with encoding declaration are not supported." et = etree.XML(b"""...""") ns = { 'lom': 'http://ltsc.ieee.org/xsd/LOM', 'zs': 'http://www.loc.gov/zing/srw/', 'dc': 'http://purl.org/dc/elements/1.1/', 'voc': 'http://www.schooletc.co.uk/vocabularies/', 'srw_dc': 'info:srw/schema/1/dc-schema' } # according to docs, .xpath returns always lists when querying for elements # .find returns one element, but only supports a subset of XPath record = et.xpath("zs:records/zs:record", namespaces=ns)[0] # in this example, we know there's only one record # but else, you should apply the following to all elements the above returns name = record.xpath("//voc:name", namespaces=ns)[0].text print("name:", name) lom_entry = record.xpath("zs:recordData/srw_dc:dc/" "lom:metaMetadata/lom:identifier/" "lom:entry", namespaces=ns)[0].text print('lom_entry:', lom_entry) lom_ids = [id.text for id in record.xpath("zs:recordData/srw_dc:dc/" "lom:classification/lom:taxonPath/" "lom:taxon/lom:id", namespaces=ns)] print("lom_ids:", lom_ids) </code></pre> <p>Output:</p> <pre><code>name: Frank Malina lom_entry: 2.6 lom_ids: ['PYTHON', 'XML', 'XML-NAMESPACES'] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload