Skip to content
Advertisement

XML/lxml Parsing first occurrence using something like [0] maybe?

The code below returns all the “str1″‘s but I only want the first “str1” for each one of these: CrntRgstns->BrnchOfLocs->BrnchOfLoc. As in just “13A MAIN ST” not also “8 WATER ST.” Is there some type of [0] that would accomplish this? Thank you.

Current Code:

JavaScript

Current Output:

JavaScript

Desired Output:

JavaScript

xml for 1 individual:

JavaScript

Advertisement

Answer

To fetch first item, use [1] (you were so close with 0!).

For example: tree.xpath('.//CrntRgstns/following-sibling::BrnchOfLocs/BrnchOfLoc[1]')

Try:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement