1

I just learned Prototype for Javascript. It's super convenient: using the $ shortcut, accessing xml elements is not painful any more!

The question: is there a Prototype-like extension for Python?

2
  • I'm not sure quite what you are after when you say $ expanding but there's an xml library for python: docs.python.org/library/xml.dom.minidom.html. Commented Sep 17, 2010 at 5:23
  • 1
    I don't wanna start any flame-war type scenario, but JQuery is a similar library that has a much larger user-base. Commented Sep 17, 2010 at 5:33

1 Answer 1

1

Python has lxml which has the xpath method wherein you could use xpath expressions to select elements. As I understand it, $ in prototype searches and returns an element that has a particular id, in which case could be translated in xpath to *[@id=<someid>] like so:

>>> import lxml.etree
>>> tree = lxml.etree.XML("<root><a id='1'/><b id='2'/></root>")
>>> tree.xpath("*[@id=1]")
[<Element a at c3bc30>]
>>> lxml.etree.tostring(tree.xpath("*[@id=1]")[0])
'<a id="1"/>'

I think the Python standard library includes support for a subset of xpath in ElementTree too so you might be able to implement that there somehow if you do not wish to install lxml (which isn't included in stdlib)...

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

Comments

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.