Search code examples
pythonxmlxml-parsingorcid

Extract data from ORCID XML files using Python


I ma trying to (offline) parse names from ORCID XML files using Python, which is downloaded from :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<record:record xmlns:internal="http://www.orcid.org/ns/internal" xmlns:address="http://www.orcid.org/ns/address" xmlns:email="http://www.orcid.org/ns/email" xmlns:history="http://www.orcid.org/ns/history" xmlns:employment="http://www.orcid.org/ns/employment" xmlns:person="http://www.orcid.org/ns/person" xmlns:education="http://www.orcid.org/ns/education" xmlns:other-name="http://www.orcid.org/ns/other-name" xmlns:personal-details="http://www.orcid.org/ns/personal-details" xmlns:bulk="http://www.orcid.org/ns/bulk" xmlns:common="http://www.orcid.org/ns/common" xmlns:record="http://www.orcid.org/ns/record" xmlns:keyword="http://www.orcid.org/ns/keyword" xmlns:activities="http://www.orcid.org/ns/activities" xmlns:deprecated="http://www.orcid.org/ns/deprecated" xmlns:external-identifier="http://www.orcid.org/ns/external-identifier" xmlns:funding="http://www.orcid.org/ns/funding" xmlns:error="http://www.orcid.org/ns/error" xmlns:preferences="http://www.orcid.org/ns/preferences" xmlns:work="http://www.orcid.org/ns/work" xmlns:researcher-url="http://www.orcid.org/ns/researcher-url" xmlns:peer-review="http://www.orcid.org/ns/peer-review" path="/0000-0001-5006-8001">
    <common:orcid-identifier>
        <common:uri>http://orcid.org/0000-0001-5006-8001</common:uri>
        <common:path>0000-0001-5006-8001</common:path>
        <common:host>orcid.org</common:host>
    </common:orcid-identifier>
    <preferences:preferences>
        <preferences:locale>en</preferences:locale>
    </preferences:preferences>

     <person:person path="/0000-0001-5006-8001/person">
    <common:last-modified-date>2016-06-06T15:29:36.952Z</common:last-modified-date>
    <person:name visibility="public" path="0000-0001-5006-8001">
        <common:created-date>2016-04-15T20:45:16.141Z</common:created-date>
        <common:last-modified-date>2016-04-15T20:45:16.141Z</common:last-modified-date>
        <personal-details:given-names>Marjorie</personal-details:given-names>
        <personal-details:family-name>Biffi</personal-details:family-name>
    </person:name>

What I want is to extract given-names and family-name: Marjorie Biffi. I am trying to use this code:

>>> import xml.etree.ElementTree as ET
>>> root = ET.parse('f.xml').getroot()
>>> p=root.findall('{http://www.orcid.org/ns/personal-details}personal-details')
>>> p
[]

I can't figure out how to extract names/surname from this XML file. I am trying also yo use XPath/Selector, but no succes.


Solution

  • This will get you the results you want, but by climbing through each one.

    p1 = root.find('{http://www.orcid.org/ns/person}person')
    name = p1.find('{http://www.orcid.org/ns/person}name')
    given_names =  name.find('{http://www.orcid.org/ns/personal-details}given-names')
    family_name = name.find('{http://www.orcid.org/ns/personal-details}family-name')
    print(given_names.text, '', family_name.text)
    

    You could also just go directly to that sublevel with .\\

    family_name = root.find('.//{http://www.orcid.org/ns/personal-details}family-name')
    

    Also I just posted here about simpler ways to parse through xml if you're doing more basic operations. These include xmltodict (converting to an OrderedDict) or untangle which is a little inefficient but very quick and easy to learn.