-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathV4-CSDL-normalize-Target.xsl
More file actions
204 lines (192 loc) · 8.14 KB
/
Copy pathV4-CSDL-normalize-Target.xsl
File metadata and controls
204 lines (192 loc) · 8.14 KB
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
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" xmlns:edm="http://docs.oasis-open.org/odata/ns/edm" xmlns:json="http://json.org/" xmlns:nodeinfo="xalan://org.apache.xalan.lib.NodeInfo">
<!--
This style sheet normalizes Target attribute values to alias-qualified names
Latest version: https://github.com/oasis-tcs/odata-vocabularies/blob/main/tools/V4-normalize-Target.xsl
-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="edm:Annotations/@Target">
<xsl:attribute name="Target">
<xsl:call-template name="normalizedPath">
<xsl:with-param name="path" select="." />
</xsl:call-template>
</xsl:attribute>
</xsl:template>
<xsl:template match="edm:Schema|edmx:Include">
<xsl:copy>
<!-- invent alias for schemas defined or included without alias
<xsl:if test="not(@Alias)">
<xsl:attribute name="Alias">
<xsl:apply-templates select="." mode="alias" />
</xsl:attribute>
</xsl:if>
-->
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="edm:Schema" mode="alias">
<xsl:text>self</xsl:text>
<xsl:variable name="position" select="count(preceding-sibling::edm:Schema)" />
<xsl:if test="$position>0">
<xsl:value-of select="$position" />
</xsl:if>
</xsl:template>
<xsl:template match="edmx:Include" mode="alias">
<xsl:choose>
<xsl:when test="starts-with(@Namespace,'Org.OData.')">
<xsl:call-template name="substring-before-last">
<xsl:with-param name="input" select="substring(@Namespace,11)" />
<xsl:with-param name="marker" select="'.'" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:text>R</xsl:text>
<xsl:value-of select="count(../preceding-sibling::edmx:Reference)" />
<xsl:text>I</xsl:text>
<xsl:value-of select="count(preceding-sibling::edmx:Include)" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- helper functions -->
<xsl:template name="normalizedPath">
<xsl:param name="path" />
<xsl:choose>
<xsl:when test="contains($path,'(')">
<!-- action/function overload: normalize action/function name -->
<xsl:call-template name="normalizedPath">
<xsl:with-param name="path" select="substring-before($path,'(')" />
</xsl:call-template>
<!-- normalize parameters -->
<xsl:text>(</xsl:text>
<xsl:call-template name="normalizedPath">
<xsl:with-param name="path" select="substring-after(substring-before($path,')'),'(')" />
</xsl:call-template>
<xsl:text>)</xsl:text>
<!-- normalize remaining path -->
<xsl:call-template name="normalizedPath">
<xsl:with-param name="path" select="substring-after($path,')')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($path,',')">
<!-- action/function overload parameter list: normalize first and remaining -->
<xsl:call-template name="normalizedPath">
<xsl:with-param name="path" select="substring-before($path,',')" />
</xsl:call-template>
<xsl:text>,</xsl:text>
<xsl:call-template name="normalizedPath">
<xsl:with-param name="path" select="substring-after($path,',')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($path,'/')">
<!-- multiple path segments: normalize first segment and remaining path -->
<xsl:call-template name="normalizedPath">
<xsl:with-param name="path" select="substring-before($path,'/')" />
</xsl:call-template>
<xsl:text>/</xsl:text>
<xsl:call-template name="normalizedPath">
<xsl:with-param name="path" select="substring-after($path,'/')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($path,'@')">
<!-- term-cast segment or annotated property: normalize term name -->
<xsl:value-of select="substring-before($path,'@')" />
<xsl:text>@</xsl:text>
<xsl:call-template name="normalizedQualifiedName">
<xsl:with-param name="qualifiedName" select="substring-after($path,'@')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($path,'.')">
<!-- qualified segment: normalize -->
<xsl:call-template name="normalizedQualifiedName">
<xsl:with-param name="qualifiedName" select="$path" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- unqualified segment, e.g. property: copy -->
<xsl:value-of select="$path" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="normalizedQualifiedName">
<xsl:param name="qualifiedName" />
<xsl:variable name="qualifier">
<xsl:call-template name="substring-before-last">
<xsl:with-param name="input" select="$qualifiedName" />
<xsl:with-param name="marker" select="'.'" />
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$qualifier='Edm' or $qualifier='odata'">
<xsl:value-of select="$qualifier" />
</xsl:when>
<xsl:when test="//edm:Schema[@Alias=$qualifier]">
<xsl:value-of select="$qualifier" />
</xsl:when>
<xsl:when test="//edmx:Include[@Alias=$qualifier]">
<xsl:value-of select="$qualifier" />
</xsl:when>
<xsl:when test="//edm:Schema[@Namespace=$qualifier and @Alias]">
<xsl:value-of select="//edm:Schema[@Namespace=$qualifier]/@Alias" />
</xsl:when>
<xsl:when test="//edmx:Include[@Namespace=$qualifier and @Alias]">
<xsl:value-of select="//edmx:Include[@Namespace=$qualifier]/@Alias" />
</xsl:when>
<xsl:when test="//edm:Schema[@Namespace=$qualifier]">
<!--
<xsl:apply-templates select="//edm:Schema[@Namespace=$qualifier]" mode="alias" />
-->
<xsl:value-of select="$qualifier" />
</xsl:when>
<xsl:when test="//edmx:Include[@Namespace=$qualifier]">
<xsl:value-of select="$qualifier" />
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Unknown name qualifier </xsl:text>
<xsl:value-of select="$qualifier" />
<xsl:text> in line </xsl:text>
<xsl:value-of select="nodeinfo:lineNumber()" />
</xsl:message>
<xsl:value-of select="$qualifier" />
</xsl:otherwise>
</xsl:choose>
<xsl:text>.</xsl:text>
<xsl:call-template name="substring-after-last">
<xsl:with-param name="input" select="$qualifiedName" />
<xsl:with-param name="marker" select="'.'" />
</xsl:call-template>
</xsl:template>
<xsl:template name="substring-before-last">
<xsl:param name="input" />
<xsl:param name="marker" />
<xsl:if test="contains($input,$marker)">
<xsl:value-of select="substring-before($input,$marker)" />
<xsl:if test="contains(substring-after($input,$marker),$marker)">
<xsl:value-of select="$marker" />
<xsl:call-template name="substring-before-last">
<xsl:with-param name="input" select="substring-after($input,$marker)" />
<xsl:with-param name="marker" select="$marker" />
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template name="substring-after-last">
<xsl:param name="input" />
<xsl:param name="marker" />
<xsl:choose>
<xsl:when test="contains($input,$marker)">
<xsl:call-template name="substring-after-last">
<xsl:with-param name="input" select="substring-after($input,$marker)" />
<xsl:with-param name="marker" select="$marker" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$input" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>