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 <>
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 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
  <person name="Kate">
    <breakfast available="true">Idly</breakfast>
  <person name="John">
    <breakfast available="false">Dosa</breakfast>

# 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>

# 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
Date User Action Args
2019-08-25 08:06:22xtreaksetrecipients: + xtreak, rhettinger, scoder, eli.bendersky, serhiy.storchaka
2019-08-25 08:06:22xtreaksetmessageid: <>
2019-08-25 08:06:22xtreaklinkissue37940 messages
2019-08-25 08:06:22xtreakcreate