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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:04:51+00:00 2026-06-09T18:04:51+00:00

I am playing around with an xml file found @ http://www.jsphylosvg.com/examples/source.php?example=2&t=xml I would like

  • 0

I am playing around with an xml file found @ http://www.jsphylosvg.com/examples/source.php?example=2&t=xml

I would like to insert a node if the value of the node name="Espresso".

E.g. I would like to change from:

<clade>
<name>Espresso</name>
<branch_length>2.0</branch_length>
</clade>

to:

<clade>
<name>Espresso</name>
<url>www.espresso.com</url>
<branch_length>2.0</branch_length>
</clade>

Based on the research I have done thus far I can use xpath to find the node that contains espresso (this should work, but it does not?)

import re, sys
import lxml.etree
f = open("test.xml", "r")
data = f.read()
tree = lxml.etree.XML(data)
if tree.xpath('//name/text()="Espresso"'):
    insert new child here

At this point, it should be possible to use use lxml.etree.Element to make the xml node, and use insert to attach them into xml document

However, while this sounds great in theory, I am unable to get it to work.
I would really appreciate any help/suggestions

  • 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-09T18:04:53+00:00Added an answer on June 9, 2026 at 6:04 pm

    Your XPath statement is not exactly right. Here’s what I think you want:

    >>> DOC = """<clade>
    ... <name>Espresso</name>
    ... <branch_length>2.0</branch_length>
    ... </clade>"""
    >>> 
    >>> import lxml.etree
    >>> tree = lxml.etree.XML(DOC)
    >>> matches = tree.xpath('//name[text()="Espresso"]')
    

    Then to append the element after the matches:

    >>> for e in matches:
    ...    sibling = lxml.etree.Element("url")
    ...    sibling.text = "www.espresso.com"
    ...    e.addnext(sibling)
    
    >>> print lxml.etree.tostring(tree)
    <clade>
    <name>Espresso</name><url>www.espresso.com</url>
    <branch_length>2.0</branch_length>
    </clade>
    

    EDIT:

    Since your document has a namespace, you want to pass a namespace map to the XPath function and prefix the tag name with the namespace prefix, like this:

    >>> nsmap = {'phylo': 'http://www.phyloxml.org'}
    >>> tree.xpath('//phylo:name[text()="Espresso"]', namespaces=nsmap)
    [<Element {http://www.phyloxml.org}name at 0x2c875f0>]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still playing around with xml. Now I have a file that looks like
Playing around in order to learn XSLT, I have the following XML file and
I am playing around with saving .xml files from data in my game. I
I've been playing around with code I found at Android Development - Playing with
I've been playing around with the xml serialization for a while and I've hit
I've been playing around with XmlDataProvider with inline XML. Here's my code: <XmlDataProvider x:Key=InternalData
I just started playing around with Phing build scripts (built with PHP). From what
After playing around with the Authorize.Net CIM XML API C# sample code , I
Playing around with Python - tkInter - Entry widget - when I use validatecommand
Playing around with MongoDB and NoRM in .NET. Thing that confused me - there

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.