I am using xmlstarlet to extract text from elements with a specific preceding sibling event. An example from the XML file:
<event type='cue' units='sec'>
<onset>11.134</onset>
<duration>0.2</duration>
<value name='side'>CUER</value>
</event>
<event type='target' units='sec'>
<onset>11.367</onset>
<duration>1.26</duration>
<value name='side'>TARGETR</value>
<value name='RT' units='msec'>379</value>
<value name='TargCorr'>1</value>
<value name='feedback'>YOU WIN!</value>
</event>
<event type='anticipation' units='sec'>
<onset>12.651</onset>
<duration>2.65</duration>
<value name='TargCorr'>1</value>
<value name='feedback'>YOU WIN!</value>
</event>
From the example, I need to do the following:
- print the
onsetof<event type='target', and - print the sum of the
durationof the<event type='target'and immediately followingdurationof the<event type='anticipation'.
I can print the correct onset using the "preceding-sibling" option:
xmlstarlet sel -t \
-m '//event[@type="anticipation" and value[@name="feedback"]="YOU WIN!"]' \
-m 'preceding-sibling::event[@type="target" and value[@name="feedback"]="YOU WIN!"][1] ' \
-v 'onset' -o ' ' -v 'duration' -o ' ' -o '1' -n $DIR/$xml \
> $DIR/output.stf
Though as written, the following code only displays the duration from the matched element rather than summing the duration of the two adjacent events. Is the latter possible using xmlstarlet?
Thank you for your help!