1

Here is my code and XML:

xml_string = """
<data>
    <user>
        <name>123</name>
    </user>
    <user>
        <name>456</name>
    </user>
</data>
"""

import xml.etree.ElementTree as ET
root = ET.fromstring(xml_string)

I'm trying to find <user> tag where <name> has text value equal to 123.

What I have tried:

result = root.findall("./user[name = '123']")
result = root.findall("./user[./name = '123']")
result = root.findall("./user[/name = '123']")
result = root.findall("./user[./name text() = '123']")

None of these attempts worked. What am I missing?

Error I'm getting:

raise SyntaxError("invalid predicate") 
File "<string>", line None
SyntaxError: invalid predicate
João Paulo
  • 6,300
  • 4
  • 51
  • 80
  • Maybe regex could work? – Joe T. Boka Jul 03 '15 at 12:24
  • If this is what you're trying to find `123`, regex will do the job. – Joe T. Boka Jul 03 '15 at 12:28
  • 7
    @JoeR that's exactly the opposite of the advice you should give; regex is *not* the best way to parse XML, which is *not* a regular language, see e.g. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – jonrsharpe Jul 03 '15 at 12:29
  • @jonrsharpe Yes, I agree and I would never suggest to parse HTML or XML using regex, however, if the OP just trying to find this one thing `123` that's an other story. – Joe T. Boka Jul 03 '15 at 12:33
  • @JoeR but that isn't what the OP wants, they're after the `user` tag *containing* that. – jonrsharpe Jul 03 '15 at 12:39
  • As agmangas pointed out, the syntax is invalid. Supported selectors and their syntax can be found here: https://docs.python.org/3/library/xml.etree.elementtree.html#example as well as some examples. – Aleksander S Jul 03 '15 at 12:41
  • @jonrsharpe I am not sure if the OP is trying to find the `` tag or he is really after the `123`, in any case, that's why I mentioned it in the comments and not posted an regex answer, because I wasn't sure what the OP is looking for exactly. – Joe T. Boka Jul 03 '15 at 12:45
  • *"I'm trying to find tag where has text value equal to 123."* – jonrsharpe Jul 03 '15 at 12:46
  • @jonrsharpe In any case, it seems that the user's name is the important data. – Joe T. Boka Jul 03 '15 at 12:49

1 Answers1

8

As the exception says, it seems you have a syntax error in the predicate, there shouldn't be a space between the tag name and the value:

xml_string = """
<data>
    <user>
        <name>123</name>
    </user>
    <user>
        <name>456</name>
    </user>
</data>
"""

import xml.etree.ElementTree as ET
root = ET.fromstring(xml_string)

result = root.findall("./user[name='123']")

print result
agmangas
  • 639
  • 5
  • 9