Skip to content

Instantly share code, notes, and snippets.

@gravesm
Created March 8, 2013 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravesm/5116963 to your computer and use it in GitHub Desktop.
Save gravesm/5116963 to your computer and use it in GitHub Desktop.
XSLT to remove duplicate instances of MARC subject fields.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:marc="http://www.loc.gov/MARC21/slim"
xmlns:fn="http://libraries.mit.edu/xslt/functions">
<xsl:template match="/">
<add>
<xsl:apply-templates select="marc:collection/marc:record" />
</add>
</xsl:template>
<xsl:template match="marc:record">
<doc>
<xsl:apply-templates />
</doc>
</xsl:template>
<xsl:template match="marc:datafield[@tag='650'][marc:subfield[@code='a']]">
<!-- Since there could be multiple 650 elements, only do the grouping if
this is the first 650 element. -->
<xsl:if test="count(. | ../marc:datafield[@tag='650'][marc:subfield[@code='a']][1]) = 1">
<!-- Grab all the 650a elements, normalize the text and then group
by the normalized text. -->
<xsl:for-each-group
select="../marc:datafield[@tag='650']/marc:subfield[@code='a']"
group-by="fn:rstrip(.)">
<field name="ThemeKeywords">
<xsl:value-of select="current-grouping-key()" />
</field>
</xsl:for-each-group>
</xsl:if>
</xsl:template>
<!-- Functions -->
<xsl:function name="fn:rstrip" as="xs:string">
<xsl:param name="input" as="xs:string" />
<xsl:variable name="pattern">
<xsl:text>[:;,.\s]+$</xsl:text>
</xsl:variable>
<xsl:value-of select="replace($input, $pattern, '')" />
</xsl:function>
<!-- Override default -->
<xsl:template match="text()" />
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment