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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:31:36+00:00 2026-05-21T07:31:36+00:00

Ok, I have a case where I need to add a tag to a

  • 0

Ok, I have a case where I need to add a tag to a certain other tag given an xpath.

Example xml:

<?xml version="1.0" encoding="UTF-8"?>
<Assets>
 <asset name="Adham">
  <general>>
   <services>
    <land/>
    <refuel/>
   </services>
  </general>
 </asset>
 <asset name="Test">
  <general>
   <Something/>
  </general>
 </asset>
</Assets>

I want to add a <missions> tag to both assets. However, the second asset is missing the parent <services> tag, which I want to add. Each asset tag is stored in a variable (say node1, node2).

I have the following xpath: xpath1 = services/missions, which, due to the way my program works, I cannot simply store in a different form (i.e. i don’t have a place to store just services)

I need to check and see if the missions tag already exists, and, if so, do nothing. If the tag does not exist, I need to create it. If its parent does not exist, I need to create that too.

How can I do this simply by using the xpath string?

Edit: I want to base all this on a boolean value: i.e. val = true, then create tag (and parent) if neccesary. If false, then delete tag.

(I have no other way of referring to the tag I need (since I have layers on layers of functions to automate this process on a large scale, you can check out my previous question here Python Lxml: Adding and deleting tags)).

Edit edit: Another issue:

I don’t have a variable containing the parent of the element to add, just a variable containing the <asset> object. I am trying to get the parent of the node I want using the xpath and a variable pointing to the ` tag.

Edit edit edit: Never mind the above, I will fix the issue by shorting the xpath to point to the parent, and using a variable name to refer to each item.

  • 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-21T07:31:37+00:00Added an answer on May 21, 2026 at 7:31 am
    def to_xml(parent, xpath, value):
        """
        parent: lxml.etree.Element
        xpath: string like 'x/y/z', anything more complex is likely to break
        value: anything, if is False - means delete node
        """        
        # find the node to proceed further        
        nodes = parent.xpath(xpath)        
        if nodes:
            node = nodes[0]
        else:
            parts = xpath.split('/')
            p = parent
            for part in parts:
                nodes = p.xpath(part)
                if not nodes:
                    n = etree.XML("<%s/>" % part)
                    p.append(n)
                    p = n
                else:
                    p = nodes[0]
            node = p
        # do whatever is specified vy value
        if value is False:
            node.getparent().remove(node)
        else:
            node.text = str(value)
    

    Although I’m not sure that combining add & remove functionality in 1 function is good idea, but anyway this is likely to work as you’re expecting.

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

Sidebar

Related Questions

I have this tag in example: <button onclick=setLocation('http://mysite.com/checkout/cart/add/product/17/') /> What I need to do
I have a use case where I only need to store certain fields to
I have a case where I need to select a random item, but I
I have a case where I need to load bitmap from a resource dll
I have a case where I need to update a jqgrid based on some
I have a use case where I need to call a (non-static) method in
I have a very specific case that I need to debug. I need to
I have a complicated problem, and I need help. I have a base case,
I have a strong use case for pre-allocating all the memory I need upfront
So I'm trying to add UIImageViews programatically (in this case I don't have the

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.