-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconvert.xsl
executable file
·306 lines (268 loc) · 12 KB
/
convert.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="cals_table.xsl"/>
<xsl:output
method="xml"
omit-xml-declaration="yes"
indent="no"/>
<xsl:template match="patents">
{
<!-- just grab the patent files, don't try and index the genetic sequences or other objects. -->
<xsl:for-each select="us-patent-grant">
<xsl:apply-templates select="."/>
<xsl:if test="not(position() = last())">,</xsl:if>
</xsl:for-each>
}
</xsl:template>
<xsl:template match="us-patent-grant">
"add": { "doc": {
"document_type":"<xsl:value-of select="@id" />",
<xsl:apply-templates select="@date-publ"/>,
<xsl:apply-templates select="@date-produced"/>,
<xsl:for-each select="us-bibliographic-data-grant|abstract|description|claims">
<xsl:apply-templates select="." />
<xsl:if test="not(position() = last())">,</xsl:if>
</xsl:for-each>
}}
</xsl:template>
<xsl:template match="@date-publ|@date-produced">
<xsl:variable name="date_y" select="substring(.,1,4)" />
<xsl:variable name="date_m" select="substring(.,5,2)" />
<xsl:variable name="date_d" select="substring(.,7,2)" />
<xsl:variable name="name"><xsl:value-of select='translate(name(), "-", "_")'/></xsl:variable>
"<xsl:value-of select='$name'/>":"<xsl:value-of select="$date_y"/>-<xsl:value-of select="$date_m"/>-<xsl:value-of select="$date_d"/>T00:00:01Z",
"<xsl:value-of select='$name'/>_s":"<xsl:value-of select="$date_y"/>-<xsl:value-of select="$date_m"/>-<xsl:value-of select="$date_d"/>",
"<xsl:value-of select='$name'/>_i":"<xsl:value-of select="$date_y"/><xsl:value-of select="$date_m"/><xsl:value-of select="$date_d"/>",
"<xsl:value-of select='$name'/>_facet":["0/<xsl:value-of select="$date_y"/>","1/<xsl:value-of select="$date_y"/>/<xsl:value-of select="$date_m"/>"]
</xsl:template>
<!-- CPC facet lookup, by CPC facet id. -->
<xsl:key name="cpc-facet-lookup" match="classification" use="@id"/>
<xsl:variable name="cpc-facet-top" select="document('cpc_facets.xml')/cpc"/>
<xsl:template match="cpc" mode="cpc">
<xsl:param name="cpc_id"/>
<xsl:for-each select="key('cpc-facet-lookup', $cpc_id)/term">
"<xsl:value-of select="."/>"<xsl:if test="not(position() = last())">,</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- CPC Classification Lookup by patent id -->
<xsl:key name="cpc-lookup" match="patent" use="@id"/>
<xsl:variable name="cpc-top" select="document('cpc_2005_2010.xml')/patents"/>
<xsl:template match="patents" mode="cpc">
<xsl:param name="patent_id"/>
"cpc_code":[
<xsl:for-each select="key('cpc-lookup', $patent_id)/code">
"<xsl:value-of select="."/>"<xsl:if test="not(position() = last())">,</xsl:if>
</xsl:for-each>
],
"cpc_facet":[
<xsl:for-each select="key('cpc-lookup', $patent_id)/code">
<xsl:variable name="cpc-facet-data">
<xsl:apply-templates select="$cpc-facet-top" mode="cpc"><xsl:with-param name="cpc_id" select="."/></xsl:apply-templates>
</xsl:variable>
<xsl:copy-of select="$cpc-facet-data"/>
<xsl:if test="$cpc-facet-data = ''">""</xsl:if>
<xsl:if test="not(position() = last())">,</xsl:if>
</xsl:for-each>
],
</xsl:template>
<xsl:template match="us-bibliographic-data-grant">
<xsl:variable name="doc_number" select="publication-reference/document-id/doc-number"/>
"id":"<xsl:value-of select="$doc_number"/>",
"doc_number":"<xsl:value-of select="$doc_number"/>",
<xsl:apply-templates select="invention-title" />,
"assignee_orgname_s":"<xsl:value-of select="assignees/assignee/addressbook/orgname" />",
<xsl:apply-templates select="classification-national/main-classification" />,
<xsl:apply-templates select="$cpc-top" mode="cpc"><xsl:with-param name="patent_id" select="$doc_number"/></xsl:apply-templates>
<xsl:for-each select="parties/applicants/applicant/addressbook">
<xsl:apply-templates select="." />
<xsl:if test="not(position() = last())">,</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- Category Lookup -->
<xsl:key name="cat-lookup" match="category" use="@id"/>
<xsl:variable name="cat-top" select="document('categories.xml')/categories"/>
<xsl:template match="categories">
<xsl:param name="catid"/>
<xsl:for-each select="key('cat-lookup', $catid)/term">
<xsl:apply-templates select="."/>
<xsl:if test="not(position() = last())">,</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="term">
"<xsl:value-of select="."/>"
</xsl:template>
<xsl:template match="main-classification">
<xsl:variable name="main_class_m" select='substring(concat(translate(substring(.,1,3), " ", "0"), "000"), 1, 3)' />
<xsl:variable name="main_class_s1" select='substring(concat("000", translate(substring(.,4,3), " ", "0")), string-length(substring(.,4,3)) + 1, 3)'/>
<xsl:variable name="main_class_s2" select='substring(concat("000", translate(substring(.,7,3), " ", "0")), string-length(substring(.,7,3)) + 1, 3)' />
<!-- If the category ends with a letter, forward pad the subclasses with 0, if it doesn't, just pad with 0's on the end -->
<xsl:variable name="catcode">
<xsl:choose>
<xsl:when test="matches(., '[A-Z]$')">
<xsl:value-of select='concat($main_class_m, $main_class_s1, $main_class_s2)'/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='substring(concat(translate(., " ", "0"), "000000000"), 1, 9)'/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- If you skill can't find the code, then fall back to just the main class -->
<xsl:variable name="check1">
<xsl:apply-templates select="$cat-top"><xsl:with-param name="catid" select="$catcode"/></xsl:apply-templates>
</xsl:variable>
<xsl:variable name="category_facet">
<xsl:choose>
<xsl:when test="$check1 != ''">
<xsl:value-of select='$check1'/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="catmain" select='substring(concat(translate(substring(.,1,3), " ", "0"), "000000000"), 1, 9)'/>
<xsl:apply-templates select="$cat-top"><xsl:with-param name="catid" select="$catmain"/></xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
"main_classification":"<xsl:value-of select="." />",
"main_classification_std":"<xsl:value-of select="$catcode"/>",
"category_facet":[<xsl:value-of select="$category_facet"/>]
</xsl:template>
<xsl:template match="further-classification">
"further_classification":"<xsl:value-of select="." />"
</xsl:template>
<xsl:template match="citation/patcit/document-id/doc-number">
"citation_s":"<xsl:value-of select="." />"
</xsl:template>
<xsl:template match="parties/applicants/applicant/addressbook">
"applicant_inventor":"<xsl:value-of select='concat(first-name," ",last-name)' />",
"applicant_inventor_facet":"<xsl:value-of select='concat(last-name,", ",first-name)' />",
"applicant_inventor_last_name":"<xsl:value-of select="last-name" />",
"applicant_inventor_first_name":"<xsl:value-of select="first-name" />"
</xsl:template>
<xsl:template match="invention-title|abstract|description">
"<xsl:value-of select='translate(name(), "-", "_")'/>_raw":
"<xsl:call-template name="escape"><xsl:with-param name="e" select="."/></xsl:call-template>",
"<xsl:value-of select='translate(name(), "-", "_")'/>_html":"<xsl:apply-templates/>"
</xsl:template>
<xsl:template match="claims">
"claims_raw":" <xsl:call-template name="escape"><xsl:with-param name="e" select="."/></xsl:call-template>",
"claims_html":"
<ol class='claims'>
<xsl:apply-templates/>
</ol>"
</xsl:template>
<xsl:template match="claim">
<xsl:variable name="num" select="number(@num)"/>
<li value='{$num}'>
<xsl:apply-templates />
</li>
</xsl:template>
<xsl:template match="claim/claim-text">
<!-- trim off a leading number, period and space from the beginning of the claim text"
so that "1. A bla bla " becomes just "A bla bla" -->
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="self::text()">
<!-- trim leading numbers off the claims text.-->
<xsl:if test="position() < 3">
<!--
<xsl:call-template name="trim-leading-numbers">
<xsl:with-param name="text" select="." />
</xsl:call-template>
-->
<xsl:value-of select="replace(., '^[0-9]{1,4}\.? ?', '')"/>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="claim-text">
<blockquote class='claim-text'>
<xsl:apply-templates />
</blockquote>
</xsl:template>
<!-- NOT USED IN US: best-mode, disclosure, background-art, technical-field, mode-for-invention -->
<xsl:template match="disclosure|sequence-list-text|technical-field|background-art|description-of-drawings|best-mode|mode-for-invention|industrial-applicability|tech-problem|tech-solution|advantageous-effects">
<xsl:variable name="class"><xsl:value-of select="name()"/></xsl:variable>
<div class='{$class}'>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="heading">
<xsl:variable name="el_name">h<xsl:value-of select="@level"/></xsl:variable>
<xsl:element name="{$el_name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="smallcaps">
<span class='smallcaps'>
<xsl:apply-templates/>
</span>
</xsl:template>
<xsl:template match="p | b | i | o | u | sup | sub | ol | ul | li | br | dl | table ">
<xsl:element name="{name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="escape"><xsl:with-param name="e" select="."/></xsl:call-template>
</xsl:template>
<!-- we must escape the \ character, if it occurs naturally in the next, because the json output considers
it an escape character, and will bomb if it occurs out of context when loading into solr, same is
true of a double quote. ) -->
<xsl:template name="escape">
<xsl:param name="e"/>
<!-- This the xslt2 version of the escape method -->
<xsl:variable name="txt" select="replace($e, '\\', '\\\\')"/>
<xsl:value-of select="replace($txt, '"', '\\"')"/>
<!-- This the xslt1 version of the escape method -->
<!--
<xsl:variable name="txt">
<xsl:call-template name="str:replace">
<xsl:with-param name="string" select="$e"/>
<xsl:with-param name="search">\</xsl:with-param>
<xsl:with-param name="replace">\\</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="str:replace">
<xsl:with-param name="string" select="$e" />
<xsl:with-param name="search">\</xsl:with-param>
<xsl:with-param name="replace">\\</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="escaped">
<xsl:call-template name="str:replace">
<xsl:with-param name="string" select="$txt" />
<xsl:with-param name="search">"</xsl:with-param>
<xsl:with-param name="replace">\"</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$escaped"/>
-->
</xsl:template>
<!-- trim off a leading number, period and space from the beginning of the claim text"
so that "1. A bla bla " becomes just "A bla bla" -->
<!-- Provides string replacement when using xsl1 -->
<xsl:template name="trim-leading-numbers">
<xsl:param name="text" />
<xsl:choose>
<xsl:when test="$text = ''">
<xsl:value-of select="$text" />
</xsl:when>
<xsl:when test="contains('0123456789. ', substring($text, 1, 1))">
<xsl:call-template name="trim-leading-numbers">
<xsl:with-param name="text" select="substring($text,2)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- TABLES (please see cals_table.xsl) -->
<xsl:template match="tables">
<div class="tables">
<xsl:apply-templates/>
</div>
</xsl:template>
</xsl:stylesheet>