bash - 以非递归方式列出可用标签的 XPath(在 shell 脚本中使用 xmllint)

标签 bash list xpath tags

我正在尝试构造一个 XPath 参数以在程序 xmllint 中使用(在 Bash shell 脚本中使用),它将返回标记内的可用标记列表(但不列出子标记)。

这是我拥有的 XML 类型:

<functionInformation>
    <class>
        setup
    </class>
    <description>
        This is a natural language description of this function.
    </description>
    <prerequisiteFunctions>
        myFunction1
        myFunction2
    </prerequisiteFunctions>
    <prerequisitePrograms>
        myProgram1
        myProgram2
    </prerequisitePrograms>
</functionInformation>

此 XML 存储在 Bash 变量 functionInformation 中。

在此 XML 上使用 xmllint 时,我希望得到的输出如下:

class
description
prerequisiteFunctions
prerequisitePrograms

我应该注意,我希望以非递归方式返回标签​​(我不希望列出所有可用标签或子标签)。

我可以使用 xmllint 以如下方式访问标签中的信息:

descriptionFunctionInformation="$(echo "${functionInformation}"\
                | xmllint --xpath '/functionInformation/description/text()' -\
                | xargs -i echo -n "{}")"

关于如何构建 XPath(或类似的东西)以返回我需要的信息,您能否为我指明正确的方向?

最佳答案

你可以使用xmlstarlet:

xmlstarlet sel -t -m '/*/*' -v 'concat(name(.)," ")' < xmlfile

关于bash - 以非递归方式列出可用标签的 XPath(在 shell 脚本中使用 xmllint),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14780233/

相关文章:

Python 和子进程中的别名

python - 给定一个整数列表,确定 70% 的值是否在其中一个值的 20% 以内

名称中的 CSS 空间和麻烦的 XSLT

python - 使用 Xpath (HtmlXPathSelector) 获取父文本和子文本

bash - 将 shell 输出绑定(bind)到 Go 中的结构的最佳方法?

linux - 如何在 Linux 中启动停止的进程

bash - 如何grep粗体文本

python - 删除由相同数字组成的列表元素

python - Pandas Dataframe - 值是列表

java - 如何等到没有带有属性ready_to_send的span标签,或者换句话说,没有带有send属性的span标签