-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiati-json.xsl
229 lines (209 loc) · 11.4 KB
/
iati-json.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
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dyn="http://exslt.org/dynamic"
xmlns:math="http://exslt.org/math"
extension-element-prefixes="dyn math">
<xsl:output method="text" encoding="application/json"/>
<xsl:key name="reporting-orgs" match="reporting-org|participating-org" use="@ref"/>
<xsl:key name="policy-marker" match="policy-marker" use="."/>
<xsl:key name="years" match="transaction/value/@value-date" use="substring(.,0,5)"/>
<xsl:key name="sector" match="sector" use="@code"/>
<xsl:template match="/">
<xsl:text>{
"properties": {
"participating-org" : {
"valueType": "item",
"label": "Participating Organisation",
"pluralLabel": "Participating Organisations"
},
"reporting-org": {
"valueType": "item",
"label" : "Reporting organisation"
},
"components": {
"valueType": "item",
"label": "Project Components"
},
"parents": {
"valueType": "item",
"label": "Parent Project"
},
"siblings": {
"valueType": "item",
"label": "Related Components/Projects"
},
"policy-marker": {
"valueType": "item",
"label": "Policy markers"
},
"sector": {
"valueType": "item",
"label": "Sector"
},
"start-date": {
"valueType": "date",
"label": "Start Date"
},
"end-date": {
"valueType": "date",
"label": "End Date"
},
"type": {
"valueType": "item",
"label": "Type"
},
"recipient-country-code": {
"valueType":"item",
"label": "Country Code"
},
"total-committments": {
"valueType":"currency",
"label": "Total of budget committments"
},
"recipient-country": {
"label": "Recieving country"
},
"activity-status": {
"label": "Activity status"
},
"collaboration-type": {
"label": "Collaboration type"
},
"default-aid-type": {
"label": "Aid Type"
},
"default-flow-type": {
"label": "Aid Flow Type"
}
},
"items":
[ {"id": "type-1", "label":"Project" }, {"id":"type-2","label":"Component" }, </xsl:text>
<xsl:for-each select="/iati-activities/iati-activity">
<xsl:text>{</xsl:text> "type": "activity",
"id": "<xsl:value-of select="iati-identifier"/>", <xsl:variable name="id" select="iati-identifier"/>
"project-id": "<xsl:value-of select="substring(iati-identifier,6,6)"/>", <!--DFID Specific-->
"uri": "<xsl:call-template name="urlPattern"> <xsl:with-param name="org" select="reporting-org/@ref"/><xsl:with-param name="id" select="iati-identifier"/> </xsl:call-template>",
"label": "<xsl:value-of select="title"/>",
"type": "<xsl:value-of select="concat('type-',@hierarchy)"/>",
"description": "<xsl:call-template name="fetchValue"> <xsl:with-param name="value" select="'description'"/>
<xsl:with-param name="parent" select="related-activity[@type=1]"/>
<xsl:with-param name="children" select="related-activity[@type=2]"/> </xsl:call-template>",
"start-date": "<xsl:value-of select="translate(activity-date[@type='start-actual'],'Z','')"/>", <!--NB. Need to handle for when start actual doesn't exist....-->
"end-date": "<xsl:value-of select="translate(activity-date[@type='end-actual'],'Z','')"/>",
"activity-status": "<xsl:value-of select="activity-status"/>", <!--Codelist available-->
"collaboration-type": "<xsl:value-of select="collaboration-type"/>", <!--Codelist available-->
"default-flow-type": "<xsl:value-of select="default-flow-type"/>", <!--Codelist available-->
"default-aid-type": [ <xsl:call-template name="join"> <xsl:with-param name="valueList" select="default-aid-type"/> </xsl:call-template> ],
"reporting-org": "<xsl:value-of select="reporting-org/@ref"/>",
"participating-org": [ <xsl:call-template name="join"> <xsl:with-param name="valueList" select="participating-org/@ref"/> </xsl:call-template> ],
"currency" : "<xsl:value-of select="@default-currency"/>",
"total-committments": <xsl:value-of select="sum(transaction[transaction-type/@code='C']/value)"/>,
"transaction-count": <xsl:value-of select="count(transaction[not(transaction-type/@code='C')])"/>,
"policy-marker": [ <xsl:call-template name="joinPolicy"> <xsl:with-param name="valueList" select="policy-marker[@significance > 0]"/> </xsl:call-template> ],
"sector": [ <xsl:call-template name="join"> <xsl:with-param name="valueList" select="sector/@code"/> </xsl:call-template> ],
"sector-amounts": [ <xsl:call-template name="join"> <xsl:with-param name="valueList" select="sector/@percentage"/> </xsl:call-template> ],
"components": [ <xsl:call-template name="join"> <xsl:with-param name="valueList" select="related-activity[@type=2]/@ref"/> </xsl:call-template> ],
"parents": [ <xsl:call-template name="join"> <xsl:with-param name="valueList" select="related-activity[@type=1]/@ref"/> </xsl:call-template> ],
"recipient-country": "<xsl:call-template name="fetchValue"> <xsl:with-param name="value" select="'recipient-country'"/>
<xsl:with-param name="parent" select="related-activity[@type=1]"/>
<xsl:with-param name="children" select="related-activity[@type=2]"/> </xsl:call-template>",
"recipient-country-code": "<xsl:call-template name="fetchValue"> <xsl:with-param name="value" select="'recipient-country/@code'"/>
<xsl:with-param name="parent" select="related-activity[@type=1]"/>
<xsl:with-param name="children" select="related-activity[@type=2]"/> </xsl:call-template>",
"latlng": "<xsl:value-of select="geocoded-latlng"/>",
"geotype": "<xsl:value-of select="geocoded-type"/>",
"timeline": <xsl:variable name="max" select="math:max(transaction[transaction-type/@code='C']/value)"/>"&chbh=r,0.5,1.5&chd=t:<xsl:for-each select="//transaction/value/@value-date[generate-id() = generate-id(key('years',substring(.,0,5))[1])]"><xsl:sort select="substring(.,0,5)"/><xsl:variable name="year" select="substring(.,0,5)"/><xsl:value-of select="dyn:evaluate(sum(//iati-activity[iati-identifier = $id]/transaction[transaction-type/@code='C']/value[substring(@value-date,0,5)=$year]))"/>,</xsl:for-each>0&chds=0,<xsl:value-of select="$max"/>&null=100&chxt=x&chxl=0:|<xsl:for-each select="//transaction/value/@value-date[generate-id() = generate-id(key('years',substring(.,0,5))[1])]"><xsl:sort select="substring(.,0,5)"/><xsl:value-of select="substring(.,3,2)"/>|</xsl:for-each>&chdlp=t&chdl=Budget of <xsl:value-of select="dyn:evaluate(sum(//iati-activity[iati-identifier = $id]/transaction[transaction-type/@code='C']/value))"/> committed"
<xsl:text>},
</xsl:text>
</xsl:for-each>
<!--Pull out each unique reporting or partner organisation and create data for them-->
<xsl:for-each select="//reporting-org[generate-id() = generate-id(key('reporting-orgs',@ref)[1])]|//participating-org[generate-id() = generate-id(key('reporting-orgs',@ref)[1])]">
<xsl:text>{</xsl:text> "type": "organisation",
"id": "<xsl:value-of select="@ref"/>",
"label": "<xsl:value-of select="."/>"
<xsl:if test="position() != last()"><xsl:text>},</xsl:text></xsl:if><xsl:if test="position() = last()"><xsl:text>}</xsl:text></xsl:if>
</xsl:for-each>
<!--Pull out each unique policy marker and create items for them-->
<xsl:for-each select="//policy-marker[generate-id() = generate-id(key('policy-marker',.)[1])]">
<xsl:if test="position() = 1">,</xsl:if>
<xsl:text>{</xsl:text> "type": "policy-marker",
"id": "<xsl:value-of select="concat(@vocabulary,'-',@code)"/>",
"label": "<xsl:value-of select="."/>",
"vocabulary": "<xsl:value-of select="@vocabulary"/>"
<xsl:if test="position() != last()"><xsl:text>},</xsl:text></xsl:if><xsl:if test="position() = last()"><xsl:text>}</xsl:text></xsl:if>
</xsl:for-each>
<!--Pull out each unique policy marker and create items for them-->
<xsl:for-each select="//sector[generate-id() = generate-id(key('sector',@code)[1])]">
<xsl:if test="position() = 1">,</xsl:if>
<xsl:text>{</xsl:text> "type": "sector",
"id": "<xsl:value-of select="@code"/>",
"label": "<xsl:value-of select="."/>"
<xsl:if test="position() != last()"><xsl:text>},</xsl:text></xsl:if><xsl:if test="position() = last()"><xsl:text>}</xsl:text></xsl:if>
</xsl:for-each>
<xsl:text>]}</xsl:text>
</xsl:template>
<xsl:template name="missingCountry">
<xsl:param name="childProject" select="''"/>
<xsl:if test="//iati-activities/iati-activity[iati-identifier]/recipient-country">"recipient-country": "<xsl:value-of select="//iati-activities/iati-activity[iati-identifier]/recipient-country"/>",
"recipient-country-code": "<xsl:value-of select="//iati-activities/iati-activity[iati-identifier]/recipient-country/@code"/>",</xsl:if>
<xsl:if test="not(//iati-activities/iati-activity[iati-identifier]/recipient-country)">
"recipient-country":"Not given", "recipient-country-code":"0",</xsl:if>
</xsl:template>
<xsl:template name="fetchValue"> <!-- Fetch value uses the dyn:evaluate function to look for values from related projects (parent or child) where they are missing. -->
<xsl:param name="value" select="''"/>
<xsl:param name="parent" select="''"/>
<xsl:param name="children" select="''"/>
<xsl:if test="$value">
<xsl:value-of select="translate(dyn:evaluate($value),'"','-')"/>
</xsl:if>
<xsl:if test="not(dyn:evaluate($value))"><!--Not found the value we want. Check for a child or parent...-->
<xsl:variable name="variable" select="$value"/>
<xsl:if test="$parent">
<xsl:variable name="identifier" select="$parent/@ref"/>
<xsl:value-of select="normalize-space(translate(dyn:evaluate(concat('//iati-activities/iati-activity[iati-identifier = $identifier]/',$value)),'"','-'))"/>
</xsl:if>
<xsl:if test="$children">
<xsl:variable name="identifier" select="$children/@ref"/>
<xsl:value-of select="normalize-space(translate(dyn:evaluate(concat('//iati-activities/iati-activity[iati-identifier = $identifier]/',$value)),'"','-'))"/>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template name="urlPattern"><!--Designed to be extended to provide authoritive URLs for different IATI Providing organisations-->
<xsl:param name="org" select="''"/>
<xsl:param name="id" select="''"/>
<xsl:choose>
<xsl:when test="$org = 'GB-1'">
<xsl:value-of select="concat('http://projects.dfid.gov.uk/project.aspx?Project=',substring($id,6,6))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('#',$id)" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="join" >
<xsl:param name="valueList" select="''"/>
<xsl:for-each select="$valueList">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:value-of select="concat('"',.,'"')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(',','"',.,'"') "/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template name="joinPolicy" > <!--Customised function to include policy marker vocabularies alongside markers themselves -->
<xsl:param name="valueList" select="''"/>
<xsl:for-each select="$valueList">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:value-of select="concat('"',@vocabulary,'-',@code,'"')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(',','"',@vocabulary,'-',@code,'"') "/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>