1

I want to define an xml element as a dictionary or string or list or whatever. This will be just a description of an xml element. It doesn't need to exist (that's why I don't use lxml or other libraries). For example having this

my_xml_element = {
                    "tag"       : "input",
                    "attribute" : ("value", "Login")
                 },

I want to get this

input[@value="Login"]

Does a module exist that can do that? I started doing my own implementation but want to be sure that I am not reinventing the wheel. Cheers!

3
  • 1
    Wait -- you're trying to generate an XPath string which will match only the specific document given as input? Why? What would the use of such a thing be? Commented Sep 12, 2014 at 19:50
  • I want to generate an xpath string which will match an element, not a document. In the end I just want to create a priority list of xml elements to look for. I want to tell the program "first look for this type of xml element, then for this and then for that". Commented Sep 12, 2014 at 21:17
  • The XPath language supports parameterization -- just as SQL does with bind variables. You might consider whether that's sufficient for your needs, particularly since there are accessors available to refer to elements' names as strings. Commented Sep 12, 2014 at 21:31

1 Answer 1

1

This is kinda what xml.etree.ElementTree is. Look at the Element() init routine:

def __init__(self, tag, attrib={}, **extra):
    attrib = attrib.copy()
    attrib.update(extra)
    self.tag = tag
    self.attrib = attrib
    self._children = []

A class is really just a dict wrapped with additional functionality. As your code matures, the dict will turn into a class, you'll add parent and clild references, implement a 'find' and finally.... end up with ElementTree.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! However etree doesn't have some sort of function to produce an xpath, right? From what I'm aware it simply CAN take xpaths.
Okay, I think I got all of this backwards. So ... you don't want to define xml elements. You want to create some sort of an element selector scheme that you can use to generate the xpath that is then used against some document? I guess that'll work for simple stuff but if you start getting into complex xpath selectors... well, it could get "interesting" - and sorry, this answer aint it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.