forked from carakey/xml2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_maker.xsl
141 lines (126 loc) · 6.07 KB
/
csv_maker.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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs" version="2.0"
>
<xsl:output method="text"/>
<xsl:param name="headerFile">
<!--<xsl:text>sample_data/sample_fields.xml</xsl:text>-->
<xsl:text>lsu-sc-msw_fields.xml</xsl:text>
</xsl:param>
<xsl:variable name="fieldList" select="document($headerFile)"/>
<!-- Sets the field separator with a variable;
if necessary, it can be reset throughout -->
<xsl:variable name="delimiter" select="'	'"/>
<xsl:template match="/">
<xsl:call-template name="headerRow"/>
<xsl:apply-templates select="modsCollection/mods"/>
</xsl:template>
<!-- to do:
* add comments
* not getting dmGetItemInfo (deliberately, for now, but need to address)
* subject strings - delimit sibling topics with dashes and separate subjects with semicolons
-->
<!-- Begin header row -->
<!-- Uses the fieldNames from the field_list.xsl output as column headers -->
<xsl:template name="headerRow">
<xsl:for-each select="$fieldList//field">
<xsl:value-of select="fieldName"/>
<!-- Inserts a delimiter after each header until the last, then inserts a new line -->
<xsl:choose>
<xsl:when test="position()!=last()">
<xsl:value-of select="$delimiter"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<!-- End header row -->
<!-- Begin building record rows -->
<xsl:template match="mods">
<!-- Matches each MODS record -->
<xsl:variable name="record" select="."/>
<!-- Stores the entire MODS record as a variable -->
<xsl:for-each select="$fieldList//field">
<xsl:variable name="header" select="fieldName"/>
<xsl:variable name="labelMatch">
<xsl:variable name="labelValue" select="substring-before($header, ' ::')"/>
<xsl:variable name="terminus" select="replace($header, '^.*:: ', '')"/>
<xsl:variable name="terminusName" select="substring-before($terminus, ' @')"/>
<xsl:variable name="attName"
select="substring-before(substring-after($terminus, ' @'), '=')"/>
<xsl:variable name="attValue"
select="substring-before(substring-after(substring-after($terminus, $attName), '="'), '"')"/>
<xsl:choose>
<xsl:when test="contains($header, '::')">
<xsl:choose>
<xsl:when test="contains($header, '@')">
<xsl:for-each
select="$record//*[@displayLabel = $labelValue]//*[name() = $terminusName][@*[name() = $attName] = $attValue]">
<xsl:call-template name="cell"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each
select="$record//*[@displayLabel = $labelValue]//*[name() = $terminus]">
<xsl:call-template name="cell"/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="$record//*[@displayLabel = $header]">
<xsl:call-template name="cell"/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="elementMatch">
<xsl:choose>
<xsl:when test="contains($header, '@')">
<xsl:variable name="elementName" select="substring-before($header, ' @')"/>
<xsl:variable name="attName"
select="substring-before(substring-after($header, ' @'), '=')"/>
<xsl:variable name="attValue"
select="substring-before(substring-after(substring-after($header, $attName), '="'), '"')"/>
<xsl:for-each
select="$record//*[name() = $elementName][@*[name() = $attName] = $attValue][not(@displayLabel)]">
<xsl:call-template name="cell"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="$record//*[name() = $header][not(@displayLabel)]">
<xsl:call-template name="cell"/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="not(contains(xpath, 'displayLabel'))">
<xsl:value-of select="$elementMatch"/>
</xsl:when>
<xsl:when test="$labelMatch">
<xsl:for-each select="$labelMatch">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="position() != last()">
<xsl:value-of select="$delimiter"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template name="cell">
<xsl:value-of select="normalize-space(.)"/>
<xsl:if test="position()!=last()">
<xsl:text>||</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>