2

Trying to parse data.xspf with Python 3.6.8 and LXML 4.4.1 following this and find <creator>Creator</creator> element but have [] output.

data.xspf

<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="http://xspf.org/ns/0/" version="1">
  <title/>
  <creator/>
  <trackList>
    <track>
      <location>http://localhost:8000</location>
      <creator>Creator</creator>
      <title>Title</title>
      <annotation>Blah
Blah
Blah
Blah
Blah
Blah
Blah</annotation>
      <info>info</info>
    </track>
  </trackList>
</playlist>

Script:

>>> from lxml import etree

>>> tree = etree.parse("data.xspf")
>>> tree.findall('.//creator')

Any idea?

Pavel Antspovich
  • 1,111
  • 1
  • 11
  • 27
  • 1
    In you sample the creator tag is empty (``). For testing purpose you may try another node : `tree.findall('.//track')` – Stephan Sep 03 '19 at 09:47
  • @Stephan, I guess this case output shoud be like `````` cause the element itself is not None. The text it contains is empty. ```tree.findall('.//track')``` -- the same result – Pavel Antspovich Sep 03 '19 at 09:51
  • And by the way ```findall()``` should find all elements inсluding the last one – Pavel Antspovich Sep 03 '19 at 09:54

1 Answers1

4

Use the second argument of the function to make sure the default namespace is taken into account tree.findall('.//creator', { None : 'http://xspf.org/ns/0/' }).

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110