XMLStarlet:MARC21的命名空间前缀未定义

3

对于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

由于未定义的命名空间前缀,导致出现错误消息:

Undefined namespace prefix xmlXPathCompiledEval: evaluation failed runtime error: element with-param Failed to evaluate the expression of variable 'select'. no result for foo.xml

为什么查询无法工作并提供“Hello world”?

1个回答

3
您使用了错误的前缀(加上一个小错误拼写):
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 world

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接