Xpath> How to choose a node based on both its attributes and content?

XML example:

<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 the destination / destination / option / value nodes, where the "name" attribute of the nodes is "MaxFeedbackAttempts" and the contents of the nodes are "-1". That is, I want to return every node that looks like this:

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

I can get each assignment / assignment / parameters / value of a node with the specified attribute using:

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

I'm just not sure how to refine this path to also limit the results based on the contents of the nodes. Is there a way to do this using XPath?

+4
source share
3 answers

Using

/ assignments / assign / options / value [@ name = "MaxFeedbackAttempts" and. = -1]

Try to avoid the abbreviation // because evaluating it can be very inefficient.

+3
source
 //assignment/options/value[@name="MaxFeedbackAttempts" and text()="-1"] 
+5
source

You can use several restrictions [...] and you might want to receive an XPath request as follows:

  // assignment / options / value [@ name = "MaxFeedbackAttempts"] [text () = "- 1"] 
+3
source

Source: https://habr.com/ru/post/1312201/


All Articles