forked from carakey/xml2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_list.xsl
96 lines (87 loc) · 4.13 KB
/
field_list.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
<?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">
<!--
This stylesheet is meant for use on the output of xpath_list.xsl.
It identifies the unique XPaths in the collection - that is, where both the elements and attributes are distinct.
For each unique path, the stylesheet:
1) creates a field name to use as the column header in the CSV; and
2) writes the mapping line to convert the updated data back to MODS XML.
-->
<xsl:output method="xml" indent="yes"/>
<!-- To Do:
* add comments to fieldName and mapping templates
-->
<!-- Creates a list of unique XPaths and stores it as a variable; omits certain migration-related data from MODS extension fields -->
<xsl:variable name="uniquePaths" select="distinct-values(xpathList/xpath[not(contains(.,'@timestamp'))][not(contains(.,'@source'))])"/>
<!-- Builds document structure -->
<xsl:template match="/">
<fieldList>
<!-- Gets a count of total fields -->
<fieldCount>
<xsl:value-of select="count($uniquePaths)"/>
</fieldCount>
<!-- For each unique XPath, copies the XPath adds a fieldName element and a mapping element -->
<xsl:for-each select="$uniquePaths">
<xsl:choose>
<xsl:when test="contains(., 'roleTerm')"/>
<xsl:otherwise>
<field>
<xpath>
<xsl:value-of select="."/>
</xpath>
<fieldName>
<xsl:call-template name="fieldName"/>
</fieldName>
<!--<mapping>
<xsl:call-template name="mapping"/>
</mapping>-->
</field>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</fieldList>
</xsl:template>
<xsl:template name="fieldName">
<xsl:variable name="terminus" select="replace(., '^.*/', '')"/>
<xsl:choose>
<xsl:when test="contains(., 'displayLabel')">
<xsl:variable name="labelValue"
select="substring-before(substring-after(., 'displayLabel="'), '"]')"/>
<xsl:value-of select="$labelValue"/>
<xsl:if test="contains(substring-after(., $labelValue), '/')">
<xsl:for-each
select="tokenize(substring-after(substring-after(., $labelValue), '/'[1]), '/')">
<xsl:text> :: </xsl:text>
<xsl:value-of select="replace(replace(., '\[', ' '), '\]', '')"/>
</xsl:for-each>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="replace(replace($terminus, '\[', ' '), '\]', '')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="mapping">
<xsl:variable name="modsTopLevel" select="replace(.,'/modsCollection/mods/','')"/>
<xsl:for-each select="tokenize($modsTopLevel,'/')">
<xsl:text><</xsl:text>
<xsl:value-of select="replace(replace(.,'\[',' '),'\]','')"/>
<xsl:text>></xsl:text>
</xsl:for-each>
<xsl:text>%value%</xsl:text>
<xsl:for-each select="tokenize($modsTopLevel,'/')">
<xsl:sort select="position()" order="descending"/>
<xsl:text></</xsl:text>
<xsl:choose>
<xsl:when test="contains(.,'[')">
<xsl:value-of select="substring-before(.,'[')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>></xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>