comparison xslt/model.xsl @ 10:19a8a71eaa54

added test-model.xsl template for testing purpose of model.xsl working on members expansion templates
author cin
date Mon, 12 Mar 2018 02:58:34 +0300
parents d3542662cf70
children
comparison
equal deleted inserted replaced
9:d3542662cf70 10:19a8a71eaa54
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <xsl:stylesheet version="1.0" 2 <xsl:stylesheet version="1.0"
3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://implab.org/schemas/data-model.v1.xsd" 3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://implab.org/schemas/data-model.v1.xsd"
4 xmlns:clr="http://implab.org/schemas/data-model/dotnet.v1.xsd"
5 xmlns:cs="http://implab.org/schemas/code-dom/csharp.v1.xsd" xmlns:sql="http://implab.org/schemas/data-model/sql.v1.xsd"
6 xmlns:t="http://implab.org/schemas/temp" xmlns:exsl="http://exslt.org/common" 4 xmlns:t="http://implab.org/schemas/temp" xmlns:exsl="http://exslt.org/common"
7 exclude-result-prefixes="clr m xsl exsl sql"> 5 exclude-result-prefixes="m xsl exsl">
8 6
9 <xsl:import href="preprocess.xsl" /> 7 <xsl:import href="preprocess.xsl" />
10 <xsl:include href="text-tools.xsl"/> 8 <xsl:include href="text-tools.xsl"/>
11 9
12 <xsl:output method="xml" indent="yes" /> 10 <xsl:output method="xml" indent="yes" />
13 11
14 <xsl:key name="type" match="m:package/m:*" use="@name" /> 12 <xsl:key name="type" match="m:package/m:*" use="@name" />
15 13
16 <xsl:template match="/"> 14 <xsl:template match="/">
17 <cs:document> 15 <xsl:apply-templates select="$module" mode="document" />
18 <xsl:apply-templates select="$module" mode="document" />
19 </cs:document>
20 </xsl:template> 16 </xsl:template>
21 17
22 <!-- code generation --> 18 <!-- code generation -->
23 19
24 <!-- disable default template --> 20 <!-- disable default template -->
172 to recursively traverse members expanding details like special 168 to recursively traverse members expanding details like special
173 properties generated by relations. 169 properties generated by relations.
174 170
175 expand-member-reference is special mode used to traverse inside 171 expand-member-reference is special mode used to traverse inside
176 members which are referenced from relations. By default this mode 172 members which are referenced from relations. By default this mode
177 does nothing. 173 creates deep copy of all elements.
178 174
179 Use this templates to produce intermediate model for further 175 Use this templates to produce intermediate model for further
180 processing by specific code-generators. 176 processing by specific code-generators.
181 177
182 @special - means that the member is a composite part and isn't 178 @special - means that the member is a composite part and isn't
190 </xsl:copy> 186 </xsl:copy>
191 </xsl:template> 187 </xsl:template>
192 188
193 189
194 <!-- short form of primaryKey{@name, @type} --> 190 <!-- short form of primaryKey{@name, @type} -->
195 <xsl:template match="m:primaryKey" mode="expand-member"> 191 <xsl:template match="m:primaryKey[not(m:member)]" mode="expand-member">
196 <xsl:variable name="pkName" select="concat(@declaringType, 'Pk')"/> 192 <xsl:variable name="pkName" select="concat(@declaringType, 'Pk')"/>
197 <xsl:variable name="propName" select="@name" /> 193 <xsl:variable name="propName" select="@name" />
198 194
199 <xsl:variable name="specialProperty"> 195 <xsl:variable name="specialProperty">
200 <m:property name="{$propName}" special="true"> 196 <m:property name="{$propName}" special="true">
205 </xsl:variable> 201 </xsl:variable>
206 202
207 <xsl:apply-templates select="exsl:node-set($specialProperty)" mode="expand-member"/> 203 <xsl:apply-templates select="exsl:node-set($specialProperty)" mode="expand-member"/>
208 <xsl:copy> 204 <xsl:copy>
209 <xsl:attribute name="name"><xsl:value-of select="$pkName"/></xsl:attribute> 205 <xsl:attribute name="name"><xsl:value-of select="$pkName"/></xsl:attribute>
206 <xsl:attribute name="declarigType"><xsl:value-of select="@declaringType"/></xsl:attribute>
210 <m:member name="{@name}" type="{@declaringType}"/> 207 <m:member name="{@name}" type="{@declaringType}"/>
211 </xsl:copy> 208 </xsl:copy>
212 </xsl:template> 209 </xsl:template>
213 210
214 <!-- long form of primaryKey{ member{@name}+ } --> 211 <!-- long form of primaryKey{ member{@name}+ } -->
215 <xsl:template match="m:primaryKey[m:member]" mode="expand-member"> 212 <!-- will be processed using default template -->
216 <xsl:apply-templates mode="expand-member"/>
217 </xsl:template>
218 213
219 <!-- short form of hasA --> 214 <!-- short form of hasA -->
220 <xsl:template match="m:hasA" mode="expand-member"> 215 <xsl:template match="m:hasA" mode="expand-member">
216 <xsl:variable name="thisPrefix" select="@name"/>
217
221 <xsl:variable name="foreignPk"> 218 <xsl:variable name="foreignPk">
222 <xsl:call-template name="getPrimaryKey"> 219 <xsl:call-template name="getPrimaryKey">
223 <xsl:with-param name="type" select="@type"/> 220 <xsl:with-param name="type" select="@type"/>
224 </xsl:call-template> 221 </xsl:call-template>
225 </xsl:variable> 222 </xsl:variable>
226 <xsl:variable name="expandedPk"> 223 <xsl:variable name="expandedPk">
227 <xsl:apply-templates select="exsl:node-set($foreignPk)" mode="expand-member"/> 224 <xsl:apply-templates select="exsl:node-set($foreignPk)" mode="expand-member"/>
228 </xsl:variable> 225 </xsl:variable>
229 <xsl:variable name="resolvedPk"> 226 <xsl:variable name="pkMembers">
230 <xsl:apply-templates select="exsl:node-set($expandedPk)/m:primaryKey" mode="resolve-members"/> 227 <xsl:apply-templates select="exsl:node-set($expandedPk)/*[not(@special)]" mode="resolve-members"/>
231 </xsl:variable> 228 </xsl:variable>
229 <t:fk>
230 <xsl:for-each select="exsl:node-set($pkMembers)/*">
231 <t:thisKey name="{$thisPrefix}{@name}" matches="{@name}">
232 <xsl:copy-of select="."/>
233 </t:thisKey>
234 </xsl:for-each>
235 </t:fk>
232 </xsl:template> 236 </xsl:template>
233 237
234 <!-- short form of hasA with matched keys specified --> 238 <!-- short form of hasA with matched keys specified -->
239 <!-- TODO -->
235 <xsl:template match="m:hasA[m:thisKey]"> 240 <xsl:template match="m:hasA[m:thisKey]">
236 <xsl:apply-templates select="m:thisKey" mode="generate-fk-properties"/> 241 <xsl:apply-templates select="m:thisKey" mode="generate-fk-properties"/>
237 <xsl:copy> 242 <xsl:copy>
238 <xsl:copy-of select="@*"/> 243 <xsl:copy-of select="@*"/>
239 <xsl:apply-templates mode="expand-member"/> 244 <xsl:apply-templates mode="expand-member"/>
240 </xsl:copy> 245 </xsl:copy>
241 </xsl:template> 246 </xsl:template>
242 247
243 <xsl:template match="m:hasA/m:thisKey" mode="expand-member"> 248 <xsl:template match="m:hasA/m:thisKey" mode="expand-member">
244 <m:member name="{@name}" matches="{@matches}"/> 249 <m:member name="{@name}" matches="{@matches}" type="{../@declaringType}"/>
245 </xsl:template> 250 </xsl:template>
246 251
247 <!-- long form of hasA --> 252 <!-- long form of hasA -->
248 <xsl:template match="m:hasA[m:member]" mode="expand-member"> 253 <xsl:template match="m:hasA[m:member]" mode="expand-member">
249 <xsl:apply-templates mode="expand-member"/> 254 <xsl:apply-templates mode="expand-member"/>
250 </xsl:template> 255 </xsl:template>
251 256
252 <!-- resolve expands --> 257 <!-- resolve-members -->
253 258 <!-- processes members and copies members to output -->
259 <!-- TODO: Possible recursion -->
254 <xsl:template match="m:member" mode="resolve-members"> 260 <xsl:template match="m:member" mode="resolve-members">
255 <xsl:variable name="$name" select="@name"/> 261 <xsl:variable name="name" select="@name"/>
256 <xsl:variable name="$declaringType" select="@declaringType" /> 262 <xsl:variable name="declaringType" select="@type" />
257
258 <xsl:choose> 263 <xsl:choose>
259 <xsl:when test="/*[@name=$name and @declaringType=$declaringType]"> 264 <xsl:when test="/*[@name=$name and @declaringType=$declaringType]">
265 <!-- when reference points to a locally defined member -->
266 <!-- locally defined members are already expanded -->
260 <xsl:apply-templates select="/*[@name=$name and @declaringType=$declaringType]" mode="resolve-members"/> 267 <xsl:apply-templates select="/*[@name=$name and @declaringType=$declaringType]" mode="resolve-members"/>
261 </xsl:when> 268 </xsl:when>
262 <xsl:otherwise> 269 <xsl:otherwise>
263 <xsl:variable name="fragment"> 270 <!-- otherwise try to use getMembers, then expand it and resolve members -->
271 <xsl:variable name="members">
264 <xsl:call-template name="getMembers"> 272 <xsl:call-template name="getMembers">
265 <xsl:with-param name="memberName" select="$name"/> 273 <xsl:with-param name="memberName" select="$name"/>
266 <xsl:with-param name="type" select="$declaringType"/> 274 <xsl:with-param name="type" select="$declaringType"/>
267 </xsl:call-template> 275 </xsl:call-template>
268 </xsl:variable> 276 </xsl:variable>
269 <xsl:apply-templates select="exsl:node-set($fragment)/*[not(@special)]" mode="resolve-members"/> 277 <!-- expand resolved members like 'hasA' and 'thisKey' -->
278 <xsl:variable name="expanded">
279 <xsl:apply-templates select="exsl:node-set($members)" mode="resolve-members"/>
280 </xsl:variable>
281 <!-- skip @special members when resolving -->
282 <xsl:apply-templates select="exsl:node-set($expanded)/*[not(@special)]" mode="resolve-members"/>
270 </xsl:otherwise> 283 </xsl:otherwise>
271 </xsl:choose> 284 </xsl:choose>
272 </xsl:template> 285 </xsl:template>
273 286
274 <xsl:template match="m:property" mode="resolve-members"> 287 <xsl:template match="m:property" mode="resolve-members">