normalize_record: missing else
[pazpar2-moved-to-github.git] / etc / MarcXML2TurboMarc.xsl
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!--
3     Converts MarcXML to TurboMarc
4     (to benefit from pazpar2's improved tmarc.xsl)
5
6     Also includes a provision to handle PicaMarc where datafield names can
7     contain a @ that is changed to Ä to give valid XML.
8  
9     2010-2011
10     Sven-S. Porst, SUB Göttingen <porst@sub.uni-goettingen.de>
11 -->
12 <xsl:stylesheet
13    version="1.0"
14    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
15    xmlns:tmarc="http://www.indexdata.com/turbomarc"
16    xmlns:marc="http://www.loc.gov/MARC21/slim">
17
18 <xsl:output indent="yes" method="xml" version="1.0" encoding="UTF-8"/>
19
20 <xsl:template match="@*|node()">
21   <xsl:copy>
22     <xsl:apply-templates select="@*|node()"/>
23     </xsl:copy>
24 </xsl:template>
25
26
27 <xsl:template match="record|marc:record">
28   <xsl:element name="r" namespace="http://www.indexdata.com/turbomarc">
29     <xsl:apply-templates select="@*|node()"/>
30     </xsl:element>
31 </xsl:template>
32
33
34 <xsl:template match="leader|marc:leader">
35   <xsl:element name="l" namespace="http://www.indexdata.com/turbomarc">
36     <xsl:apply-templates select="@*|node()"/>
37     </xsl:element>
38 </xsl:template>
39
40
41 <xsl:template match="controlfield|datafield|subfield|marc:controlfield|marc:datafield|marc:subfield">
42   <!--
43       Try to mock Indexdata's specification without regexps:
44       Translate all allowed characters to 'a' and assume field names are
45       shorter than 62 characters.
46       Given the typical 3 digit Marc field numbers this seems
47       safe in the practical cases I have seen.
48
49       http://www.indexdata.com/blog/2010/05/turbomarc-faster-xml-marc-records
50       http://www.indexdata.com/yaz/doc/marc.html
51       -->
52
53   <xsl:variable name="allowedCharacters" select="'0123465789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@'"/>
54   <xsl:variable name="manyAs" select="'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'"/>
55
56   <xsl:choose>
57     <xsl:when test="(name(.)='datafield' or name(.)='controlfield') and
58                     contains($manyAs, translate(@tag, $allowedCharacters, $manyAs))">
59       <xsl:element name="{concat(substring(local-name(),1,1), translate(@tag,'@','Ä'))}"
60                    namespace="http://www.indexdata.com/turbomarc">
61         <xsl:apply-templates select="@*[name(.)!='tag']|node()"/>
62         </xsl:element>
63       </xsl:when>
64
65     <xsl:when test="name(.)='subfield' and
66                     contains($manyAs, translate(@code, $allowedCharacters, $manyAs))">
67       <xsl:element name="{concat(substring(local-name(),1,1), @code)}"
68                    namespace="http://www.indexdata.com/turbomarc">
69         <xsl:apply-templates select="@*[name(.)!='code']|node()"/>
70         </xsl:element>
71       </xsl:when>
72
73     <xsl:otherwise>
74       <xsl:copy>
75         <xsl:apply-templates select="@*|node()"/>
76         </xsl:copy>
77       </xsl:otherwise>
78     </xsl:choose>
79 </xsl:template>
80
81
82 </xsl:stylesheet>