Created
February 12, 2013 18:54
-
-
Save sandsfish/4772275 to your computer and use it in GitHub Desktop.
R function to parse out list of specific field/sub-field from MARC/XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(XML) | |
getMARCField = function(marc_doc, tag, code) { | |
xpath = paste("/m:collection/m:record/m:datafield[@tag='", tag, "']/m:subfield[@code='", code, "']", sep="") | |
return(xpathApply(marc_doc, xpath, namespaces=c("m"), xmlValue)) | |
} | |
vs = xmlRoot(xmlParse('vail-first-3500.xml')) | |
field100a = getMARCField(vs, '100', 'a') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After fighting with R's XPath support for a bit, I figured out how to assign the default namespace, which it doesn't do automagically...
Also, using xmlParse, as it turns out, is key to using XPath, since it keeps nodes represented in C-level objects, which allows XPath to work. xmlTreeParse (unless useInternalNodes = FALSE is specified) does not allow this, and is also much slower.