2

[N.B. I'm quite new to Python & Xpath]

I was wanting to perform an Xpath query and use a variable in the expression to cycle through all the elements using an index.

For example, rather than having the specific position of [1], [2], etc, I'd like to be able to place a variable i in this expression:

for x in root.xpath('/movies/a_movie[i]/studio'):
    print "<studio>" + x.text + "</studio>"

I'm unsure whether it's even possible, but I guess it can't hurt to ask!

To clarify, this is why I would like to do this: I'm trying to process all the elements of a_movie[1], then at the end of the function, I want to process all the elements of a_movie[2], and so on.

2
  • Just leave outh the [i] altogether? You are selecting studio elements that are inside of a a_movie element, you already would be looping over all matches.. Commented Mar 28, 2013 at 11:15
  • @MartijnPieters I had that originally, but it didn't fit my needs – I'm trying to process all the elements of a_movie[1], then at the end of the function, I wanted to process all the elements of a_movie[2]. Commented Mar 28, 2013 at 11:19

1 Answer 1

1

You want to loop over the /movies/a_movie tags instead, but only those that have a studio child:

for a_movie in root.xpath('/movies/a_movie[studio]'):
    name = a_movie.find('name').text
    studio = a_movie.find('studio')
    print "<studio>" + studio.text + "</studio>"

If you wanted to just print out the child elements of the a_movie element as XML, I'd just loop over the element (looping over the child elements) and use ElementTree.tostring() on each:

for a_movie in root.xpath('/movies/a_movie'):
    for elem in a_movie:
        print ElementTree.tostring(elem),
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks, Martijn. However, this still results in the issue I mentioned before; when I run my script, it prints all the child names successively – e.g. it printed all the studio names of each of the a_movie elements in the XML file at once. I want it to print the children names of a_movie[1], then all the children names of a_movie[2], then a_movie[3]… The reason I'd like to do this is to use the same block of code to process each of the a_movie elements.
@Daniel: This loops over the a_movie tags in order. If there are multiple <studio> tags inside each a_movie tag, then loop over those too. You didn't include a sample of your input XML, so I have to guess at what you mean here.
Here is the input XML: cl.ly/code/2N3F1g3L2W0V I hope that helps clarify…
@Daniel: What is the problem then? This code does what you want to do already. It loops over a_movie tags in order, then finds the studio tag inside. Inside the loop you are processing the a_movie tag. Added printing the movie name.
You must be misunderstanding my intention – here is the desired output: <title>The Lord of the Rings: The Fellowship of the Ring</title> <rating>PG</rating> <studio>New Line Cinema</studio> <genre>Fantasy</genre> etcetera for each movie. In other words, I would like to reproduce the XML code how it currently appears.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.