Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:

Thanks Stefan for the link. XPath support sounds cool to me given that there is 
already support in stdlib. It could help with filtering using xml.tool itself 
instead of passing the output to another command to filter. My initial approach 
was to take it from command line --xpath argument and apply it to root node to 
pretty print the elements that match the XPath query. I have pushed the xpath 
changes also to https://github.com/tirkarthi/cpython/tree/bpo14465-xml-tool. I 
will try to add docstrings with xpath examples and tests to raise a PR for this.

# Sample XML

$ python -m xml.tool /tmp/person.xml
<root>
  <person name="Kate">
    <breakfast available="true">Idly</breakfast>
  </person>
  <person name="John">
    <breakfast available="false">Dosa</breakfast>
  </person>
</root>

# Select person with name as Kate

$ python -m xml.tool --xpath './person[@name="Kate"]' /tmp/person.xml
<person name="Kate">
  <breakfast available="true">Idly</breakfast>
</person>

# Get all unavailable breakfast items

python -m xml.tool --xpath './/breakfast[@available="false"]' /tmp/person.xml
<breakfast available="false">Dosa</breakfast>


It could also mask the traceback to return error when the XPath is invalid and 
raises exception.


# Error messages

$ python -m xml.tool --xpath './person/[breakfast='Dosa']' /tmp/person.xml
invalid predicate
$ python -m xml.tool --xpath './/[breakfast=Dosa]' /tmp/person.xml
invalid descendant

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37940>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to