]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xsl/locDoc2xml.xsl
adding copyright and docs
[Evergreen.git] / Open-ILS / xsl / locDoc2xml.xsl
1 <!--
2
3 Copyright (C) 2008 Equinox Software, Inc.
4 Mike Rylander <miker@esilibrary.com>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
19
20 -->
21
22
23 <!--
24
25 This XSLT will take the MARC21 Concise Format for Bibliographic Data
26 documentation maintained by the Library of Congress and turn it into an XML
27 document for use by Evergreen (and others, if you so desire, under the terms
28 of the GPL).
29
30 The format of the output XML is similar to that produced sometime during
31 2005 by Ed Summers and shared at textualize.com.  That site was not available
32 at the time of this XSLT's creation, and thus this exists.
33
34 Please report any problems to Mike Rylander at <miker@esilibrary.com>.
35
36 Here's an easy way to use this:
37
38 curl -o - http://www.loc.gov/marc/bibliographic/ecbd{ldrd,cntr,007s,008s,numb,clas,main,tils,impr,phys,sers,not1,not2,subj,adde,link,srae,hold}.html | \
39  tidy -asxml -n -q -utf8 | \
40  xsltproc &hyphen;&hyphen;html &hyphen;&hyphen;novalid locDoc2xml.xsl - | \
41  xmllint &hyphen;&hyphen;format &hyphen;&hyphen;noblanks - > marcedit-tooltips.xml
42
43 -->
44
45
46 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
47     <xsl:output omit-xml-declaration="yes" method="xml" encoding="UTF-8" media-type="text/plain" />
48
49     <xsl:template match="/">
50     <fields>
51         <xsl:for-each select="//a[substring(@href,1,5)='#mrcb']">
52             <xsl:call-template name="field">
53                 <xsl:with-param name="datafieldLabel" select="substring-after(@href,'#')"/>
54             </xsl:call-template>
55         </xsl:for-each>
56     </fields>
57     </xsl:template>
58
59     <xsl:template name="field">
60         <xsl:param name="datafieldLabel"/>
61         <xsl:variable name="locatorAnchor" select="//a[@name=$datafieldLabel]"/>
62
63         <xsl:variable name="tagValue" select="substring-before($locatorAnchor, ' - ')"/>
64         <xsl:if test="$tagValue != ''">
65
66             <xsl:variable name="nameStart" select="substring-after($locatorAnchor, ' - ')"/>
67
68             <xsl:variable name="nameValue" select="substring-before($nameStart, '(')"/>
69
70             <xsl:variable name="repeatable" select="substring-after($nameStart, '(')"/>
71             <xsl:variable name="description" select="$locatorAnchor/parent::node()/following-sibling::node()/descendant-or-self::*"/>
72
73             <field>
74                 <xsl:attribute name="tag">
75                     <xsl:value-of select="$tagValue"/>
76                 </xsl:attribute>
77                 <xsl:attribute name="repeatable">
78                     <xsl:choose>
79                         <xsl:when test="substring($repeatable,1,1)='R'">
80                             <xsl:text>true</xsl:text>
81                         </xsl:when>
82                         <xsl:otherwise>
83                             <xsl:text>false</xsl:text>
84                         </xsl:otherwise>
85                     </xsl:choose>
86                 </xsl:attribute>
87
88                 <name>
89                     <xsl:value-of select="normalize-space($nameValue)"/>
90                 </name>
91
92                 <description>
93                     <xsl:value-of select="normalize-space($description)"/>
94                 </description>
95
96                 <xsl:call-template name="indicators">
97                     <xsl:with-param name="indUL" select="$locatorAnchor/parent::node()/following-sibling::h3[.='Indicators'][1]/following-sibling::ul[1]"/>
98                 </xsl:call-template>
99
100                 <xsl:if test="substring($tagValue,1,2) != '00'">
101                     <xsl:call-template name="subfields">
102                         <xsl:with-param name="sfUL" select="$locatorAnchor/parent::node()/following-sibling::h3[.='Subfield Codes'][1]/following-sibling::ul[1]"/>
103                     </xsl:call-template>
104                 </xsl:if>
105
106             </field>
107
108         </xsl:if>
109     </xsl:template>
110
111     <xsl:template name="indicators">
112         <xsl:param name="indUL"/>
113         <xsl:for-each select="$indUL/li">
114             <xsl:if test="string-length(substring-after(.,' - Undefi')) = 0">
115
116                 <xsl:variable name="indPos">
117                     <xsl:choose>
118                         <xsl:when test="starts-with(.,'First')">
119                             <xsl:text>1</xsl:text>
120                         </xsl:when>
121                         <xsl:otherwise>
122                             <xsl:text>2</xsl:text>
123                         </xsl:otherwise>
124                     </xsl:choose>
125                 </xsl:variable>
126
127                 <xsl:for-each select="./ul/li">
128                     <indicator>
129                         <xsl:attribute name="position">
130                             <xsl:value-of select="$indPos"/>
131                         </xsl:attribute>
132
133                         <xsl:attribute name="value">
134                             <xsl:value-of select="substring-before(.,' - ')"/>
135                         </xsl:attribute>
136
137                         <description>
138                             <xsl:value-of select="normalize-space(substring-after(.,' - '))"/>
139                         </description>
140                     </indicator>
141                 </xsl:for-each>
142             </xsl:if>
143         </xsl:for-each>
144     </xsl:template>
145
146     <xsl:template name="subfields">
147         <xsl:param name="sfUL"/>
148         <xsl:for-each select="$sfUL/li">
149             <xsl:variable name="sfCode" select="substring-before(., ' - ')"/>
150             <xsl:variable name="descStart" select="substring-after(., ' - ')"/>
151             <xsl:variable name="descValue" select="substring-before($descStart, '(')"/>
152             <xsl:variable name="sfRepeatable" select="substring-after(., '(')"/>
153
154             <subfield>
155                 <xsl:attribute name="code">
156                     <xsl:value-of select="substring($sfCode,2,1)"/>
157                 </xsl:attribute>
158
159                 <xsl:attribute name="repeatable">
160                     <xsl:choose>
161                         <xsl:when test="substring($sfRepeatable,1,1)='R'">
162                             <xsl:text>true</xsl:text>
163                         </xsl:when>
164                         <xsl:otherwise>
165                             <xsl:text>false</xsl:text>
166                         </xsl:otherwise>
167                     </xsl:choose>
168                 </xsl:attribute>
169
170                 <description>
171                     <xsl:value-of select="normalize-space($descValue)"/>
172                 </description>
173             </subfield>
174         </xsl:for-each>
175     </xsl:template>
176
177 </xsl:stylesheet>
178