Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:33:47+00:00 2026-06-06T02:33:47+00:00

XPath returns nothing if a child element has no text value. In this case,

  • 0

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]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:33:48+00:00Added an answer on June 6, 2026 at 2:33 am

    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]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Xpath query inside a loop, //div[@class='listing_content'][#{i}]/div/div/h3/a/text() I want to process each
Can anyone please suggest an XPath expression format that returns a string value containing
I have seen several things on this, but nothing has seemed to work so
I want to write an XPath expression that returns true if it is a
I've created an xpath expression as follows: /html/body/div[2]/div/ul[2]/li/a/node() It returns something like this: #01
Using lxml in python I created this xpath syntax htmlPage.xpath(/html/body//a/text()) It gets me all
Given this XML, what XPath returns all elements whose prop attribute contains Foo (the
I have xpath page.search(//table[@class='campaign']//table) which returns two tables. I need to choose only first
Let's imagine that I have a result of evaluating XPath expression //node/@*. MSXML6 returns
I need an xpath expression that would return the value of I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.