tags:

views:

67

answers:

5

I have an xml file like this:

<students>
<student name=....>
</student>
<student name=....>
</student>
<student name=....>
</student>
<student name=....>
    <failedcourses>
          ...
    </failedcourses>
</student>
<student name=....>
    <failedcourses>
          ...
    </failedcourse>
</student>
</students>

I.e. I have a list of students, a portion of which have a subelement , but most of the students don't have this subelement.

I want to get the name of the nth student that has failed a course, how do I do that?

I want an XPath expression that works, I don't care about performance.

+2  A: 

Ok, so the answer seems to be pretty easy. Just use parenthesis:

(/student/failedcourses)[3]/../@name

In the example, n=3

flybywire
A: 

Do you mind using an xquery?

let $studentsWhoFailed := $myxml/students/student/failedcourse

return $studentsWhoFailed[n]/@name

(syntax might not be 100% correct as I haven't tested it)

rxin
A: 

Hmm, just for fun and variety (and its way late so I won't guarantee the syntax):

student[count(failedcourses) > 0][3]/@name
Murph
+2  A: 
(/students/student[failedcourses])[3]/@name
vtd-xml-author
A: 

If i need failed Coursed based on student name...How can i get?

test