Home > Mobile >  How to parse XML depending on other parsing condition?
How to parse XML depending on other parsing condition?

Time:03-18

I want to extract data from the XML, only if a particulair field is filled in the XML.

The elements I want to extract are:

  • The city: tag 110 code c (for example Berlin)
  • The library code: tag 110 code g (for example D-Bbbf)
  • The County code: tag 043 code c (for example XA-DE)

This is a part from the XML:

<marc:record>
  <marc:controlfield tag="001">39612</marc:controlfield>
  <marc:controlfield tag="003">DE-633</marc:controlfield>
  <marc:controlfield tag="005">20161109000000.0</marc:controlfield>
  <marc:controlfield tag="008">161109n|||||||a|||              a</marc:controlfield>
  <marc:datafield tag="043" ind1=" " ind2=" ">
    <marc:subfield code="c">XA-DE</marc:subfield>
  </marc:datafield>
  <marc:datafield tag="110" ind1="2" ind2=" ">
    <marc:subfield code="a">Bibliothek für Bildungsgeschichtliche Forschung</marc:subfield>
    <marc:subfield code="c">Berlin</marc:subfield>
  </marc:datafield>
</marc:record><marc:record>
  <marc:controlfield tag="001">30006648</marc:controlfield>
  <marc:controlfield tag="003">DE-633</marc:controlfield>
  <marc:controlfield tag="005">20161109000000.0</marc:controlfield>
  <marc:controlfield tag="008">161109n|||||||a|||              a</marc:controlfield>
  <marc:datafield tag="043" ind1=" " ind2=" ">
    <marc:subfield code="c">XA-GB</marc:subfield>
  </marc:datafield>
  <marc:datafield tag="110" ind1="2" ind2=" ">
    <marc:subfield code="a">The National Archives</marc:subfield>
    <marc:subfield code="c">London</marc:subfield>
    <marc:subfield code="g">GB-Lna</marc:subfield>
  </marc:datafield>
</marc:record>
 

Only if the library code tag is filled, I would like to extract the city and country code too.

This is what I aready coded:

data = []

# Read the XML file
with open('oefen.xml', 'r', encoding="utf8") as f_in:
    soup = BeautifulSoup(f_in.read(), 'html.parser') 
    
for record in soup.find_all(tag="110"):
    data.append({
        'City' : e.get_text(strip=True) if (e := record.select_one('[code="c"]')) else None, # select city
        'Code' : e.get_text(strip=True) if (e := record.select_one('[code="g"]')) else None  # select code
    })

for part in 'Code':
    if part != None:
        for record in soup.find_all(tag="043"):
            data.append({
                'City Code' : e.get_text(strip=True) if (e := record.select_one('[code="c"]')) else None, # select city code
            })

pd.DataFrame(data)

CodePudding user response:

Iterate over the records then find the 110 and 043 elements within each record.

Try something like this:

from bs4 import BeautifulSoup
import pandas as pd

xml = '''<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim">
<marc:record>
  <marc:controlfield tag="001">39612</marc:controlfield>
  <marc:controlfield tag="003">DE-633</marc:controlfield>
  <marc:controlfield tag="005">20161109000000.0</marc:controlfield>
  <marc:controlfield tag="008">161109n|||||||a|||              a</marc:controlfield>
  <marc:datafield tag="043" ind1=" " ind2=" ">
    <marc:subfield code="c">XA-DE</marc:subfield>
  </marc:datafield>
  <marc:datafield tag="110" ind1="2" ind2=" ">
    <marc:subfield code="a">Bibliothek für Bildungsgeschichtliche Forschung</marc:subfield>
    <marc:subfield code="c">Berlin</marc:subfield>
  </marc:datafield>
</marc:record>
<marc:record>
  <marc:controlfield tag="001">30006648</marc:controlfield>
  <marc:controlfield tag="003">DE-633</marc:controlfield>
  <marc:controlfield tag="005">20161109000000.0</marc:controlfield>
  <marc:controlfield tag="008">161109n|||||||a|||              a</marc:controlfield>
  <marc:datafield tag="043" ind1=" " ind2=" ">
    <marc:subfield code="c">XA-GB</marc:subfield>
  </marc:datafield>
  <marc:datafield tag="110" ind1="2" ind2=" ">
    <marc:subfield code="a">The National Archives</marc:subfield>
    <marc:subfield code="c">London</marc:subfield>
    <marc:subfield code="g">GB-Lna</marc:subfield>
  </marc:datafield>
</marc:record>
</marc:collection>
'''
 
data = []

# Read XML from string
soup = BeautifulSoup(xml, 'xml')

# uncomment below to read from XML file
# with open('oefen.xml', 'r', encoding="utf8") as f_in:
#    soup = BeautifulSoup(f_in.read(), 'xml')

for record in soup.find_all('marc:record'):
    rec = {}
    if code := record.find(tag="110"):
        if e := code.select_one('[code="c"]'):
            rec['City'] = e.get_text(strip=True)
        if e := code.select_one('[code="g"]'):
            rec['Code'] = e.get_text(strip=True)

    if code := record.find(tag="043"):
        if e := code.select_one('[code="c"]'):
            rec['City Code'] = e.get_text(strip=True)
    if rec:
        # only add if have at least one field
        data.append(rec)

df = pd.DataFrame(data)
print(df)

Output:

     City City Code    Code
0  Berlin     XA-DE     NaN
1  London     XA-GB  GB-Lna

If don't want NaN to appear for missing values then add the line df = df.fillna('').

CodePudding user response:

Nearly the same issue as in your last question - Just iterate over records and add the new field to your process. Using xml parser you do not have to care about namespaces:

for record in soup.select('record'):

    data.append({
        'City' : e.get_text(strip=True) if (e := record.select_one('datafield[tag="110"] [code="c"]')) else None,
        'Library Code' : e.get_text(strip=True) if (e := record.select_one('datafield[tag="110"] [code="g"]')) else None,
        'Country Code' : e.get_text(strip=True) if (e := record.select_one('datafield[tag="043"] [code="c"]')) else None
    })

Example

xml='''
<root>
<marc:record>
  <marc:controlfield tag="001">39612</marc:controlfield>
  <marc:controlfield tag="003">DE-633</marc:controlfield>
  <marc:controlfield tag="005">20161109000000.0</marc:controlfield>
  <marc:controlfield tag="008">161109n|||||||a|||              a</marc:controlfield>
  <marc:datafield tag="043" ind1=" " ind2=" ">
    <marc:subfield code="c">XA-DE</marc:subfield>
  </marc:datafield>
  <marc:datafield tag="110" ind1="2" ind2=" ">
    <marc:subfield code="a">Bibliothek für Bildungsgeschichtliche Forschung</marc:subfield>
    <marc:subfield code="c">Berlin</marc:subfield>
  </marc:datafield>
</marc:record>
<marc:record>
  <marc:controlfield tag="001">30006648</marc:controlfield>
  <marc:controlfield tag="003">DE-633</marc:controlfield>
  <marc:controlfield tag="005">20161109000000.0</marc:controlfield>
  <marc:controlfield tag="008">161109n|||||||a|||              a</marc:controlfield>
  <marc:datafield tag="043" ind1=" " ind2=" ">
    <marc:subfield code="c">XA-GB</marc:subfield>
  </marc:datafield>
  <marc:datafield tag="110" ind1="2" ind2=" ">
    <marc:subfield code="a">The National Archives</marc:subfield>
    <marc:subfield code="c">London</marc:subfield>
    <marc:subfield code="g">GB-Lna</marc:subfield>
  </marc:datafield>
</marc:record>
</root>
'''

from bs4 import BeautifulSoup
import pandas as pd

data = []

soup = BeautifulSoup(xml,'xml')
for record in soup.select('record'):

    data.append({
        'City' : e.get_text(strip=True) if (e := record.select_one('datafield[tag="110"] [code="c"]')) else None,
        'Library Code' : e.get_text(strip=True) if (e := record.select_one('datafield[tag="110"] [code="g"]')) else None,
        'Country Code' : e.get_text(strip=True) if (e := record.select_one('datafield[tag="043"] [code="c"]')) else None
    })

pd.DataFrame(data)

Output

City Library Code Country Code
Berlin None XA-DE
London GB-Lna XA-GB
  • Related