XMLStarlet: Undefined namespace prefix for MARC21

Viewed 413

For the xml file 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>

the XMLStarlet query

xmlstarlet sel -N xmlns="http://www.loc.gov/MARC21/slim" -t -v '//_:collection/_:record/_:leader[text()]' -nl foo.xml

results in an error message due to an undefined namespace prefix:

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

Why does the query not work and deliver "Hello world"?

1 Answers

You used the wrong prefix (plus a minor typo):

xmlstarlet sel -N marcxml="http://www.loc.gov/MARC21/slim" -t -v '//marcxml:collection/marcxml:record/marcxml:leader[text()]' -nl foo.xml

or, simpler:

xmlstarlet sel -t -v '//*[local-name()="leader"][text()]' -nl foo.xml

Output:

Hello world
Related