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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:58:53+00:00 2026-06-18T05:58:53+00:00

I have an xml file like this: <data> <entry> <word>ABC</word> (this) </entry> <entry> <word>ABC</word>

  • 0

I have an xml file like this:

<data>
      <entry>
           <word>ABC</word> (this)
      </entry>
      <entry>
           <word>ABC</word> [not this]
      </entry>
</data>

I want to select nodes whose descendant include “(“, and move (.*) to the text of <entry>. That is:

<data>
      <entry>
           (this)
           <word>ABC</word>
      </entry>
      <entry>
           <word>ABC</word> [not this]
      </entry>
</data>

I’m using lxml. And I tried:

 import lxml.etree as ET
 data = ET.parse('sample.xml')
 for entry in data.iter('entry'):
      A = entry.xpath('.//*[text() = ".*(.*?)"]')

But it doesn’t work. “(” can appear as a tail of a node or as a text of a node.

  • 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-18T05:58:55+00:00Added an answer on June 18, 2026 at 5:58 am

    There are a few problems here:

    First, you’re trying to use xpath to do regex matching, but you’re using =. Your regex is also improperly formatted. To actually do regex matching in xpath, you’d need to do something like:

    import lxml.etree as ET
    data = ET.parse('sample.xml')
    regexpNS = "http://exslt.org/regular-expressions"
    for entry in data.iter('entry'):
        A = entry.xpath('.//*[re:test(text(), ".*\(.*\).*")]',
                        namespaces={'re':regexpNS})
    

    Unfortunately, this won’t actually work for you because you want text in the tail, which isn’t included in text(). The lxml docs make it look like this should be included in string(), but I tried that and it doesn’t work either. I can’t find any way to do this using xpath and lxml.

    So, here’s a way to do it with more Python and less xpath:

     import re
     import lxml.etree as ET
     rx = re.compile('.*\(.*\).*')
     data = ET.parse('sample.xml')
     for entry in data.iter('entry'):
        for child in entry.xpath('.//*'):
            if rx.match(child.text + child.tail):
                # Your manipulations go here
                print child
    

    In either case, a happy side effect is that this regex is having a totally rockin’ good time in the snow: .*\(.*\).*.

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

Sidebar

Related Questions

I have an XML file that reads like this <abc> <ab>value</ab> <aa>time</aa> <ac>money</ac> </abc>
If I have a XML file like this one: <?xml version='1.0' encoding='ISO-8859-1'?> <data> <tag1>Some
I have XML file (output of some application) like this: <data> <call function=get_user_data> <output>
I have an XML data file that looks like this: <Sales> <Store Number=8 Date=2012-08-20T03:00:00>
I have a xml file like this : <data> <season id=00> <project id=text/> <project
I have an xml file that looks like this: <args> <sometag value=abc /> <anothertag
Basically I have an XML file that looks like this: <?xml version=1.0 encoding=UTF-8?> <data>
I have an XML file that looks like this example: <Data> <defect> <record-id>1</record-id> <custom-field-value
I have a XML file like this <?xml version=1.0 encoding=utf-8?> <Survey Name=Aerosmith's Survey Id=S2
I have an xml file like this: <result> <customer> <id>1</id> <name>A</name> </customer> <customer> <id>2</id>

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.