python - How to select the following specific XML nodes using XPath? -


i have xml doc following:

<objects>   <object distname="a/b">  </object>   <object distname="a/b/c1">  </object>   <object distname="a/b/c4/d/e">  </object>   <object distname="a/b/c2">  </object>   <object distname="a/b/c6/d">  </object> </objects> 

and need select nodes has path ends "c" + number. like: "a/b/c1" , "a/b/c2" not "a/b/c6/d", nor "a/b/c4/d/e".

if try following:

      `cnodes = xmldoc.xpath("//object[contains(@path, `a/b/c`)]")` 

then include "a/b/c6/d" , "a/b/c4/d/e" not require.

so there way job in 1 or maybe 2 lines of code. mean can loop , stuff that, don't want to. that's because real xml doc thousands of nodes.

ps: python 2.7, lxml

unfortuantely it's not simple express condition matches patterns using xpath 1.0. if can make assumptions you're looking for, can craft such query.

//object[starts-with(@distname, 'a/b/c') , substring-after(@distname, 'a/b/c') >= 0] 

breaking up, we're first checking if distname attribute starts a/b/c. if after string number. depending on needs, might enough.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -