MARCXML文件foo.xml的结构如下:
<record><header><identifier>myID001</identifier><datestamp>2020-10-12</datestamp></header><metadata><marcxml:collection xmlns:marcxml="http://www.loc.gov/MARC21/slim">
<marcxml:record>
<marcxml:datafield ind1=" " ind2=" " tag="084">
<marcxml:subfield code="2">rvk</marcxml:subfield>
<marcxml:subfield code="a">MG 98092</marcxml:subfield>
</marcxml:datafield>
<marcxml:datafield ind1=" " ind2=" " tag="084">
<marcxml:subfield code="2">bk</marcxml:subfield>
<marcxml:subfield code="a">89.52</marcxml:subfield>
</marcxml:datafield>
<marcxml:datafield ind1=" " ind2=" " tag="084">
<marcxml:subfield code="2">ddc</marcxml:subfield>
<marcxml:subfield code="a">320.9439</marcxml:subfield>
</marcxml:datafield>
</marcxml:record>
</marcxml:collection>
</metadata></record>
我只想提取<marcxml:subfield code="a">
的内容,其中前一个字段<marcxml:subfield code="2">
包含字符串"bk"。
因此,在这个例子中,期望的输出是:89.52。
到目前为止,我尝试了
xmlstarlet sel -N marcxml="http://www.loc.gov/MARC21/slim" -t -m "//marcxml:collection/marcxml:record/marcxml:datafield/marcxml:subfield[text()='bk']" -v '//marcxml:collection/marcxml:record/marcxml:datafield/marcxml:subfield[text()]' -nl foo.xml
导致
rvk
MG 98092
bk
89.52
ddc
320.9439
XMLStarlet如何做到这一点?
尝试以下方法:
xmlstarlet sel -N marcxml="http://www.loc.gov/MARC21/slim" -t -v '//marcxml:subfield[@code="2"][text()="bk"]/following-sibling::marcxml:subfield[@code="a"]' -nl foo.xml