开发者

XPath expression help

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>
          开发者_StackOverflow中文版...
    </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.


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


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

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

In the example, n=3


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)


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

student[count(failedcourses) > 0][3]/@name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜