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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:36:42+00:00 2026-06-09T16:36:42+00:00

XML keeps throwing me curve balls. I am having a hard time finding a

  • 0

XML keeps throwing me curve balls. I am having a hard time finding a manual I can understand. So I apologize for all the questions in the past couple of days.

In any case, I have the following XML:

      <clade>
        <clade>
          <branch_length>0.5</branch_length>
          <clade>
            <name>MnPV1</name>
            <annotation>
<desc>Iotapapillomavirus 1</desc></annotation><chart><group>Iota</group></chart><branch_length>1.0</branch_length>
          </clade>
          <clade> 

I would like to change this to:

  <clade>
    <clade>
      <branch_length>0.5</branch_length>
      <clade>
        <name bgstyle="green">MnPV1</name>
        <annotation><desc>Iotapapillomavirus 1</desc><uri>http://pave.niaid.nih.gov/#fetch?id=MnPV1REF&amp;format=Locus%20view&amp;hasStructure=none</uri></annotation><chart><group>Iota</group></chart><branch_length>1.0</branch_length>
      </clade>
      <clade>

So I want to change:

<name>MnPV1</name>

to:

<name bgstyle="green">MnPV1</name>

The catch is, that I am looking for whether :

tree.xpath('//phylo:group[text()="Iota"]

If it is I would like to get the “uncle” of “group” node so I can edit the “name” node

This is what I came up with so far:

tree = lxml.etree.XML(data)
nsmap = {'phylo': 'http://www.phyloxml.org'}
matches = tree.xpath('//phylo:group[text()="Iota"]', namespaces=nsmap)
for e in matches:
    uncle=e.getparent().getsibling() #however, getsibling() does not exist...

I would appreciate any help (and/or recommendations for lxml for dummies).

  • 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-09T16:36:44+00:00Added an answer on June 9, 2026 at 4:36 pm

    How about this?

    >>> data = r'''<clade>
    ...  <name>MnPV1</name>
    ...  <annotation>
    ...    <desc>Iotapapillomavirus 1</desc>
    ...  </annotation>
    ...  <chart>
    ...    <group>Iota</group>
    ...  </chart>
    ...  <branch_length>1.0</branch_length>
    ... </clade>'''
    ...
    >>> tree = lxml.etree.XML(data)
    >>> for name in tree.xpath('//group[text()="Iota"]/../preceding-sibling::name'):
    ...   name.attrib['bgstyle'] = 'green'
    ...
    >>> print lxml.etree.tostring(tree, pretty_print=True)
    <clade>
     <name bgstyle="green">MnPV1</name>
     <annotation>
       <desc>Iotapapillomavirus 1</desc>
     </annotation>
     <chart>
       <group>Iota</group>
     </chart>
     <branch_length>1.0</branch_length>
    </clade>
    
    >>>
    

    The trick is to use XML tools (e.g., XPath and XSLT) to manipulate XML documents. The w3schools sites are pretty good starting points. XPath is fairly powerful in its own right and is quite readable once you get the hang of it. This type of problem is best solved using XSLT though. If you are going to be manipulating a bunch of XML, do yourself a huge favor and purchase a copy of the Oxygen XML editor or something similar.

    If you are looking for something using less XPath and more Python, then use the getparent followed by calls to getprevious. I’m not sure how well supported getparent and getprevious are, but they are documented and do work.

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

Sidebar

Related Questions

I'm try to parse this xml, but c# keeps throwing an exception saying it
I'm currently having trouble with XMLParse() , as the title says, it keeps throwing
The problem : My index.jsp with web.xml keeps going into HTTP 404 and 500
If I want to keep an xml file updated with all the images that
I need to strip all xml tags from an xml document, but keep the
The codebase keeps files in XML format and javascript receives them via ajax and
I have a file that keeps appending the following xml. What I want to
Good Afternoon, I have a Joomla! template that keeps throwing up a pop up
I am having a really frustrating problem in CakePHP where a space keeps coming
I have Word Add-in, which keeps updating winword.exe.config.xml file present at C:\program files\microsoft office\.

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.