This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author xtreak
Recipients eli.bendersky, rhettinger, scoder, serhiy.storchaka, xtreak
Date 2019-08-25.08:06:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1566720382.69.0.0923645107348.issue37940@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2019-08-25 08:06:22xtreaksetrecipients: + xtreak, rhettinger, scoder, eli.bendersky, serhiy.storchaka
2019-08-25 08:06:22xtreaksetmessageid: <1566720382.69.0.0923645107348.issue37940@roundup.psfhosted.org>
2019-08-25 08:06:22xtreaklinkissue37940 messages
2019-08-25 08:06:22xtreakcreate