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

XMLStarlet: Undefined namespace prefix for MARC21

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

您使用了错误的前缀(还有一个小错字):

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