XML-Parsing error AttributeError: 'NoneType' object has no attribute 'text'

Viewed 590

There is probably a simple solution to my problem, but I am very new to python3 so please go easy on me;)

I have a simple script running, which already successfully parses information from an xml-file using this code

import xml.etree.ElementTree as ET

root = ET.fromstring(my_xml_file)

u = root.find(".//name").text.rstrip()
print("Name: %s\n" % u)

The xml I am parsing looks like this

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.2/style/exchange.xsl"?>
<example:world-data xmlns="http://www.example.org" xmlns:ops="http://example.oorg" xmlns:xlink="http://www.w3.oorg/1999/xlink">
  <exchange-documents>
    <exchange-document system="acb.org" family-id="543672" country="US" doc-number="95962" kind="B2">
      <bibliographic-data>
             <name>SomeName</name>
       ...and so on... and ends like this
   </exchange-document>
  </exchange-documents>
</example:world-data>

(Links are edited due to stackoverflow policy)

Output as expected

SomeName

However, if I try to parse another xml from the same api using the same python commands, I get this error-code

AttributeError: 'NoneType' object has no attribute 'text'

The second xml-file looks like this

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.2/style/pub-ftxt-claims.xsl"?>
<ops:world-data xmlns="http://www.example.org/exchange" xmlns:example="http://example.org" xmlns:xlink="http://www.example.org/1999/xlink">
  <ftxt:fulltext-documents xmlns="http://www.examp.org/fulltext" xmlns:ftxt="ww.example/fulltext">
    <ftxt:fulltext-document system="example.org" fulltext-format="text-only">
      <bibliographic-data>
        <publication-reference data-format="docdb">
          <document-id>
            <country>EP</country>
            <doc-number>10000</doc-number>
            <kind>A</kind>
          </document-id>
        </publication-reference>
      </bibliographic-data>
      <claims lang="EN">
        <claim>
          <claim-text>1. Some text.</claim-text>
          <claim-text>2. Some text.</claim-text>
          <claim-text>2. Some text.</claim-text>
        </claim>
      </claims>
    </ftxt:fulltext-document>
  </ftxt:fulltext-documents>
</ops:world-data>

I tried again

root = ET.fromstring(usr_str)

u = root.find(".//claim-text").text.rstrip()
print("Abstract: %s\n" % u)

Expected output

1. Some text.

But it only prints the above mentioned error message. Why can I parse the first xml but not the second one using these commands?

Any help is highly appreciated.

edit: code by Jack Fleeting works in python console, but unfortunately not in my PyCharm

from lxml import etree
root = etree.XML(my_xml.encode('ascii'))
root2 = etree.XML(my_xml2.encode('ascii'))

root.xpath('//*[local-name()="name"]/text()')
root2.xpath('//*[local-name()="claim-text"]/text()')

Could this be a bug in my PyCharm? My first mentioned code snippet still prints a correct result for name...

edit: Turns out I had to force the output using

a = root3.xpath('//*[local-name()="claim-text"]/text()')
print(a, flush=True)
1 Answers

A couple of problems here before we get to a possible solution. One, the first xml snippet you provided is invalid (for instance, the <bibliographic-data> isn't closed). I realize it's just a snippet but since this is what we have to work with, I modified the snippet below to fix that. Two, both snippets have xmlns declaration with unbound (unused) prefixes (example:world-datain the first, and ops:world-data in the second). I had to remove these prefixes, too, for the rest to work.

Given these modifications, using the lxml library should work for you.

First modified snippet:

 my_xml = """<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.2/style/exchange.xsl"?>
<world-data xmlns="http://www.example.org" xmlns:ops="http://example.oorg" xmlns:xlink="http://www.w3.oorg/1999/xlink">
  <exchange-documents>
    <exchange-document system="acb.org" family-id="543672" country="US" doc-number="95962" kind="B2">
      <bibliographic-data>
             <name>SomeName</name>
       ...and so on... and ends like this
         </bibliographic-data>
   </exchange-document>
  </exchange-documents>
</world-data>"""

And:

my_xml2 = """<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/3.2/style/pub-ftxt-claims.xsl"?>
<world-data xmlns="http://www.example.org/exchange" xmlns:example="http://example.org" xmlns:xlink="http://www.example.org/1999/xlink">
  <ftxt:fulltext-documents xmlns="http://www.examp.org/fulltext" xmlns:ftxt="ww.example/fulltext">
    <ftxt:fulltext-document system="example.org" fulltext-format="text-only">
      <bibliographic-data>
        <publication-reference data-format="docdb">
          <document-id>
            <country>EP</country>
            <doc-number>10000</doc-number>
            <kind>A</kind>
          </document-id>
        </publication-reference>
      </bibliographic-data>
      <claims lang="EN">
        <claim>
          <claim-text>1. Some text.</claim-text>
          <claim-text>2. Some text.</claim-text>
          <claim-text>3. Some text.</claim-text>
        </claim>
      </claims>
    </ftxt:fulltext-document>
  </ftxt:fulltext-documents>
</world-data>"""

And now to work:

from lxml import etree
root = etree.XML(my_xml.encode('ascii'))
root2 = etree.XML(my_xml2.encode('ascii'))

root.xpath('//*[local-name()="name"]/text()')

output:

['SomeName']

root2.xpath('//*[local-name()="claim-text"]/text()')

Output:

['1. Some text.', '2. Some text.', '3. Some text.']

Related