【问题标题】:bash - how to extract all of the same tag in xmlbash - 如何在 xml 中提取所有相同的标签
【发布时间】:2017-11-16 15:18:39
【问题描述】:

给出这个文件.xml

  <session id = 1111>

    <query text = text1 >
            <response>
                    firstresponse1
            </response>
            <response>
                    secondresponse1
            </response>
    </query>
    <query text = secondtext >
            <response>
                    !!!aresponse2!!!
            </response>
            <response>
                    !!!aresponse3!!!
            </response>
    </query>
    <query text = thirdtext>
            <response>
                    firstreponse3
            </response>
            <response>
                    secondresponese4
            </response>
    </query>
    </session>

我想在 secondtext

中获取两个响应标签

输出:

!!!响应2!!!

!!!响应3!!!

.

最有效的方法是什么?

【问题讨论】:

  • Bash 没有提供解析 XML 的工具。我希望您需要使用外部工具,可能是可以让您根据XPath 进行查询的工具。你试过什么?
  • 请注意,您的输入文件不是格式良好的 XML。属性必须在 XML 中引用。

标签: html xml bash


【解决方案1】:

正确的方法是使用 XML/HTML 解析器,如 xmllintxmlstarlet

xmllint解决方案:

xmllint --html --xpath "//query[@text='secondtext']/response/text()" file.xml 2>/dev/null

输出:

            !!!aresponse2!!!

            !!!aresponse3!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2020-10-06
    • 2014-07-29
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多