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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:25:54+00:00 2026-06-06T16:25:54+00:00

I am trying to match for country or Country using lower-case function in XPath.

  • 0

I am trying to match for country or Country using lower-case function in XPath. translate is kinda messy, so using lower-case and my Python version 2.6.6 has XPath 2.0 support I believe since lower-case is only available in XPath 2.0.

How I can put lower-case to use in my case is what I am looking for. Hope the example is self explanatory. I am looking for ['USA', 'US'] as output (both countries in one go which can happen if lower-case evaluates Country and country to be the same).

HTML: doc.htm

<html>
    <table>
        <tr>
            <td>
                Name of the Country : <span> USA </span>
            </td>
        </tr>
        <tr>
            <td>
                Name of the country : <span> UK </span>
            </td>
        </tr>
</table>

Python :

import lxml.html as lh

doc = open('doc.htm', 'r')
out = lh.parse(doc)
doc.close()

print out.xpath('//table/tr/td[text()[contains(. , "Country")]]/span/text()')
# Prints : [' USA ']
print out.xpath('//table/tr/td[text()[contains(. , "country")]]/span/text()')
# Prints : [' UK ']

print out.xpath('//table/tr/td[lower-case(text())[contains(. , "country")]]/span/text()')
# Prints : [<Element td at 0x15db2710>]

Update :

out.xpath('//table/tr/td[text()[contains(translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") , "country")]]/span/text()')

Now the question remains, can I store the translate part as a global variable ‘handlecase’ and print that global variable whenever I do an XPath?

Something like this works :

handlecase = """translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")"""

out.xpath('//table/tr/td[text()[contains(%s , "country")]]/span/text()' % (handlecase))

But for sake of simplicity and readability, I want to run it like this :

out.xpath('//table/tr/td[text()[contains(handlecase , "country")]]/span/text()')
  • 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-06T16:25:56+00:00Added an answer on June 6, 2026 at 4:25 pm

    Use:

       //td[translate(substring(text()[1], string-length(text()[1]) - 9),
                      'COUNTRY :',
                      'country'
                      )
            =
             'country'
           ]
            /span/text()
    

    XSLT – based verification:

    <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:copy-of select=
      "//td[translate(substring(text()[1], string-length(text()[1]) - 9),
                      'COUNTRY :',
                      'country'
                      )
            =
             'country'
           ]
            /span/text()
           "/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <html>
            <table>
                <tr>
                    <td>
                        Name of the Country : <span> USA </span>
                    </td>
                </tr>
                <tr>
                    <td>
                        Name of the country : <span> UK </span>
                    </td>
                </tr>
            </table>
    </html>
    

    the XPath expression is evaluated and the selected two text-nodes are copied to the output:

     USA  UK 
    

    Explanation:

    1. We use a specific variant of the XPath 1.0 expression that implements the XPath 2.0 standard function ends-with($text, $s): this is:

    …..

    $s = substring($text, string-length($text) - string-length($s) +1)
    

    .2. The next step is, using the translate() function, to convert the ending 10-character long string to lowercase, eliminating any spaces or any “:” character.

    .3. If the result is the string (all lowercase) “country”, then we select the children text nodes (only one in this case) of the s=span child of this td.

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

Sidebar

Related Questions

I'm trying to match the h4 of a div (using jQuery) so that I
I'm trying to match elements with a name that is 'container1$container2$chkChecked' , using a
I am trying to find a function to match two data frames of different
Im trying to match forum_id with several different forum_id's, something like forum_id = 5,7,12,43,63,78
I'm trying to match a string against a pattern, but there's one thing I
I'm trying to match the '12345' from a url in this form: http://domain.com/folder/title_of_this_12345 So
I'm trying to match expressions that start with PRE and end with PRE. I'm
I am trying to match on a string that begins with ./media I have
I am trying to match words driven from a database in a string input
I'm trying to match variables in some PHP code which are missing their leading

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.