tags:

views:

38

answers:

3

Sample XML:

<assignments>
<assignment id="911990211" section-id="1942268885" item-count="21" sources="foo">
    <options>
        <value name="NumRetakes">4</value>
        <value name="MultipleResultGrading">6</value>
        <value name="MaxFeedbackAttempts">-1</value>
        <value name="ItemTakesBeforeHint">1</value>
        <value name="TimeAllowed">0</value>
    </options>
</assignment>
<assignment id="1425185257" section-id="1505958877" item-count="4" sources="bar">
    <options>
        <value name="NumRetakes">0</value>
        <value name="MultipleResultGrading">6</value>
        <value name="MaxFeedbackAttempts">3</value>
        <value name="ItemTakesBeforeHint">1</value>
        <value name="TimeAllowed">0</value>
    </options>
</assignment>
<assignments>

Using XPath, I would like to select all assignments/assignment/options/value nodes where the nodes "name" attribute is "MaxFeedbackAttempts" and the nodes content is "-1". That is to say, I want to return each node that looks like:

<value name="MaxFeedbackAttempts">-1</value>

I can get each assignments/assignment/options/value node with the specified attribute using:

//assignment/options/value[@name="MaxFeedbackAttempts"]

I am just not sure how to refine this path to also limit the results based on the nodes content. Is there any way to do this using XPath?

+1  A: 

You can use multiple [...] constraints, and probably want an XPath query like this:

//assignment/options/value[@name="MaxFeedbackAttempts"][text()="-1"]
Donal Fellows
Your solution would be a valid answer, if you change `value()` to `text()` or `.`.
Mads Hansen
@Mads: I knew I should have tested! Fixed…
Donal Fellows
+3  A: 
//assignment/options/value[@name="MaxFeedbackAttempts" and text()="-1"]
LaustN
Wonderful. Exactly what I was looking for. Thanks!
Andrew Kirk
+1  A: 

Use:

/assignments/assignment/options/value[@name='MaxFeedbackAttempts' and . = -1]

Try to avoid the // abbreviation as its evaluation can be very inefficient.

Dimitre Novatchev
Good to know. Thanks!
Andrew Kirk
@Andrew-Kirk: The practice here is to accept and upvote the useful answers. There should be something like a check mark next to an answer -- to be accepted upon clicking on this picture. :)
Dimitre Novatchev
@Dimitre Novatchev: Thanks for pointing out the process of accepting an answer. My bad, I should have read the posting instructions... At this time, I don't have the reputation to upvote and answer but here is a +1. :-)
Andrew Kirk