How to make XPath return 'None' in Python if no data found?
Question: XPath returns nothing if a child element has no text value. In this case,
rating has no data, so I want it to say so - None or nothing in this child
instead of just ignoring it. Your input is much appreciated.

**XML :**

    
    
    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <bookstore>
    
    <book>  
      <title lang="eng">Harry Potter</title>
      <price>29.99</price>
      <rating></rating>
    </book>
    
    <book>
      <title lang="hindi">Learning XML</title>
      <price>39.95</price>
      <rating></rating>
    </book>
    
    </bookstore>
    

**Python :**

    
    
    >>> import lxml.html as lh
    >>> bk=open('book.xml','r')
    >>> bkout=lh.parse(bk)
    >>> bk.close()
    >>> bkout.xpath('//book/*/text()')
    ['Harry Potter', '29.99', 'Learning XML', '39.95']
    
    >>> bkout.xpath('//book/* and not(text())/text()')
    True
    

**Desired Output :**

    
    
    ['Harry Potter', '29.99', '', 'Learning XML', '39.95', '']
    or
    ['Harry Potter', '29.99', None, 'Learning XML', '39.95', None]
    


Answer: Remove the "text()":

    
    
    In [16]: [x.text for x in bk.xpath("//book/*")]
    Out[16]: ['Harry Potter', '29.99', None, 'Learning XML', '39.95', None]
    

