Extracting data of this xml to Python dataframe

I cannot get the extraction of this xml data to dataframe to work properly.
This is my xml sample. In reality I have multiple stacks of “Entity” which represent one line of data in the desired result. To save space I only pasted one instance of “Entity” here:

<EntityCollection>

  <Entities>

    <Entity>

      <Attributes>

        <KeyValuePairOfstringanyType>

          <key>client_number</key>

          <value type="b:string">ABC123345</value>

        </KeyValuePairOfstringanyType>

        <KeyValuePairOfstringanyType>

          <key>my_data.client_type</key>

          <value type="AliasedValue">

            <AttributeLogicalName>my_type</AttributeLogicalName>

            <EntityLogicalName>my_data1</EntityLogicalName>

            <NeedFormatting>true</NeedFormatting>

            <ReturnType>123</ReturnType>

            <Value type="OptionSetValue">

              <Value>345</Value>

            </Value>

          </value>

        </KeyValuePairOfstringanyType>

        <KeyValuePairOfstringanyType>

          <key>my_data.status</key>

          <value type="AliasedValue">

            <AttributeLogicalName>status</AttributeLogicalName>

            <EntityLogicalName>my_data1</EntityLogicalName>

            <NeedFormatting>true</NeedFormatting>

            <ReturnType>123</ReturnType>

            <Value type="OptionSetValue">

              <Value>67</Value>

            </Value>

          </value>

        </KeyValuePairOfstringanyType>

        <KeyValuePairOfstringanyType>

          <key>my_data.date</key>

          <value type="AliasedValue">

            <AttributeLogicalName>date</AttributeLogicalName>

            <EntityLogicalName>my_data1</EntityLogicalName>

            <NeedFormatting>true</NeedFormatting>

            <ReturnType>89</ReturnType>

            <Value type="b:string">2024-01-01</Value>

          </value>

        </KeyValuePairOfstringanyType>

        <KeyValuePairOfstringanyType>

          <key>my_data.country</key>

          <value type="AliasedValue">

            <AttributeLogicalName>country</AttributeLogicalName>

            <EntityLogicalName>my_data1</EntityLogicalName>

            <NeedFormatting>true</NeedFormatting>

            <ReturnType>123</ReturnType>

            <Value type="OptionSetValue">

              <Value>456</Value>

            </Value>

          </value>

        </KeyValuePairOfstringanyType>

        <KeyValuePairOfstringanyType>

          <key>client_code</key>

          <value type="b:guid">some_code456</value>

        </KeyValuePairOfstringanyType>

        <KeyValuePairOfstringanyType>

          <key>my_data.my_data1id</key>

          <value type="AliasedValue">

           <AttributeLogicalName>my_data1id</AttributeLogicalName>

            <EntityLogicalName>my_data1</EntityLogicalName>

            <NeedFormatting>true</NeedFormatting>

            <ReturnType>13</ReturnType>

            <Value type="b:guid">some_code123</Value>

          </value>

        </KeyValuePairOfstringanyType>

      </Attributes>

      <EntityState nil="true"/>

      <FormattedValues>

        <KeyValuePairOfstringstring>

          <key>my_data.client_type</key>

          <value>client123</value>

        </KeyValuePairOfstringstring>

        <KeyValuePairOfstringstring>

          <key>my_data.status</key>

          <value>OK</value>

        </KeyValuePairOfstringstring>

        <KeyValuePairOfstringstring>

          <key>my_data.country</key>

          <value>some_country</value>

        </KeyValuePairOfstringstring>

      </FormattedValues>

      <Id>some_code456</Id>

      <KeyAttributes/>

      <LogicalName>some-id</LogicalName>

      <RelatedEntities/>

      <RowVersion>ID123</RowVersion>

    </Entity>

</Entities>

</EntityCollection>

The elements in ‘key’ (or in ‘AttributeLogicalName’) should be the headers. The “value”/”Value” should be the actual data. This is the desired outcome (first row).

As the data is very nested, I tried using xml.etree but did not come very far as I not sure how to proceed form here:

for _, elem in ET.iterparse(my_xml):
    if len(elem) == 0:
        print(f'{elem.tag} {elem.attrib} text={elem.text}')
    else:
        print(f'{elem.tag} {elem.attrib}')

How can I get the desired result?

1

Consider XSLT, to transform the nested XML for the specific entity, key and value/Value nodes. Then, use pandas to pivot by entity and due to repeated keys, suffixed numbers are added to key names. Pandas read_xml supports XSLT 1.0 using the lxml parser.

XSLT (save as .xsl file)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:template match="/EntityCollection/Entities">
    <root>
        <xsl:apply-templates select="Entity"/>
    </root>
  </xsl:template>
  
  <xsl:template match="Entity">
    <xsl:apply-templates select="descendant::KeyValuePairOfstringanyType|descendant::KeyValuePairOfstringstring"/>
  </xsl:template>

  <xsl:template match="KeyValuePairOfstringanyType|KeyValuePairOfstringstring">
    <data>
        <entity><xsl:value-of select="ancestor::Entity/Id"/></entity>
        <xsl:copy-of select="descendant::key"/>
        <value><xsl:value-of select="descendant::Value[not(*)]|descendant::value[not(*)]"/></value>
    </data>
  </xsl:template>

</xsl:stylesheet>

XSLT Output (long format to scale to any key and value)

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <data>
      <entity>some_code456</entity>
      <key>client_number</key>
      <value>ABC123345</value>
   </data>
   <data>
      <entity>some_code456</entity>
      <key>my_data.client_type</key>
      <value>345</value>
   </data>
   <data>
      <entity>some_code456</entity>
      <key>my_data.status</key>
      <value>67</value>
   </data>
   <data>
      <entity>some_code456</entity>
      <key>my_data.date</key>
      <value>2024-01-01</value>
   </data>
   <data>
      <entity>some_code456</entity>
      <key>my_data.country</key>
      <value>456</value>
   </data>
   <data>
      <entity>some_code456</entity>
      <key>client_code</key>
      <value>some_code456</value>
   </data>
   <data>
      <entity>some_code456</entity>
      <key>my_data.my_data1id</key>
      <value>some_code123</value>
   </data>
   <data>
      <entity>some_code456</entity>
      <key>my_data.client_type</key>
      <value>client123</value>
   </data>
   <data>
      <entity>some_code456</entity>
      <key>my_data.status</key>
      <value>OK</value>
   </data>
   <data>
      <entity>some_code456</entity>
      <key>my_data.country</key>
      <value>some_country</value>
   </data>
</root>

Python (handles duplicate keys with suffixed count)

entities_df = (
    pd.read_xml("input.xml", stylesheet="style.xsl")
      .assign(key = lambda df: df["key"].str.cat(df.groupby("key").cumcount().add(1).astype('str')))
      .pivot(index="entity", columns="key", values="value")
)

Python Output

entities_df
# key           client_code1 client_number1 my_data.client_type1 my_data.client_type2  ... my_data.date1 my_data.my_data1id1 my_data.status1 my_data.status2
# entity                                                                               ...                                                                  
# some_code456  some_code456      ABC123345                  345            client123  ...    2024-01-01        some_code123              67              OK

# [1 rows x 10 columns]

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật