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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:38:37+00:00 2026-05-21T10:38:37+00:00

I have an xpath for a node in an xml file which does not

  • 0

I have an xpath for a node in an xml file which does not yet exist, and want to use it to generate the node in question. I’ve started work on a function to do this but was wondering if there’s an existing library which can do it and save me some time? I’m currently working with pyxml but am considering porting it to ElementTree. So to clarify I want :

root/foo/bar

to produce:

<root>
  <foo>
    <bar>
    </bar>
  </foo>
</root>

My suspicion is that the behaviour of such a function is not well enough defined for the general case for anyone to have bothered but thought I’d throw it out there just in case. I have a DTD for the file as well if that would help.

  • 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-21T10:38:38+00:00Added an answer on May 21, 2026 at 10:38 am

    Didn’t find anything ready,
    but it should be more or less straightforward using ElementTree (or even another xml library – it is just that I am more acquainted with ElementTree).

    The snippet bellow seems to work for the limited subset of xpath that is needed for it:

    # -*- coding: utf-8 -*-
    from xml.etree import ElementTree as ET
    
    def build_xpath(node, path):
        components = path.split("/")
        if components[0] == node.tag:
            components.pop(0)
        while components:
            # take in account positional  indexes in the form /path/para[3] or /path/para[location()=3]
            if "[" in components[0]:
                component, trail = components[0].split("[",1)
                target_index = int(trail.split("=")[-1].strip("]"))
            else:
                component = components[0]
                target_index = 0
            components.pop(0)
            found_index = -1
            for child in node.getchildren():
                if child.tag == component:
                    found_index += 1
                    if found_index == target_index:
                        node = child
                        break
            else:
                for i in range(target_index - found_index):
                    new_node = ET.Element(component)
                    node.append(new_node)
                node = new_node
    
    
    if __name__  == "__main__":
        #Example
        root = ET.Element("root")
        build_xpath(root, "root/foo/bar[position()=4]/snafu")
        print ET.tostring(root)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file I have a node which is there per layer node
I have this xml file and I want to get some values with Xpath.
I have an application which extracts data from an XML file using XPath. If
I have the below xml file and I need to use an xpath to
I have an XML document that includes XPath expressions that I need to use
If I have a string which contains proper xml format and I want to
I have an XML-File, which has the following structure: <mediawiki ...> <siteinfo>...</siteinfo> <page> <title>The
I have an XML file, part of which looks like this: <wave waveID=1> <well
I have a big xml file which i'm parsing using jscript. I have used
I have an xml document and need XPath expression to select node values based

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.