如何使用xpath获取元素的位置。例子。我有一个xml如下。
class Song(object):
def _init_(self, lyrics):
self.lyrics=lyrics
def sing_me_a_song(self):
for line in self.lyrics:
print line
happy_bday = Song(["Happy birthday to you",
"I dont want to get sued",
"So I'll stop right there"])
bulls_on_parade = Song(["The rally around the family",
"with pockets ful of shales"])
happy_bday.sing_me_a_song()
bulls_on_parade.sing_me_a_song()
我正在尝试使用xpath表达式<a>
<b>
<c>
<ID>12324234</ID>
<FirstName>Something</FirstName>
<LastName>Somethingelse</LastName>
</c>
<c>
<ID>12324235</ID>
<FirstName>Something</FirstName>
<LastName>Somethingelse</LastName>
</c>
</b>
</a>
提取元素id
。我坚持的是,我也想提取这个位置。
示例
/a/b/c/ID/text()
逗号只是为了理解。我可以使用上面提到的xpath将数据提取为第一步,然后使用xpath作为位置。请注意,数组可以有n个值,但不限于2。
我试过计数,位置等,但没有运气。任何人都可以建议我一些选择
答案 0 :(得分:2)
You can try below XPath to get required output:
/a/b/c/concat(./ID/text(), ", ", (count(./preceding-sibling::c/ID) + 1))
Output:
12324234, 1
12324235, 2