diff 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
line wrap: on
line diff
--- a/xslt/model.xsl	Sun Mar 11 10:27:32 2018 +0300
+++ b/xslt/model.xsl	Mon Mar 12 02:58:34 2018 +0300
@@ -1,10 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0"
 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://implab.org/schemas/data-model.v1.xsd"
-	xmlns:clr="http://implab.org/schemas/data-model/dotnet.v1.xsd"
-	xmlns:cs="http://implab.org/schemas/code-dom/csharp.v1.xsd" xmlns:sql="http://implab.org/schemas/data-model/sql.v1.xsd"
 	xmlns:t="http://implab.org/schemas/temp" xmlns:exsl="http://exslt.org/common"
-	exclude-result-prefixes="clr m xsl exsl sql">
+	exclude-result-prefixes="m xsl exsl">
 	
 	<xsl:import href="preprocess.xsl" />
 	<xsl:include href="text-tools.xsl"/>
@@ -14,9 +12,7 @@
 	<xsl:key name="type" match="m:package/m:*" use="@name" />
 
 	<xsl:template match="/">
-		<cs:document>
-			<xsl:apply-templates select="$module" mode="document" />
-		</cs:document>
+		<xsl:apply-templates select="$module" mode="document" />
 	</xsl:template>
 
 	<!-- code generation -->
@@ -174,7 +170,7 @@
 	
 	expand-member-reference is special mode used to traverse inside
 	members which are referenced from relations. By default this mode
-	does nothing.
+	creates deep copy of all elements.
 	
 	Use this templates to produce intermediate model for further
 	processing by specific code-generators.
@@ -192,7 +188,7 @@
 	
 	
 	<!-- short form of primaryKey{@name, @type} -->
-	<xsl:template match="m:primaryKey" mode="expand-member">
+	<xsl:template match="m:primaryKey[not(m:member)]" mode="expand-member">
 		<xsl:variable name="pkName" select="concat(@declaringType, 'Pk')"/>
 		<xsl:variable name="propName" select="@name" />
 		
@@ -207,17 +203,18 @@
 		<xsl:apply-templates select="exsl:node-set($specialProperty)" mode="expand-member"/>
 		<xsl:copy>
 			<xsl:attribute name="name"><xsl:value-of select="$pkName"/></xsl:attribute>
+			<xsl:attribute name="declarigType"><xsl:value-of select="@declaringType"/></xsl:attribute>
 			<m:member name="{@name}" type="{@declaringType}"/>
 		</xsl:copy>
 	</xsl:template>
 	
 	<!-- long form of primaryKey{ member{@name}+ }  -->
-	<xsl:template match="m:primaryKey[m:member]" mode="expand-member">
-		<xsl:apply-templates mode="expand-member"/>
-	</xsl:template>
+	<!-- will be processed using default template -->
 	
 	<!-- short form of hasA  -->
 	<xsl:template match="m:hasA" mode="expand-member">
+		<xsl:variable name="thisPrefix" select="@name"/>
+		
 		<xsl:variable name="foreignPk">
 			<xsl:call-template name="getPrimaryKey">
 				<xsl:with-param name="type" select="@type"/>
@@ -226,12 +223,20 @@
 		<xsl:variable name="expandedPk">
 			<xsl:apply-templates select="exsl:node-set($foreignPk)" mode="expand-member"/>
 		</xsl:variable>
-		<xsl:variable name="resolvedPk">
-			<xsl:apply-templates select="exsl:node-set($expandedPk)/m:primaryKey" mode="resolve-members"/>
+		<xsl:variable name="pkMembers">
+			<xsl:apply-templates select="exsl:node-set($expandedPk)/*[not(@special)]" mode="resolve-members"/>
 		</xsl:variable>
+		<t:fk>
+			<xsl:for-each select="exsl:node-set($pkMembers)/*">
+				<t:thisKey name="{$thisPrefix}{@name}" matches="{@name}">
+					<xsl:copy-of select="."/>
+				</t:thisKey>
+			</xsl:for-each>
+		</t:fk>
 	</xsl:template>
 	
 	<!-- short form of hasA with matched keys specified -->
+	<!-- TODO -->
 	<xsl:template match="m:hasA[m:thisKey]">
 		<xsl:apply-templates select="m:thisKey" mode="generate-fk-properties"/>
 		<xsl:copy>
@@ -241,7 +246,7 @@
 	</xsl:template>
 	
 	<xsl:template match="m:hasA/m:thisKey" mode="expand-member">
-		<m:member name="{@name}" matches="{@matches}"/>
+		<m:member name="{@name}" matches="{@matches}" type="{../@declaringType}"/>
 	</xsl:template>
 	
 	<!-- long form of hasA -->
@@ -249,24 +254,32 @@
 		<xsl:apply-templates mode="expand-member"/>
 	</xsl:template>
 	
-	<!-- resolve expands -->
-	
+	<!-- resolve-members -->
+	<!-- processes members and copies members to output -->
+	<!-- TODO: Possible recursion -->
 	<xsl:template match="m:member" mode="resolve-members">
-		<xsl:variable name="$name" select="@name"/>
-		<xsl:variable name="$declaringType" select="@declaringType" />
-		
+		<xsl:variable name="name" select="@name"/>
+		<xsl:variable name="declaringType" select="@type" />
 		<xsl:choose>
 			<xsl:when test="/*[@name=$name and @declaringType=$declaringType]">
+				<!-- when reference points to a locally defined member  -->
+				<!-- locally defined members are already expanded -->
 				<xsl:apply-templates select="/*[@name=$name and @declaringType=$declaringType]" mode="resolve-members"/>
 			</xsl:when>
 			<xsl:otherwise>
-				<xsl:variable name="fragment">
+				<!-- otherwise try to use getMembers, then expand it and resolve members  -->
+				<xsl:variable name="members">
 					<xsl:call-template name="getMembers">
 						<xsl:with-param name="memberName" select="$name"/>
 						<xsl:with-param name="type" select="$declaringType"/>
 					</xsl:call-template>
 				</xsl:variable>
-				<xsl:apply-templates select="exsl:node-set($fragment)/*[not(@special)]" mode="resolve-members"/>
+				<!-- expand resolved members like 'hasA' and 'thisKey' -->
+				<xsl:variable name="expanded">
+					<xsl:apply-templates select="exsl:node-set($members)" mode="resolve-members"/>
+				</xsl:variable>
+				<!-- skip @special members when resolving -->
+				<xsl:apply-templates select="exsl:node-set($expanded)/*[not(@special)]" mode="resolve-members"/>
 			</xsl:otherwise>
 		</xsl:choose>
 	</xsl:template>