如何用XPath 2.0从MARC21-XML中精准提取译者姓名
Let’s fix that XPath to only capture actual translators and avoid pulling in other contributors. The core issue with your original expression was that it didn’t filter based on the contributor’s role code—we just need to add a check for the trl (translator) role in the subfield[@code='4'].
Here’s the refined XPath 2.0 expression:
/record/datafield[@tag='700'][@ind1='1'][@ind2=' '][subfield[@code='4'] = 'trl']/subfield[@code='a']/text()
Breakdown of the expression:
/record/datafield[@tag='700'][@ind1='1'][@ind2=' ']: First, we target the700datafields (for personal names) that match the indicator values you specified (ind1="1"for direct entry,ind2=" "with no second indicator).[subfield[@code='4'] = 'trl']: This is the critical filter. It ensures we only keepdatafields that have asubfieldwithcode="4"(the MARC role code field) whose text is exactly"trl"(the code for "translator")./subfield[@code='a']/text(): Finally, we extract the text content of thesubfieldwithcode="a"—which holds the contributor’s name.
How it works with your examples:
Example XML 1: The expression returns only the two translators:
Wasel, Ulrike Timmermann, KlausIt correctly ignores the
datafieldwithind2="2"since it doesn’t meet the indicator criteria.Example XML 2: It still returns the same two translators, and excludes John Doe—his
subfield[@code='4']has"oth"(other role), so he gets filtered out as intended.
Quick note: If your XPath processor requires handling the MARC21 namespace (
http://www.loc.gov/MARC21/slimfrom your XML), you’ll need to bind a namespace prefix (e.g.,marc) and adjust the expression to use it:/marc:record/marc:datafield[@tag='700'][@ind1='1'][@ind2=' '][marc:subfield[@code='4'] = 'trl']/marc:subfield[@code='a']/text()
内容的提问来源于stack exchange,提问作者burnersk

