Jürgen O.
Jürgen O.

Reputation: 33

xmlstarlet: selecting all values based on element and attribute

I'm learning-by-doing xmlstarlet to perform a specific task:

To select every <value> in an element <outcomeVariable> with the parent <itemResult> and additional with the attribute @identifier="MAXSCORE" (and print them line-by-line).

With this example-file

<test>
    <itemResult>
        <outcomeVariable identifier="MAXSCORE" cardinality="single" manualScored="false" baseType="float" view="scorer">
        <value baseType="float">1.5</value>
        </outcomeVariable>
        <outcomeVariable identifier="MAXSCORE" cardinality="single" manualScored="false" baseType="float" view="scorer">
        <value baseType="float">2.5</value>
        </outcomeVariable>
        <outcomeVariable identifier="MAXSCORE" cardinality="single" manualScored="false" baseType="float" view="scorer">
        <value baseType="float">3.5</value>
        </outcomeVariable>
    </itemResult>
        <outcomeVariable identifier="MAXSCORE" cardinality="single" manualScored="false" baseType="float" view="scorer">
        <value baseType="float">4.5</value>
        </outcomeVariable>
</test>

my code

xmlstarlet sel --template --match "//itemResult/outcomeVariable[@identifier='MAXSCORE']" -v . *.xml

put's out

        1.5
        
        2.5
        
        3.5

(with leading spaces and blank lines)

[edit] I would like to have

1.5
2.5
3.5

[/edit].

BUT with the (extract of the) real file (from where I just copied the example-node and changed the values)

<?xml version="1.0" encoding="UTF-8"?>
<assessmentResult xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_v2p1 imsqti_v2p1.xsd">
<context />
<testResult identifier="idTest9cb05b88-72ee-47fe-92b0-c517bf5ee46b" datestamp="2020-11-21T12:17:57" currentItemIndex="11">
<responseVariable identifier="duration" cardinality="single" baseType="duration">
<candidateResponse>
<value baseType="integer">500</value>
</candidateResponse>
</responseVariable>
<responseVariable identifier="id8a88d3f5-631d-45dd-a3ef-29f6b2457d78.duration" cardinality="single" baseType="duration">
<candidateResponse>
<value baseType="integer">245</value>
</candidateResponse>
</responseVariable>
<outcomeVariable identifier="SCORE_id8a88d3f5-631d-45dd-a3ef-29f6b2457d78" cardinality="single" manualScored="false" baseType="float" view="tutor">
<value baseType="float">13.5</value>
</outcomeVariable>
<outcomeVariable identifier="MAXSCORE_id8a88d3f5-631d-45dd-a3ef-29f6b2457d78" cardinality="single" manualScored="false" baseType="float" view="scorer">
<value baseType="float">16</value>
</outcomeVariable>
<outcomeVariable identifier="MINSCORE_id8a88d3f5-631d-45dd-a3ef-29f6b2457d78" cardinality="single" manualScored="false" baseType="float" view="scorer">
<value baseType="float">0</value>
</outcomeVariable>
<outcomeVariable identifier="SCORE_idd35753ab-a9bd-4234-b7ea-d1db89ce6516" cardinality="single" manualScored="false" baseType="float" view="tutor">
<value baseType="float">0</value>
</outcomeVariable>
<outcomeVariable identifier="MAXSCORE_idd35753ab-a9bd-4234-b7ea-d1db89ce6516" cardinality="single" manualScored="false" baseType="float" view="scorer">
<value baseType="float">2</value>
</outcomeVariable>
<outcomeVariable identifier="MINSCORE_idd35753ab-a9bd-4234-b7ea-d1db89ce6516" cardinality="single" manualScored="false" baseType="float" view="scorer">
<value baseType="float">0</value>
</outcomeVariable>
</testResult>
<itemResult identifier="iddb301400-85da-4652-9162-088a49f927f0_1" datestamp="2021-07-26T09:18:51" sessionStatus="final" sequenceIndex="1">
<responseVariable identifier="duration" cardinality="single" baseType="duration">
<candidateResponse>
<value baseType="integer">54</value>
</candidateResponse>
</responseVariable>
<responseVariable identifier="RESPONSE_1" baseType="identifier" cardinality="multiple" choiceSequence="ID_3 ID_2 ID_6 ID_5 ID_4 ID_1">
<correctResponse>
<value baseType="identifier">ID_1</value>
<value baseType="identifier">ID_2</value>
</correctResponse>
<candidateResponse>
<value baseType="identifier">ID_2</value>
<value baseType="identifier">ID_1</value>
</candidateResponse>
</responseVariable>
<outcomeVariable identifier="SCORE" cardinality="single" manualScored="false" baseType="float" view="tutor">
<value baseType="float">2</value>
</outcomeVariable>
<outcomeVariable identifier="MAXSCORE" cardinality="single" manualScored="false" baseType="float" view="scorer">
<value baseType="float">2</value>
</outcomeVariable>
<outcomeVariable identifier="MINSCORE" cardinality="single" manualScored="false" baseType="float" view="scorer">
<value baseType="float">0</value>
</outcomeVariable>
</itemResult>
</assessmentResult>

the output is empty ...

I found different tutorials etc., where the --template --match "//... is described for searching the whole file for that pattern.

So my questions are

  1. Why is the code producing different ouput (none) when using the real file (is there a significant difference I've missed)?
  2. How to get the same output with the real file (resp. the values stored there, they are different to the ones in the example of course)?
  3. How can I avoid the leading spaces (and the blank lines)?

I hope to get some answers/hints where to look for a solution to my promblem(-s)!

Best regards,

J.

Upvotes: 2

Views: 895

Answers (0)

Related Questions