我正在尝试构造一个 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/