使用xmlstarlet提取多个值

时间:2013-07-19 11:34:14

标签: xmlstarlet

如何从所有元素中提取“失败”并将其添加?

<gateway>
    <smscs>
        <count>3</count>
        <smsc>
            <id>a</id>
            <received><sms>0</sms><dlr>0</dlr></received>
            <sent><sms>10537</sms><dlr>0</dlr></sent>
            <failed>13</failed>
            <queued>6272</queued>
        </smsc>
        <smsc>
            <id>b</id>
            <received><sms>0</sms><dlr>0</dlr></received>
            <sent><sms>10530</sms><dlr>0</dlr></sent>
            <failed>10</failed>
            <queued>6284</queued>
        </smsc>
        <smsc>
            <id>c</id>
            <received><sms>0</sms><dlr>0</dlr></received>
            <sent><sms>10679</sms><dlr>0</dlr></sent>
            <failed>11</failed>
            <queued>6291</queued>
        </smsc>
    </smscs>
</gateway>

2 个答案:

答案 0 :(得分:4)

我只是用

xmlstarlet sel -t -v "sum(/gateway/smscs/smsc/failed)" -n input.xml

返回

34

我们的想法是使用sum()函数来获取节点集,并返回所有元素的字符串值转换为数字的总和。

答案 1 :(得分:0)

解决了

xmlstarlet sel -t -m "gateway/smscs/smsc/failed" -v "." -n | awk '{s+=$1} END {print s}'