对于xml文件foo.xml
<?xml version="1.0" encoding="UTF-8" ?>
<record><header><identifier>oai:tib.eu:TIBKAT:010000011</identifier><datestamp>2020-10-12</datestamp><setSpec>tibkat</setSpec></header><metadata><marcxml:collection xmlns:marcxml="http://www.loc.gov/MARC21/slim">
<marcxml:record>
<marcxml:leader>Hello world</marcxml:leader>
</marcxml:record>
</marcxml:collection>
</metadata></record>XMLStarlet查询
xmlstarlet sel -N xmlns="http://www.loc.gov/MARC21/slim" -t -v '//_:collection/_:record/_:leader[text()]' -nl foo.xml由于未定义的命名空间前缀,导致出现错误消息:
未定义的命名空间前缀xmlXPathCompiledEval:计算失败的运行时错误:带有-param的元素无法计算变量'select‘的表达式。foo.xml无结果
为什么查询不起作用并传递"Hello“?
发布于 2020-11-14 23:08:38
您使用了错误的前缀(加上一个小错误):
xmlstarlet sel -N marcxml="http://www.loc.gov/MARC21/slim" -t -v '//marcxml:collection/marcxml:record/marcxml:leader[text()]' -nl foo.xml或者,更简单:
xmlstarlet sel -t -v '//*[local-name()="leader"][text()]' -nl foo.xml输出:
Hello worldhttps://stackoverflow.com/questions/64839030
复制相似问题