user225312
user225312

Reputation: 131807

XPath and lxml syntax

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.

Upvotes: 0

Views: 1811

Answers (2)

mlschechter
mlschechter

Reputation: 994

To count the number of y nodes, you can use the XPath expression 'count(/x/y)'.

Also, I think the problem with your expression in the try_it function is that you appear to be using the literal value x instead of concatenating the input parameter into the XPath expression.

Maybe something like this would work:

 >>> def try_it(x):
...     return root.xpath('y[' + x + ']/text()')

Hope this helps!

Upvotes: 1

mykhal
mykhal

Reputation: 19924

what about 'y[%i]/text()' % x ?

now you see where you did a mistake? :)

( .. note that you can capture all y elements together with xpath 'y' or '//y' )

Upvotes: 1

Related Questions