<jupyter_start><jupyter_text><jupyter_code>%%capture
!pip install pandas_read_xml
 
# restart runtime after this install to allow Google colab to use the updated packages.
test_xml = """<?xml version="1.0" encoding="UTF-8"?>
<!-- bookstore.xml -->
<bookstore>
  <book ISBN="0123456001">
    <title>Java For Dummies</title>
    <author><NAME></author>
    <category>Programming</category>
    <year>2009</year>
    <edition>7</edition>
    <price>19.99</price>
  </book>
  <book ISBN="0123456002">
    <title>More Java For Dummies</title>
    <author><NAME></author>
    <category>Programming</category>
    <year>2008</year>
    <price>25.99</price>
  </book>
  <book ISBN="0123456010">
    <title>The Complete Guide to Fishing</title>
    <author><NAME></author>
    <author><NAME></author>
    <author><NAME></author>
    <category>Fishing</category>
    <category>Leisure</category>
    <language>French</language>
    <year>2000</year>
    <edition>2</edition>
    <price>49.99</price>
  </book>
</bookstore>"""
import pandas_read_xml as pdx
from pandas_read_xml import flatten, fully_flatten, auto_separate_tables
# See what the reading does
 
df = pdx.read_xml(test_xml, ['bookstore'])
 
df
# See what "flatten" does
 
df = df.pipe(flatten)
 
df
# See what another "flatten" does
 
df = df.pipe(flatten)
 
df
# There are relational data implied here.
# See what auto_separate_tables can do. Specify what the "primary key" would be.
# Note that this "magic" cannot work on all solutions.
 
key_columns = ['book|@ISBN']
 
data = df.pipe(auto_separate_tables, key_columns)
# The resulting "separated" tables are stored as table_name: dataframe pairs.
 
data.keys()
data['author']
data['category']
data['book']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14810562571756603, 'fraction_numerical': 0.0338691159586682, 'mean_word_length': 4.413043478260869, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 1, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '140022', 'license': 'Apache-2.0', 'n_tokens_mistral': 647, 'n_tokens_neox': 593, 'n_words': 145, 'path': 'pandas_read_xml_example.ipynb', 'repo_name': 'jimfhahn/Annif', 'size': '1765'}