#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Simple XPath Query Raises Exception


    Why does the following line of code raise a "SyntaxError: invalid predicate" exception?

    Code:
    root.find("def/hij[text()=klm]")
    My simple code is:

    Code:
    import xml.etree.ElementTree as ET
    tree = ET.parse("testXML.xml")
    root = tree.getroot() # root = "abc/" element (ie, main element)
    
    test = root.find("def/hij")                 # Does not raise exception
    test = root.find("def/hij[text()=klm]") # raises exception
    My XML file:

    Code:
    <abc>
    	<def>
    		<hij>blah</hij>
    		<hij>klm</hij>
    	</def>
    </abc>
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    I'd say you get that syntax error because etree doesn't support that syntax. http://docs.python.org/3/library/xml...d-xpath-syntax

    Maybe it works with expat
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo