xmlstarlet:根据元素和属性选择所有值

我正在边学边做 xmlstarlet 以执行特定任务:

选择元素 <value> 中的每个 <outcomeVariable>,父元素 <itemResult> 和附加属性 @identifier="MAXSCORE"(并逐行打印)。>

使用这个示例文件

<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>

我的代码

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

放出来

        1.5
        
        2.5
        
        3.5

(带前导空格和空行)

[edit] 我想要

1.5
2.5
3.5

[/edit]。

但是使用(提取的)真实文件(从那里我刚刚复制了示例节点并更改了值)

<?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>

输出为空 ...

我发现了不同的教程等,其中描述了 --template --match "//... 用于在整个文件中搜索该模式。

所以我的问题是

  1. 为什么代码在使用真实文件时会产生不同的输出(无)(我是否遗漏了显着差异)?
  2. 如何获得与真实文件相同的输出(特别是存储在那里的值,当然它们与示例中的值不同)?
  3. 如何避免前导空格(和空行)?

我希望得到一些答案/提示,在哪里寻找解决我的问题的方法(-s)!

最好的问候,

J.

shijy07 回答:xmlstarlet:根据元素和属性选择所有值

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/5380.html

大家都在问