【发布时间】:2018-02-02 06:28:52
【问题描述】:
示例 XML:
<parts>
<title>computer parts</title>
<components>
<hardware>motherboard</hardware>
<id>1234</id>
</components>
<part>
<item>motherboard</item>
<manufacturer>asus</manufacturer>
<model>p3b-f</model>
<cost> 123.00</cost>
</part>
<components>
<hardware>video card</hardware>
<id>2345</id>
</components>
<part>
<item>video card</item>
<manufacturer>ati</manufacturer>
<model>all-in-wonder pro</model>
<cost> 160.00</cost>
</part>
<components>
<hardware>sound card</hardware>
<id>3456</id>
</components>
<part>
<item>sound card</item>
<manufacturer>creative labs</manufacturer>
<model>sound blaster live</model>
<cost> 80.00</cost>
</part>
</parts>
在上面的示例 XML 中,我需要首先在 components 元素中搜索值 motherboard;如果在那里找到该值,那么我们需要在item标签下的<part>元素中搜索该值,如果该值在item标签中可用,那么我们应该得到model的值标记。
注意:我已经突出显示了需要搜索的值和需要提取的值。
如何在 Bash 中做到这一点?
【问题讨论】:
-
到目前为止你尝试过什么?你能告诉我们你的代码吗?您是否尝试过使用
xmlstarlet? -
不要使用 bash;而是使用带有 xpath 的 xmllint 或 xmlstartlet 来完成此操作。理想情况下,您应该使用具有 XML 解析器的脚本语言。
-
你能用你给出的示例 xml 举一个例子吗?我不明白您是在寻找一个名为
motherboard的标签还是一个值。
标签: xml bash shell unix scripting