Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLStarlet: Undefined namespace prefix for MARC21

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"?

like image 807
Yahalnaut Avatar asked Nov 19 '25 20:11

Yahalnaut


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
like image 154
Jack Fleeting Avatar answered Nov 23 '25 11:11

Jack Fleeting



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!