Hi everyone. I have a XML file with the structure as shown below:
<x>
<y/>
<y/>
.
.
</x>
The number of <y>
tags are arbitrary.
I want to get the text of the <y>
tags and for this I decided to use XPath. I have figured out the syntax, say for the first y
: (Assume root
as x
)
textFirst = root.xpath('y[1]/text()')
This works as expected.
However my problem is that I won't be knowing the number of <y>
tags beforehand, so to fix that, I did this:
>>> count = 0
>>> for number in root.getiterator('y'):
... count += 1
So now I know that there are count
number of y
in x
. (Is there a better way to get the number of tags? If yes, please suggest)
However, if I do this:
>>> def try_it(x):
... return root.xpath('y[x]/text()')
...
>>> try_it(1)
[]
It returns an empty list.
So my question is: not knowing the arbitrary number of tags, how do I get an XPath syntax or expression for it and using lxml
?
Sorry if something is not clear, I tried my best to explain the problem.