我有一个类似下面输入的XML。在
这里我想聚集Diesel
燃料类型的Sales
值。在
如何迭代所有<Tank>
元素并读取fuelItem
属性,以找到相同燃料类型的多个实例,然后求出Sales
属性值的总和?在
输入:
<EnterpriseDocument>
<FuelTankList>
<Tank fuelItem="Petrol" Sales="1000" />
<Tank fuelItem="Diesel" Sales="2000" />
<Tank fuelItem="Diesel" Sales="3000" />
</FuelTankList>
</EnterpriseDocument>
首选输出:
^{pr2}$
由于您使用的是lxml,所以可以使用XSLT和Muenchian Grouping按其
fuelItem
属性对Tank
元素进行分组。在示例。。。在
XML输入(输入.xml)在
XSLT 1.0(测试.xsl)在
^{pr2}$Python
输出(标准输出)
试试这个:
^{pr2}$$ xmllint -format so-output.xml
的输出:希望这有帮助。它迭代每个fueltanklist,从中获取一个坦克列表,检索其值并删除它们。一旦我们有了这些值并对其进行了操作,我们会将带有过程值的新油箱添加到燃油箱列表中。在
相关问题 更多 >
编程相关推荐