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

  • Home
  • SEARCH
  • 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 9132605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:20:20+00:00 2026-06-17T08:20:20+00:00

I am attempting to parse a maven project definition using python to extract a

  • 0

I am attempting to parse a maven project definition using python to extract a version.

The project definition looks like:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                        http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>...</groupId>
   <artifactId>...</artifactId>
   <version>1.6.0-SNAPSHOT</version>
   ...
</project>

I can extract the version using:

root = ET.fromstring(xml)
version = root.find('./p:version', { 'p': 'http://maven.apache.org/POM/4.0.0' })
print(version.text)

prints: 1.6.0-SNAPSHOT

However, the namespace used may change, and I don’t want to depend on this. Is there a way to extract the namespace to use in my subsequent xpath expression?

I tried the following, to see if xmlns was itself exposed, but no luck:

root = ET.fromstring(xml)
for k in root.attrib:
    print('%s => %s' % (k, root.attrib[k]))

prints: {http://www.w3.org/2001/XMLSchema-instance}schemaLocation => http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd
  • 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-17T08:20:21+00:00Added an answer on June 17, 2026 at 8:20 am

    However, the namespace used may change, and I don’t want to depend on this.

    Are you saying that the namespace uri might change, or that the prefix might? If it’s just the prefix, then that’s not an issue, because what matters is that the prefixes in your XPath match the prefixes you supply to the XPath evaluator. And in either case, auto-detecting the namespaces is probably a bad call. Suppose someone decides to start generating that XML like this:

    <proj:project xmlns:proj="http://maven.apache.org/POM/4.0.0" 
    xmlns:other="http://maven.apache.org/POM/5.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                        http://maven.apache.org/maven-v4_0_0.xsd">
    

    which is still perfectly representing the XML in the same namespace as your example, but you have no idea that the proj prefix is the namespace prefix you’re looking for.

    I think it’s unlikely that Apache would suddenly change the namespace for one of their official XML formats, but if you are genuinely worried about it, there should always be the option of using local-name() to namespace-agnostically find a node you’re looking for:

    version = root.find('./*[local-name() = "version"]')
    

    Also, I’m not familiar with the elementTree library, but you could try this to try to get information about the XML document’s namespaces, just to see if you can:

    namespaces = root.findall('//namespace::*')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to parse an XML file using python expat. I have the
I am attempting to parse a XML file using Javascript and I'm running into
I am attempting to parse a string like the following using a .NET regular
I am attempting to parse html data from a website using BeautifulSoup for python.
I have an XML message I am attempting to parse using GXT and AutoBeans.
I have become accustomed to using TryParse for attempting to parse unknown types: Dim
I'm attempting to parse a JSON feed using the LastFM API but there are
I'm attempting to parse a set of CSV data using PHP, but having a
I am attempting to parse an XML and store the URL from one element
I am attempting to parse a text (CSS) file using fscanf and pull out

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.