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 7592149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:50:02+00:00 2026-05-30T20:50:02+00:00

I have the following XPath to match authors name in an Amazon page: //div[@class=’pTitle’]/span[@class=’small

  • 0

I have the following XPath to match authors name in an Amazon page:

//div[@class=’pTitle’]/span[@class=’small itemByline’] | //div[@class=’pTitle’]/span[not(text())]

The first part of this XPath matches it just fine, however some items in the page have no span after such div with class pTitle, so there’s nothing to match, but I’d like to either get a ” or something else, to know the author was not found for real instead of just skipping it. I suppose the second XPath is invalid as it does not work…

For instance, the 3 titles starting with ‘A Ditadura’ should return ” for the author entry using the XPath I’m building. They’re not though. It’s making the above XPath return 179 items instead of 209.

Target is http://www.amazon.com/wishlist/3MCYFXCFDH4FA/ref=cm_wl_act_print_o?_encoding=UTF8&layout=standard-print&disableNav=1&visitor-view=1&items-per-page=1000

This is part of the code of my Python module https://github.com/caio1982/Amazon-Wishlist (thanks by the way for all the good answers in SO so far, I’ve learned XPath thanks to you guys).

For sake of info, I’m trying this using Firefox’s XPath Checker extension, implementing it with Python (lxml).

It sounds similar to How do I return '' for an empty node's text() in XPath? but I’m not sure though.

I suspect the answer may be something around XPath axes and a [notcontains] restriction of some sort?

EDIT1: rephrasing it a bit after Dimitre’s suggestion… is it possible to use –– and if so, do you have a working example of –– Becker’s XPath method using lxml?

EDIT2: sample tree and expected results:

    <html>
        <body>
            <h1>Title</h1>
            <p>First Paragraph</p>
            <p>Second paragraph: <span>value</span></p>
            <p>Third paragraph: <span>value</span></p>
            <p>Forth paragraph:</p>
        </body>
    </html>

XPath //p/span returns the Second and the Third paragraph ‘value’ strings accordingly. That’s ok, but I’m looking for 4 results instead of 2, like this:

    None
    value
    value
    None

I know //p/span does not work for this, hence I’m looking for some string-magic, node comparison or conditionals etc.

  • 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-05-30T20:50:03+00:00Added an answer on May 30, 2026 at 8:50 pm

    You can use an XPath expression like this one:

    concat(
    //div[@class='pTitle']/span[@class='small itemByline'],
    substring('UNKNOWN', 
              1 + 7*(boolean(//div[@class='pTitle']/span[@class='small itemByline'])
              )
           )
    

    When this XPath expression is evaluated, and if //div[@class='pTitle']/span[@class='small itemByline'] exists, then its string value (concatenated with the empty string) is produced.

    When //div[@class='pTitle']/span[@class='small itemByline'] doesn’t exist, then the result is the string 'UNKNOWN' — the empty string is concatenated with substring('UNKNOWN', 1+0).

    Here we use the fact that in XPath 1.0 whenever a boolean value is an argument of an arithmetic operator, it is first converted to a number, using the rule that:

       number(true()) = 1
    

    and

       number(false()) = 0
    

    Update: Here is an XSLT- based verification, using the XML document from the EDIT 2 by the OP and producing exactly the wanted result (the Same XPath expression (only an index is updated) is evaluated 4 times and all produced values are output — each on a separate line):

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
      <xsl:for-each select="(//node())[not(position() > count(//p))]">
       <xsl:variable name="vPos" select="position()"/>
       <xsl:value-of select=
         "concat((//p)[position() = $vPos]/span,
                 substring('UNKNOWN',
                           1 +7*boolean((//p)[position() = $vPos]/span)
                           )
                 )
         "/>
    
         <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the latest provided XML document:

    <html>
        <body>
            <h1>Title</h1>
            <p>First Paragraph</p>
            <p>Second paragraph: 
                <span>value</span>
            </p>
            <p>Third paragraph: 
                <span>value</span>
            </p>
            <p>Forth paragraph:</p>
        </body>
    </html>
    

    the XPath expression is evaluated N (4) times and the results of this evaluation are produced — as we see, these are exactly the wanted results:

    UNKNOWN
    value
    value
    UNKNOWN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XPath to match attributes of the class span: //span[@class='amount'] I
i have the following code to for xpath query... <div class=buying> <h1 class=parseasinTitle >
I have the following XPath: //div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href When I try out this XPath
I have the following xpath statement in a selenium test: //tbody/tr[td/span[text()='IPODate']]/td[4]/input It gets what
I have the following XPath expression: //*[@name='outputFormat']/ows:Value I would like to make it case
I have the following XPath 1.0 query: /root/Nodes/*[self::CustomNode[not(../DefaultNode)]|self::DefaultNode]/Name As I understand it, this will
I have HTML which looks basically like the following: ... <a class=btnX btnSelectedBG href=#><span>Sign
I have the following xpath query which seems to be working but I just
I have the following XPATH line: //det[@nItem=1]/prod/cProd That successfully selects the desired node using
I have the following html and like to know how to use xpath 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.