changeset 2:035de8b7b18e

Temporary commit, refactoring, added readme
author cin
date Sun, 25 Feb 2018 17:12:33 +0300
parents 7f803979305f
children 437127ab6a12
files data/model.xml readme.md xslt/generator.csharp.xsl xslt/model.xsl xslt/model.xslt xslt/preprocess.xsl xslt/preprocess.xslt
diffstat 7 files changed, 965 insertions(+), 678 deletions(-) [+]
line wrap: on
line diff
--- a/data/model.xml	Thu Feb 22 19:14:00 2018 +0300
+++ b/data/model.xml	Sun Feb 25 17:12:33 2018 +0300
@@ -97,6 +97,9 @@
 		<hasA name="Coordinator" type="MaritimeRcc" nullable="true">
 			<thisKey name="CoordinatorId" />
 		</hasA>
+		<hasMany name="PartnerLinks" type="OperationRccLink">
+			<otherKey name="Operation"/>
+		</hasMany>
 	</entity>
 
 	<entity name="SarCase">
@@ -124,5 +127,38 @@
 	<entity name="Region">
 		<description>Район проведение поисково-спасательной операции</description>
 	</entity>
+	
+	<entity name="OperationRccLink">
+		<!-- composite primary key -->
+		<primaryKey name="Id">
+			<!-- use existing members -->
+			<member name="Operation"/>
+			<member name="Rcc"/>
+		</primaryKey>
+		<hasA name="Operation" type="Operation">
+			<thisKey name="OperationId"/>
+		</hasA>
+		<hasA name="Rcc">
+			<thisKey name="RccId"/>
+		</hasA>
+		<hasMany name="Info" type="LinkInfo">
+			<otherKey name="Link"/>
+			<clr:association />
+		</hasMany>
+	</entity>
+	
+	<entity name="LinkInfo">
+		<primaryKey name="Id">
+			<member name="Link"/>
+		</primaryKey>
+		<hasA name="Link" type="OperationRccLink">
+			<thisKey>
+				<property name="OperationId"/>
+				<property name="RccId"/>
+			</thisKey>
+			<clr:association />
+		</hasA>
+		<property name="description" type="string"/>
+	</entity>
 
 </package>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/readme.md	Sun Feb 25 17:12:33 2018 +0300
@@ -0,0 +1,160 @@
+
+# SINOPSYS 
+
+```xml
+<package
+	xmlns="http://implab.org/schemas/data-model.v1.xsd"
+	name="simple-model"
+>
+	<!-- import predefined types -->
+	<import href="types.xml"/>
+	
+	<!-- define entity -->
+	<entity name="Order">
+		<!-- define primary key -->
+		<primaryKey name="Id" type="uuid"/>
+		
+		<!-- define simple property -->
+		<property name="OrderDate" type="dateTime"/>
+		
+		<!-- define hasA relation -->
+		<hasA name="Item" type="Item">
+			<!-- specified a property which holds reference -->
+			<thisKey name="ItemId"/>
+		</hasA>
+		
+		<!-- define hasMany relation -->
+		<hasMany name="Comments" type="OrderComment">
+			<otherKey name="Order"/>
+		</hasMany>
+	</entity>
+	
+	<entity name="Item">
+		<primaryKey name="Id" type="uuid"/>
+		
+		<property name="Title" type="string" />
+		
+	</entity>
+	
+	<entity name="OrderComment">
+		<primaryKey name="Id" type="uuid" />
+		
+		<property name="Comment" type="text" />
+		
+		<property name="Date" type="dateTime" />
+		
+		<hasA name="Order" type="Order">
+			<thisKey name="OrderId" />
+		</hasA>
+	</entity>
+</package>
+```
+
+# DESCRIPTION
+
+Описание модели включает в себя описание наборов сущностей, каждый набор
+называется пакетом.
+
+В пакете содержатся описания сущностей, а также объявления внешних типов,
+которые используются при описании сущностей.
+
+Для уменьшения количества дублируемого кода, а также для упрощения читаемости
+и упрощения сопровождения существует возможность импортирования уже объявленных
+пакетов. В процессе сборки происходит предварительная обработка, которая
+использует все директивы `import` для составления общего модуля, состоящего
+из нескольких пакетов, при этом пакет с которого был начат процесс сборки
+называется первичным и после окончания предварительной обработки для него будет
+выполнена сборка.
+
+Каждая сущность модели состоит из нескольких типов членов, это
+* Свойства - используются для хранения состояния сущности, аналогичны столбцам в таблице
+* Ключи - информация для идентификации записей, каждая запись должна иметь по крайней мере первичный ключ.
+* Отношения - информация о связях между сущностями
+
+## Properties
+
+## PrimaryKey
+
+Внешний ключ представляет собой набор уникальных данных, позволяющий однозначно
+идентифицировать запись, при этом внешний ключ может состоять как из одного
+поля (простые), так и из нескольких (составные)
+
+Простые первичные ключи
+
+```xml
+<primaryKey name="Id" type="uuid"/>
+```
+
+Составные первичные ключи
+
+```xml
+<primaryKey name="Id">
+	<member name="Owner"/>
+	<member name="SeqNum"/>
+</primaryKey>
+<property name="SeqNum" type="integer"/>
+<hasA name="Owner" type="User">
+	<thisKey name="UserId" />
+</hasA>
+```
+При описании составных ключей перечисляется набор членов сущности, которые
+будут образовывать уникальный набор данных для первичного ключа, следует
+отметить, что не все отношения храняться в самой сущности, например,
+`hasMany` может хранится не в записях самой сущности, а в записях связанной
+сущности, такие отношения нельзя включать в первичный ключ.
+Отношение `hasA`, напротив, как парвило хранится в свойствах самой сущности
+и включение его в первичный ключ означает автоматическое включение всех
+свойств.
+
+## Relations
+
+### hasA
+
+`hasA` это отношение, при котором текущей сущности может
+соответсвовать одна связанная сущность. При реализации
+данного отношение может быть указано свойство, используемое
+для хранения ссылки при помощи дочернего элемента `thisKey`
+
+`thisKey` указывает способ реализации отношения, при котором
+в текущей сущности хранится информация для осуществления связи.
+
+По-умолчанию для связи используются первичные ключи, что
+позволяет автоматически определять набор и типы необходимых
+свойств для хранения ссылки.
+
+```xml
+<hasA name="Order" type="Order" optional="true">
+	<thisKey name="OrderId" />
+</hasA>
+```
+
+В случях когда первичный ключ является сложным можно указать
+префикс или явно перечислить свойства в которых будет хранится
+связь
+
+```xml
+<property name="UserId" type="uuid"/>
+<property name="MemberNo" type="integer"/>
+<hasA name="Membership" type="Membership">
+	<thisKey>
+		<member name="UserId"/>
+		<member name="MemberNo"/>
+	</thisKey>
+</hasA>
+```
+
+### hasMany
+
+Отношение при котором с текущей записью связано произвольное
+количество записей другой сущности.
+
+Как правило это отношение является комплиментарным к `hasA` с другой стороны.
+
+```xml
+<hasMany name="Roles" type="Role">
+	<otherKey name="User"/>
+</hasMany>
+```
+
+`otherKey` указывает имя члена в связанной сущности, который хранит информацию
+о связи, это может быть оношение, свойство или ключ.
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xslt/generator.csharp.xsl	Sun Feb 25 17:12:33 2018 +0300
@@ -0,0 +1,379 @@
+<?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">
+	<xsl:import href="model.xsl"/>
+	
+	<xsl:output method="xml" indent="yes" />
+	
+	<!-- entity -->
+	<xsl:template match="m:entity" mode="document">
+		<xsl:apply-templates select="." mode="entity"/>
+	</xsl:template>
+	
+	<xsl:template match="m:entity" mode="entity">
+		<cs:class name="{@name}" modifiers="partial">
+			<xsl:attribute name="name">
+				<xsl:apply-templates select="." mode="class-name"/>
+			</xsl:attribute>
+			<xsl:apply-templates mode="class-attributes" />
+			<xsl:apply-templates mode="members" />
+		</cs:class>
+	</xsl:template>
+	
+	<!-- class-name -->
+	<xsl:template match="*[@name]" mode="class-name">
+		<xsl:value-of select="@name"/>
+	</xsl:template>
+	
+	<xsl:template match="*[@clr:name]" mode="class-name">
+		<xsl:value-of select="@clr:name"/>
+	</xsl:template>
+	
+	<!-- class-attributes -->
+	<xsl:template match="*|text()" mode="class-attributes" />
+
+	<xsl:template match="sql:table" mode="class-attributes">
+		<cs:attribute>
+			<cs:type name="Table" namespace="Linq2Db" />
+			<cs:parameter><cs:string text="{@name}"/></cs:parameter>
+		</cs:attribute>
+	</xsl:template>
+	
+	<!-- generate members -->
+
+	<xsl:template match="*|text()" mode="members" />
+
+	<xsl:template match="m:primaryKey | m:property | m:thisKey | clr:association" mode="members">
+		<t:trace msg="{name()} {@name}"/>
+		<xsl:apply-templates select="." mode="property"/>
+	</xsl:template>
+	
+	<!-- hasA and hasMany doesn't generate members itself, they delegate this work to inner members -->
+	<xsl:template match="m:hasA | m:hasMany" mode="members">
+		<t:trace msg="{name()} {@name}" />
+		<xsl:apply-templates mode="members" />
+	</xsl:template>
+	
+	<xsl:template match="m:hasA/clr:lazy" mode="members">
+		<xsl:apply-templates select="." mode="field"/>
+	</xsl:template>
+
+	<!-- member-name -->
+	<xsl:template match="*|text()|@*" mode="member-name" />
+
+	<xsl:template match="*[@name]" mode="member-name">
+		<xsl:value-of select="@name"/>
+	</xsl:template>
+	<xsl:template match="*[@clr:name]" mode="member-name">
+		<xsl:value-of select="@clr:name"/>
+	</xsl:template>
+	
+	<!-- member-type -->
+	<xsl:template match="*|text()|@*" mode="member-type" />
+	
+	<xsl:template match="*[@type]" mode="member-type">
+		<xsl:call-template name="getClrType">
+			<xsl:with-param name="type" select="@type" />
+		</xsl:call-template>
+	</xsl:template>
+	
+	<xsl:template match="*[clr:type]" mode="member-type">
+		<xsl:apply-templates select="clr:type" mode="clr-type" />
+	</xsl:template>
+	
+	<!-- member-attributes -->
+	<xsl:template match="*|text()" mode="member-attributes"/>
+	
+	<!-- member-extension -->
+	<xsl:template match="*|text()" mode="member-extension"/>
+	<xsl:template match="*" mode="member-extension">
+		<xsl:apply-templates mode="member-extension-comments"/>
+	</xsl:template>
+	
+	<xsl:template match="m:hasA/* | m:hasMany/*" mode="member-extension">
+		<xsl:variable name="comments" select="../m:description | m:description"/>
+		<xsl:apply-templates select="$comments[position() = count($comments)]" mode="member-extension-comments"/>
+	</xsl:template>
+	
+	<!-- member-extension-comments -->
+	<xsl:template match="*|text()" mode="member-extension-comments"/>
+	
+	<xsl:template match="m:description" mode="member-extension-comments">
+		<cs:comments>
+			<xsl:apply-templates mode="comments" />
+		</cs:comments>
+	</xsl:template>
+		
+	<!-- property -->
+	
+	<xsl:template match="*" mode="property">
+		<cs:property modifiers="public">
+			<xsl:attribute name="name"><xsl:apply-templates select="." mode="property-name"/></xsl:attribute>
+			<xsl:apply-templates select="." mode="property-type"/>
+			<xsl:apply-templates select="." mode="property-attributes"/>
+			<xsl:apply-templates select="." mode="property-extension" />
+			<xsl:apply-templates select="." mode="property-accessors"/>
+		</cs:property>
+	</xsl:template>
+	
+	<!-- property-name -->
+	<xsl:template match="m:hasA/clr:association[not(@name|@clr:name)]" mode="property-name">
+		<!-- if the association doesn't define a name, use it from the parent node -->
+		<xsl:apply-templates select=".." mode="property-name"/>
+	</xsl:template>
+	<xsl:template match="m:hasMany/clr:association[not(@name|@clr:name)]" mode="property-name">
+		<!-- if the association doesn't define a name, use it from the parent node -->
+		<xsl:apply-templates select=".." mode="property-name"/>
+	</xsl:template>
+	
+	<xsl:template match="*" mode="property-name">
+		<xsl:apply-templates select="." mode="member-name"/>
+	</xsl:template>
+	
+	<!-- property-type -->
+	<xsl:template match="m:hasA[@type]/clr:association[not(clr:type)]" mode="property-type">
+		<xsl:apply-templates select=".." mode="property-type"/>
+	</xsl:template>
+	
+	<xsl:template match="m:hasMany[@type]/clr:association[not(clr:type)]" mode="property-type">
+		<cs:array>
+			<xsl:apply-templates select=".." mode="property-type"/>
+		</cs:array>
+	</xsl:template>
+	
+	<xsl:template match="m:hasA[@type]/m:thisKey" mode="property-type">
+		<xsl:call-template name="getKeyType">
+			<xsl:with-param name="type" select="../@type"/>
+		</xsl:call-template>
+	</xsl:template>
+	
+	<xsl:template match="m:hasA[@type and boolean(@optional)]/m:thisKey" mode="property-type">
+		<cs:nullable>
+			<xsl:call-template name="getKeyType">
+				<xsl:with-param name="type" select="../@type"/>
+			</xsl:call-template>
+		</cs:nullable>
+	</xsl:template>
+	
+	<xsl:template match="*" mode="property-type">
+		<xsl:apply-templates select="." mode="member-type"/>
+	</xsl:template>
+	
+	<!-- property-attributes -->
+	<xsl:template match="m:primaryKey" mode="property-attributes">
+		<cs:attribute>
+			<cs:type name="PrimaryKey" namespace="Linq2Db" />
+		</cs:attribute>
+	</xsl:template>
+	
+	<xsl:template match="m:hasA/clr:association" mode="property-attributes">
+		<cs:attribute>
+			<cs:type name="Association" namespace="Linq2Db"/>
+			<cs:parameter name="thisKey">
+				<cs:nameOf>
+					<xsl:call-template name="getMember">
+						<xsl:with-param name="memberName" select="../@name"/>
+						<xsl:with-param name="explicitMemberName" select="@thisKey"/>
+						<xsl:with-param name="type" select="../../@name"/>
+						<xsl:with-param name="short" select="true()"/>
+					</xsl:call-template>
+				</cs:nameOf>
+			</cs:parameter>
+			<cs:parameter name="otherKey">
+				<cs:nameOf>
+					<xsl:call-template name="getKeyMember">
+						<xsl:with-param name="explicitMemberName" select="@thisKey"/>
+						<xsl:with-param name="type" select="../@type"/>
+					</xsl:call-template>
+				</cs:nameOf>
+			</cs:parameter>
+		</cs:attribute>
+	</xsl:template>
+
+	<xsl:template match="m:hasMany/clr:association" mode="property-attributes">
+		<cs:attribute>
+			<cs:type name="Association" namespace="Linq2Db"/>
+			<!-- thisKey points to own primaryKey which may be inherited, using getKeyName to address such cases -->
+			<cs:parameter name="thisKey">
+				<cs:nameOf>
+					<xsl:call-template name="getKeyMember">
+						<xsl:with-param name="type" select="../../@name"/>
+						<xsl:with-param name="explicitMemberName" select="@thisKey"/>
+						<xsl:with-param name="short" select="true()"/>
+					</xsl:call-template>
+				</cs:nameOf>
+			</cs:parameter>
+			<cs:parameter name="otherKey">
+				<cs:nameOf>	
+					<xsl:call-template name="getMember">
+						<xsl:with-param name="type" select="../@type"/>
+						<xsl:with-param name="memberName" select="../m:otherKey/@name"/>
+						<xsl:with-param name="explicitMemberName" select="@otherKey"/>
+					</xsl:call-template>
+				</cs:nameOf>
+			</cs:parameter>
+		</cs:attribute>
+	</xsl:template>
+	
+	<xsl:template match="*" mode="property-attributes">
+		<xsl:apply-templates select="." mode="member-attributes"/>
+	</xsl:template>	
+	
+	<!-- property-extension -->
+	<xsl:template match="*" mode="property-extension">
+		<xsl:apply-templates select="." mode="member-extension"/>
+	</xsl:template>
+	
+	<!-- property-accessors -->
+	<xsl:template match="*" mode="property-accessors">
+		<cs:get/>
+		<cs:set/>
+	</xsl:template>
+	
+	<xsl:template match="m:hasA[clr:lazy]/m:thisKey" mode="property-accessors">
+		<cs:get>
+			<xsl:text>return </xsl:text>
+			<xsl:apply-templates select="../clr:lazy" mode="field-name"/>
+			<xsl:text>.Key;</xsl:text>
+		</cs:get>
+		<cs:set>
+			<xsl:apply-templates select="../clr:lazy" mode="field-name"/>
+			<xsl:text>.Key = value;</xsl:text>
+		</cs:set>
+	</xsl:template>
+	
+	<xsl:template match="m:hasA[clr:lazy]/clr:association" mode="property-accessors">
+		<cs:get>
+			<xsl:text>return </xsl:text>
+			<xsl:apply-templates select="../clr:lazy" mode="field-name"/>
+			<xsl:text>.Instance;</xsl:text>
+		</cs:get>
+		<cs:set>
+			<xsl:apply-templates select="../clr:lazy" mode="field-name"/>
+			<xsl:text>.Instance = value;</xsl:text>
+		</cs:set>
+	</xsl:template>
+	
+	<!-- fields -->
+	<xsl:template match="*" mode="field">
+		<cs:field>
+			<xsl:attribute name="name"><xsl:apply-templates select="." mode="field-name"/></xsl:attribute>
+			<xsl:apply-templates select="." mode="field-type"/>
+			<xsl:apply-templates select="." mode="field-initializer"/>
+		</cs:field>
+	</xsl:template>
+	
+	<!-- field-name  -->
+	<xsl:template match="m:hasA/clr:lazy" mode="field-name">
+		<xsl:text>m_lazy</xsl:text>
+		<xsl:apply-templates select=".." mode="property-name"/>
+	</xsl:template>
+	
+	<xsl:template match="clr:lazy[@field]" mode="field-name">
+		<xsl:value-of select="@field"/>
+	</xsl:template>
+	
+	<xsl:template match="*" mode="field-name">
+		<xsl:apply-templates select="." mode="member-name"/>
+	</xsl:template>
+	
+	<!-- field-type -->
+	<xsl:template match="m:hasA[@optional='true']/clr:lazy" mode="field-type">
+		<cs:type name="NullableReference" namespace="Pallada.Data">
+			<xsl:call-template name="getKeyType">
+				<xsl:with-param name="type" select="../@type"/>
+			</xsl:call-template>
+			<xsl:apply-templates select=".." mode="property-type"/>
+		</cs:type>
+	</xsl:template>
+	<xsl:template match="m:hasA[@optional!='true']/clr:lazy" mode="field-type">
+		<cs:type name="Reference" namespace="Pallada.Data">
+			<xsl:apply-templates select="../m:thisKey" mode="property-type"/>
+			<xsl:apply-templates select=".." mode="property-type"/>
+		</cs:type>
+	</xsl:template>
+	
+	<xsl:template match="*" mode="field-type">
+		<xsl:apply-templates select="." mode="member-type"/>
+	</xsl:template>
+	<!-- field-initializer -->
+	<xsl:template match="*|text()" mode="field-initializer"/>
+	
+	<xsl:template name="getKeyClrMemberName">
+		<xsl:param name="explicitMemberName" select="''"/>
+		<xsl:param name="type"/>
+		<xsl:param name="short" select="false()"/>
+		<cs:member>
+			<xsl:choose>
+				<xsl:when test="$explicitMemberName">
+					<xsl:attribute name="name">
+						<xsl:value-of select="$explicitMemberName"/>
+					</xsl:attribute>
+				</xsl:when>
+				<xsl:otherwise>
+					<xsl:attribute name="name">
+						<xsl:call-template name="getKeyName">
+							<xsl:with-param name="type" select="$type"/>
+						</xsl:call-template>
+					</xsl:attribute>
+				</xsl:otherwise>
+			</xsl:choose>
+			<xsl:if test="not($short)">
+				<xsl:call-template name="getClrType">
+					<xsl:with-param name="type" select="$type"/>
+				</xsl:call-template>
+			</xsl:if>
+		</cs:member>
+	</xsl:template>
+	
+	<xsl:template name="getClrMemberName">
+		<xsl:param name="memberName"/>
+		<xsl:param name="type"/>
+		<xsl:param name="explicitMemberName" select="''"/>
+		<xsl:param name="short" select="false()"/>
+		<cs:member>
+			<xsl:choose>
+				<xsl:when test="$explicitMemberName">
+					<xsl:attribute name="name">
+						<xsl:value-of select="$explicitMemberName"/>
+					</xsl:attribute>
+				</xsl:when>
+				<xsl:otherwise>
+					<xsl:attribute name="name">
+						<xsl:call-template name="getClrPropertyName">
+							<xsl:with-param name="member" select="$memberName"/>
+							<xsl:with-param name="type" select="$type"/>
+						</xsl:call-template>
+					</xsl:attribute>
+				</xsl:otherwise>
+			</xsl:choose>
+			<xsl:if test="not($short)">
+				<xsl:call-template name="getClrType">
+					<xsl:with-param name="type" select="$type"/>
+				</xsl:call-template>
+			</xsl:if>
+		</cs:member>
+	</xsl:template>
+	
+		<!-- lookups for the specified member for the specified type
+	and returns the CLR property name  -->
+	<xsl:template name="getClrPropertyName">
+		<xsl:param name="type"/>
+		<xsl:param name="member"/>
+		<!-- prevents cyclic references -->
+		<xsl:param name="seen" select="/.."/>
+		<xsl:for-each select="$module">
+			<xsl:apply-templates select="key('type', $type)"
+				mode="member-lookup">
+				<xsl:with-param name="member" select="$member"/>
+				<xsl:with-param name="seen" select="$seen"/>
+			</xsl:apply-templates>
+		</xsl:for-each>
+	</xsl:template>
+	
+	
+</xsl:stylesheet>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xslt/model.xsl	Sun Feb 25 17:12:33 2018 +0300
@@ -0,0 +1,312 @@
+<?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">
+	
+	<xsl:import href="preprocess.xsl" />
+	<xsl:include href="text-tools.xsl"/>
+	
+	<xsl:output method="xml" indent="yes" />
+
+	<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:template>
+
+	<!-- code generation -->
+
+	<!-- disable default template -->
+	<xsl:template match="*|text()" mode="document" />
+
+	<xsl:template match="t:module" mode="document">
+		<xsl:apply-templates mode="document" />
+	</xsl:template>
+
+	<!-- generate code for primary package -->
+	<xsl:template match="m:package[@primary='true' and @clr:namespace]"	mode="document">
+		<cs:namespace name="{@clr:namespace}">
+			<xsl:apply-templates mode="document" />
+		</cs:namespace>
+	</xsl:template>
+
+	<xsl:template match="m:package[@primary='true']" mode="document">
+		<xsl:apply-templates mode="document" />
+	</xsl:template>
+	
+	<!-- member resolution traits -->
+	
+	<!-- Resolves members using given criteria
+		@param type
+			the name of the type whose members are inspected
+		@param memberName
+			the name of the member to lookup
+		@param scope
+			the scope where to look (members, properties, keys, relations)
+			
+		This template recursively inspects the given type down to it's inheritance
+		hierarchy and outputs members matching criteria.
+	 -->
+	<xsl:template name="getMembers">
+		<xsl:param name="type"/>
+		<xsl:param name="memberName" select="''"/>
+		<xsl:param name="scope" select="'members'"/>
+		<!-- prevents cyclic references -->
+		<xsl:param name="seen" select="/.."/>
+		<xsl:for-each select="$module">
+			<xsl:apply-templates select="key('type', $type)"
+				mode="lookup-members">
+				<xsl:with-param name="memberName" select="$memberName"/>
+				<xsl:with-param name="scope" select="$scope"/>
+				<xsl:with-param name="seen" select="$seen"/>
+			</xsl:apply-templates>
+		</xsl:for-each>
+	</xsl:template>
+	
+	<xsl:template match="*" mode="lookup-members"/>
+	
+	<xsl:template match="m:entity" mode="lookup-members">
+		<xsl:param name="memberName" select="''"/>
+		<xsl:param name="scope" select="'members'"/>
+		<xsl:param name="seen"/>
+		
+		<xsl:variable name="self" select="."/>
+		
+		<xsl:if test="not($seen[generate-id() = generate-id($self)])">
+			<xsl:variable name="scopeMembers">
+				<xsl:call-template name="selectScopeMembers">
+					<xsl:with-param name="scope" select="$scope"/>
+				</xsl:call-template>
+			</xsl:variable>
+			
+			<xsl:variable name="members" select="exsl:node-set($scopeMembers)/*[not($memberName) or @name='$memberName']"/>
+			
+			<xsl:choose>
+				<xsl:when test="$members">
+					<xsl:copy-of select="$members"/>
+				</xsl:when>
+				<xsl:when test="m:extends">
+					<xsl:call-template name="getMembers">
+						<xsl:with-param name="type" select="m:extends/@type"/>
+						<xsl:with-param name="memberName" select="$memberName"/>
+						<xsl:with-param name="scope" select="$scope"/>
+						<xsl:with-param name="seen" select="$seen | $self"/>
+					</xsl:call-template>
+				</xsl:when>
+				<xsl:otherwise>
+					<xsl:call-template name="warn">
+						<xsl:with-param name="msg">
+							<t:trace msg="failed to resolve {$member}"/>
+							<t:trace msg="inspected classes">
+							<xsl:for-each select="$seen | $self">
+								<t:trace msg="{name()} {@name}"/>
+							</xsl:for-each>
+							</t:trace>
+						</xsl:with-param>
+					</xsl:call-template>
+				</xsl:otherwise>
+			</xsl:choose>
+		</xsl:if>
+	</xsl:template>
+	
+	<!-- this template implements scope filtering -->
+	<xsl:template name="selectScopeMembers">
+		<xsl:param name="scope" select="'members'"/>
+		<xsl:choose>
+			<xsl:when test="$scope='members'">
+				<xsl:apply-templates mode="filter-members"/>
+			</xsl:when>
+			<xsl:when test="$scope='keys'">
+				<xsl:apply-template mode="filter-keys"/>
+			</xsl:when>
+			<xsl:when test="$scope='properties'">
+				<xsl:apply-templates mode="fiflter-properties"/>
+			</xsl:when>
+			<xsl:when test="$scope='relations'">
+				<xsl:apply-templates mode="fiflter-relations"/>
+			</xsl:when>
+		</xsl:choose>
+	</xsl:template>
+	
+	<!-- filter-members -->
+	<xsl:template match="*|text()" mode="filter-members"/>
+	<xsl:template match="*[@name]" mode="filter-members">
+		<xsl:copy>
+			<xsl:copy-of select="@*" />
+			<xsl:attribute name="declaringType"><xsl:value-of select="../@name" /></xsl:attribute>
+			<xsl:copy-of select="*" />
+		</xsl:copy>
+	</xsl:template>
+	
+	<!-- filter-keys -->
+	<xsl:template match="*|text()" mode="filter-keys"/>
+	<xsl:template match="m:primaryKey" mode="filter-keys">
+		<xsl:apply-templates select="." mode="filter-members"/>
+	</xsl:template>
+	
+	<!-- filter-properties -->
+	<xsl:template match="*|text()" mode="filter-properties"/>
+	<xsl:template match="m:property" mode="filter-properties">
+		<xsl:apply-templates select="." mode="filter-members"/>
+	</xsl:template>
+	
+	<!-- filter-relations -->
+	<xsl:template match="*|text()" mode="filter-relations"/>
+	<xsl:template match="m:hasA | m:hasMany" mode="filter-relations">
+		<xsl:apply-templates select="." mode="filter-members"/>
+	</xsl:template>
+
+	
+
+	<!-- primary key -->
+	
+	<!-- Resolves primaryKey information for the given type name.
+		@returns m:primaryKey node copy with additional attribute
+		@declaringType which points to the type where this primaryKey
+		was defined.
+	-->
+	<xsl:template name="getPrimaryKey">
+		<xsl:param name="type" />
+		<xsl:call-template name="getMembers">
+			<xsl:with-param name="type" select="$type"/>
+			<xsl:with-param name="scope" select="keys"/>
+		</xsl:call-template>
+	</xsl:template>
+	
+	<!--  -->
+	<xsl:template name="getKeyType">
+		<xsl:param name="type" />
+		<xsl:variable name="otherKey">
+			<xsl:call-template name="getKey">
+				<xsl:with-param name="type" select="$type" />
+			</xsl:call-template>
+		</xsl:variable>
+		<xsl:apply-templates select="exsl:node-set($otherKey)/m:primaryKey" mode="property-type"/>
+	</xsl:template>
+	
+	<xsl:template name="getKeyName">
+		<xsl:param name="type" />
+		<xsl:variable name="otherKey">
+			<xsl:call-template name="getKey">
+				<xsl:with-param name="type" select="$type" />
+			</xsl:call-template>
+		</xsl:variable>
+		<xsl:apply-templates select="exsl:node-set($otherKey)/m:primaryKey" mode="property-name"/>
+	</xsl:template>
+
+	<!-- internal. applied to the entity with a primaryKey node -->
+	<xsl:template match="m:entity[m:primaryKey]" mode="resolvePK">
+		<xsl:apply-templates select="m:primaryKey" mode="resolvePK" />
+	</xsl:template>
+
+	<!--
+	This template formats the result of 'resolvePk' template,
+	override this template to extend returned metadata, beware that
+	other templates rely on the resulting format of this template.
+	-->
+	<xsl:template match="m:primaryKey" mode="resolvePK">
+		<xsl:copy>
+			<xsl:copy-of select="@*" />
+			<xsl:attribute name="declaringType"><xsl:value-of select="../@name" /></xsl:attribute>
+			<xsl:copy-of select="*" />
+		</xsl:copy>
+	</xsl:template>
+
+	<!-- internal, used to traverse the hierarchy -->
+	<xsl:template match="m:entity" mode="resolvePK">
+		<xsl:apply-templates select="m:extends" mode="resolvePK" />
+	</xsl:template>
+
+	<!-- internal, used to traverse the hierarchy -->
+	<xsl:template match="m:extends" mode="resolvePK">
+		<xsl:apply-templates select="key('type', @type)"
+			mode="resolvePK" />
+	</xsl:template>
+
+	<!-- resolves CLR type for the given type -->
+	<xsl:template name="getClrType">
+		<xsl:param name="type" />
+		<xsl:param name="typeArgs" select="/.." />
+		<!-- <t:trace msg="resolveClrType {$type}"/> -->
+		<xsl:for-each select="$module">
+			<xsl:apply-templates select="key('type', $type)"
+				mode="clr-type">
+				<xsl:with-param name="typeArgs" select="$typeArgs"/>
+			</xsl:apply-templates>
+		</xsl:for-each>
+	</xsl:template>
+	
+	<!-- CLR type construction -->
+	<xsl:template match="*" mode="clr-type" />
+
+	<xsl:template match="m:type[clr:type]" mode="clr-type">
+		<xsl:param name="typeArgs" select="clr:type/*" />
+		<xsl:apply-templates select="clr:type" mode="clr-type">
+			<xsl:with-param name="typeArgs" select="$typeArgs" />
+		</xsl:apply-templates>
+	</xsl:template>
+
+	<xsl:template match="m:entity" mode="clr-type">
+		<cs:type name="{@clr:name | @name[not(../@clr:name)]}" namespace="{ancestor::*[@clr:namespace]/@clr:namespace}" />
+	</xsl:template>
+	
+	<xsl:template match="clr:type" mode="clr-type">
+		<xsl:apply-templates mode="clr-type"/>
+	</xsl:template>
+
+	<xsl:template match="clr:type[@ref]" mode="clr-type">
+		<xsl:param name="typeArgs" select="*" />
+		<xsl:call-template name="getClrType">
+			<xsl:with-param name="type" select="@ref" />
+			<xsl:with-param name="typeArgs" select="$typeArgs" />
+		</xsl:call-template>
+	</xsl:template>
+
+	<xsl:template match="clr:type[@cs:name or @name]" mode="clr-type">
+		<xsl:param name="typeArgs" select="*" />
+		<xsl:variable name="ns"
+			select="@cs:namespace | @namespace[not(../@cs:namespace)]" />
+		<cs:type name="{@cs:name | @name[not(../@cs:name)]}">
+			<xsl:if test="$ns">
+				<xsl:attribute name="namespace"><xsl:value-of select="$ns" /></xsl:attribute>
+			</xsl:if>
+			<xsl:copy-of select="@struct"/>
+			
+			<xsl:apply-templates select="$typeArgs | *[not($typeArgs)]"
+				mode="clr-type" />
+		</cs:type>
+	</xsl:template>
+
+	<xsl:template match="clr:arrayOf[@type]" mode="clr-type">
+		<xsl:param name="typeArgs" select="*" />
+		<cs:array>
+			<xsl:call-template name="getClrType">
+				<xsl:with-param name="type" select="@type" />
+				<xsl:with-param name="typeArgs" select="$typeArgs" />
+			</xsl:call-template>
+		</cs:array>
+	</xsl:template>
+
+	<xsl:template match="clr:arrayOf[@cs:name or @name]" mode="clr-type">
+		<xsl:param name="typeArgs" select="*" />
+		<xsl:variable name="ns"
+			select="@cs:namespace | @namespace[not(../@cs:namespace)]" />
+		<cs:array>
+			<cs:type name="{@cs:name | @name[not(../@cs:name)]}">
+				<xsl:if test="$ns">
+					<xsl:attribute name="namespace"><xsl:value-of
+						select="$ns" /></xsl:attribute>
+				</xsl:if>
+				<xsl:apply-templates select="$typeArgs | *[not($typeArgs)]"
+					mode="clr-type" />
+			</cs:type>
+		</cs:array>
+	</xsl:template>
+	
+	
+</xsl:stylesheet>
--- a/xslt/model.xslt	Thu Feb 22 19:14:00 2018 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,659 +0,0 @@
-<?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">
-	<xsl:include href="text-tools.xsl"/>
-	
-	<xsl:output method="xml" indent="yes" />
-
-	<xsl:key name="type" match="m:package/m:*" use="@name" />
-
-	<xsl:variable name="fragment">
-		<t:fragment>
-			<xsl:call-template name="preprocess" />
-		</t:fragment>
-	</xsl:variable>
-
-	<xsl:template name="preprocess">
-		<xsl:param name="root" select="/" />
-		<xsl:param name="pending" select="$root/m:package/m:import[@href]" />
-		<xsl:param name="seen" select="/.." />
-		<xsl:param name="file" select="''" />
-		<xsl:param name="primary" select="true()" />
-
-		<xsl:if test="not($seen[generate-id() = generate-id($root)])">
-			<xsl:apply-templates select="$root" mode="preprocess">
-				<xsl:with-param name="primary" select="$primary" />
-				<xsl:with-param name="file" select="$file" />
-			</xsl:apply-templates>
-			<xsl:if test="$pending">
-				<xsl:variable name="doc" select="document($pending[1]/@href)" />
-				<xsl:call-template name="preprocess">
-					<xsl:with-param name="root" select="$doc" />
-					<xsl:with-param name="file" select="$pending[1]/@href" />
-					<xsl:with-param name="pending"
-						select="$pending[position() > 1] | $doc/m:package/m:import[@href]" />
-					<xsl:with-param name="seen" select="$root | $seen" />
-					<xsl:with-param name="primary" select="false()" />
-				</xsl:call-template>
-			</xsl:if>
-		</xsl:if>
-	</xsl:template>
-
-	<xsl:template match="m:package" mode="preprocess">
-		<xsl:param name="primary" />
-		<xsl:param name="file" />
-		<xsl:copy>
-			<xsl:copy-of select="@*" />
-			<xsl:if test="$primary">
-				<xsl:attribute name="primary"><xsl:value-of select="$primary" /></xsl:attribute>
-			</xsl:if>
-			<xsl:if test="$file">
-				<xsl:attribute name="file"><xsl:value-of select="$file" /></xsl:attribute>
-			</xsl:if>
-			<xsl:apply-templates mode="preprocess">
-				<xsl:with-param name="primary" select="$primary" />
-				<xsl:with-param name="file" select="$file" />
-			</xsl:apply-templates>
-		</xsl:copy>
-	</xsl:template>
-
-	<xsl:template match="*|/" mode="preprocess">
-		<xsl:param name="primary" />
-		<xsl:param name="file" />
-		<xsl:copy>
-			<xsl:copy-of select="@*" />
-			<xsl:apply-templates mode="preprocess">
-				<xsl:with-param name="primary" select="$primary" />
-				<xsl:with-param name="file" select="$file" />
-			</xsl:apply-templates>
-		</xsl:copy>
-	</xsl:template>
-
-	<xsl:template match="/">
-		<cs:document>
-			<xsl:apply-templates select="exsl:node-set($fragment)"
-				mode="generate-document" />
-		</cs:document>
-	</xsl:template>
-
-	<!-- code generation -->
-
-	<!-- disable default template -->
-	<xsl:template match="*|text()" mode="generate-document" />
-
-	<xsl:template match="t:fragment" mode="generate-document">
-		<xsl:apply-templates mode="generate-document" />
-	</xsl:template>
-
-	<!-- generate code for primary package -->
-	<xsl:template match="m:package[@primary='true' and @clr:namespace]"
-		mode="generate-document" priority="1">
-		<cs:namespace name="{@clr:namespace}">
-			<xsl:apply-templates mode="generate-document" />
-		</cs:namespace>
-	</xsl:template>
-
-	<xsl:template match="m:package[@primary='true']" mode="generate-document">
-		<xsl:apply-templates mode="generate-document" />
-	</xsl:template>
-
-	<xsl:template match="m:entity" mode="generate-document">
-		<cs:class name="{@name}" modifiers="partial ">
-			<xsl:apply-templates mode="attributes" />
-			<xsl:apply-templates mode="members" />
-		</cs:class>
-	</xsl:template>
-
-	<xsl:template match="*|text()" mode="attributes" />
-
-	<xsl:template match="sql:table" mode="attributes">
-		<cs:attribute>
-			<cs:type name="Table" namespace="Linq2Db" />
-			<cs:parameter><cs:string text="{@name}"/></cs:parameter>
-		</cs:attribute>
-	</xsl:template>
-	
-	<!-- class-name -->
-	<xsl:template match="*[@name]" mode="type-name">
-		<xsl:value-of select="@name"/>
-	</xsl:template>
-	
-	<xsl:template match="*[@clr:name]" mode="type-name">
-		<xsl:value-of select="@clr:name"/>
-	</xsl:template>
-	
-	<!-- generate members -->
-
-	<xsl:template match="*|text()" mode="members" />
-
-	<xsl:template match="m:primaryKey | m:property | m:thisKey | clr:association" mode="members">
-		<t:trace msg="{name()} {@name}"/>
-		<xsl:apply-templates select="." mode="property"/>
-	</xsl:template>
-	
-	<!-- hasA and hasMany doesn't generate members itself, they delegate this work to inner members -->
-	<xsl:template match="m:hasA | m:hasMany" mode="members">
-		<t:trace msg="{name()} {@name}" />
-		<xsl:apply-templates mode="members" />
-	</xsl:template>
-	
-	<xsl:template match="m:hasA/clr:lazy" mode="members">
-		<xsl:apply-templates select="." mode="field"/>
-	</xsl:template>
-
-	<!-- member-name -->
-	<xsl:template match="*|text()|@*" mode="member-name" />
-
-	<xsl:template match="*[@name]" mode="member-name">
-		<xsl:value-of select="@name"/>
-	</xsl:template>
-	<xsl:template match="*[@clr:name]" mode="member-name">
-		<xsl:value-of select="@clr:name"/>
-	</xsl:template>
-	
-	<!-- member-type -->
-	<xsl:template match="*|text()|@*" mode="member-type" />
-	
-	<xsl:template match="*[@type]" mode="member-type">
-		<xsl:call-template name="getClrType">
-			<xsl:with-param name="type" select="@type" />
-		</xsl:call-template>
-	</xsl:template>
-	
-	<xsl:template match="*[clr:type]" mode="member-type">
-		<xsl:apply-templates select="clr:type" mode="clr-type" />
-	</xsl:template>
-	
-	<!-- member-attributes -->
-	<xsl:template match="*|text()" mode="member-attributes"/>
-	
-	<!-- member-extension -->
-	<xsl:template match="*|text()" mode="member-extension"/>
-	<xsl:template match="*" mode="member-extension">
-		<xsl:apply-templates mode="member-extension-comments"/>
-	</xsl:template>
-	
-	<xsl:template match="m:hasA/* | m:hasMany/*" mode="member-extension">
-		<xsl:variable name="comments" select="../m:description | m:description"/>
-		<xsl:apply-templates select="$comments[position() = count($comments)]" mode="member-extension-comments"/>
-	</xsl:template>
-	
-	<!-- member-extension-comments -->
-	<xsl:template match="*|text()" mode="member-extension-comments"/>
-	
-	<xsl:template match="m:description" mode="member-extension-comments">
-		<cs:comments>
-			<xsl:apply-templates mode="comments" />
-		</cs:comments>
-	</xsl:template>
-	
-
-	
-	<!-- property -->
-	
-	<xsl:template match="*" mode="property">
-		<cs:property modifiers="public">
-			<xsl:attribute name="name"><xsl:apply-templates select="." mode="property-name"/></xsl:attribute>
-			<xsl:apply-templates select="." mode="property-type"/>
-			<xsl:apply-templates select="." mode="property-attributes"/>
-			<xsl:apply-templates select="." mode="property-extension" />
-			<xsl:apply-templates select="." mode="property-accessors"/>
-		</cs:property>
-	</xsl:template>
-	
-	<!-- property-name -->
-	
-	<xsl:template match="m:hasA/clr:association[not(@name|@clr:name)]" mode="property-name">
-		<!-- if the association doesn't define a name, use it from the parent node -->
-		<xsl:apply-templates select=".." mode="property-name"/>
-	</xsl:template>
-	<xsl:template match="m:hasMany/clr:association[not(@name|@clr:name)]" mode="property-name">
-		<!-- if the association doesn't define a name, use it from the parent node -->
-		<xsl:apply-templates select=".." mode="property-name"/>
-	</xsl:template>
-	
-	<xsl:template match="*" mode="property-name">
-		<xsl:apply-templates select="." mode="member-name"/>
-	</xsl:template>
-	
-	<!-- property-type -->
-	<xsl:template match="m:hasA[@type]/clr:association[not(clr:type)]" mode="property-type">
-		<xsl:apply-templates select=".." mode="property-type"/>
-	</xsl:template>
-	
-	<xsl:template match="m:hasMany[@type]/clr:association[not(clr:type)]" mode="property-type">
-		<cs:array>
-			<xsl:apply-templates select=".." mode="property-type"/>
-		</cs:array>
-	</xsl:template>
-	
-	<xsl:template match="m:hasA[@type]/m:thisKey" mode="property-type">
-		<xsl:call-template name="getKeyType">
-			<xsl:with-param name="type" select="../@type"/>
-		</xsl:call-template>
-	</xsl:template>
-	
-	<xsl:template match="m:hasA[@type and boolean(@optional)]/m:thisKey" mode="property-type">
-		<cs:nullable>
-			<xsl:call-template name="getKeyType">
-				<xsl:with-param name="type" select="../@type"/>
-			</xsl:call-template>
-		</cs:nullable>
-	</xsl:template>
-	
-	<xsl:template match="*" mode="property-type">
-		<xsl:apply-templates select="." mode="member-type"/>
-	</xsl:template>
-	
-	<!-- property-attributes -->
-	<xsl:template match="m:primaryKey" mode="property-attributes">
-		<cs:attribute>
-			<cs:type name="PrimaryKey" namespace="Linq2Db" />
-		</cs:attribute>
-	</xsl:template>
-	
-	<xsl:template match="m:hasA/clr:association" mode="property-attributes">
-		<cs:attribute>
-			<cs:type name="Association" namespace="Linq2Db"/>
-			<cs:parameter name="thisKey">
-				<cs:nameOf>
-					<xsl:call-template name="getMember">
-						<xsl:with-param name="memberName" select="../@name"/>
-						<xsl:with-param name="explicitMemberName" select="@thisKey"/>
-						<xsl:with-param name="type" select="../../@name"/>
-						<xsl:with-param name="short" select="true()"/>
-					</xsl:call-template>
-				</cs:nameOf>
-			</cs:parameter>
-			<cs:parameter name="otherKey">
-				<cs:nameOf>
-					<xsl:call-template name="getKeyMember">
-						<xsl:with-param name="explicitMemberName" select="@thisKey"/>
-						<xsl:with-param name="type" select="../@type"/>
-					</xsl:call-template>
-				</cs:nameOf>
-			</cs:parameter>
-		</cs:attribute>
-	</xsl:template>
-	
-	
-	
-	<xsl:template match="m:hasMany/clr:association" mode="property-attributes">
-		<cs:attribute>
-			<cs:type name="Association" namespace="Linq2Db"/>
-			<!-- thisKey points to own primaryKey which may be inherited, using getKeyName to address such cases -->
-			<cs:parameter name="thisKey">
-				<cs:nameOf>
-					<xsl:call-template name="getKeyMember">
-						<xsl:with-param name="type" select="../../@name"/>
-						<xsl:with-param name="explicitMemberName" select="@thisKey"/>
-						<xsl:with-param name="short" select="true()"/>
-					</xsl:call-template>
-				</cs:nameOf>
-			</cs:parameter>
-			<cs:parameter name="otherKey">
-				<cs:nameOf>	
-					<xsl:call-template name="getMember">
-						<xsl:with-param name="type" select="../@type"/>
-						<xsl:with-param name="memberName" select="../m:otherKey/@name"/>
-						<xsl:with-param name="explicitMemberName" select="@otherKey"/>
-					</xsl:call-template>
-				</cs:nameOf>
-			</cs:parameter>
-		</cs:attribute>
-	</xsl:template>
-	
-	<xsl:template match="*" mode="property-attributes">
-		<xsl:apply-templates select="." mode="member-attributes"/>
-	</xsl:template>	
-	
-	<!-- property-extension -->
-	<xsl:template match="*" mode="property-extension">
-		<xsl:apply-templates select="." mode="member-extension"/>
-	</xsl:template>
-	
-	<!-- property-accessors -->
-	<xsl:template match="*" mode="property-accessors">
-		<cs:get/>
-		<cs:set/>
-	</xsl:template>
-	
-	<xsl:template match="m:hasA[clr:lazy]/m:thisKey" mode="property-accessors">
-		<cs:get>
-			<xsl:text>return </xsl:text>
-			<xsl:apply-templates select="../clr:lazy" mode="field-name"/>
-			<xsl:text>.Key;</xsl:text>
-		</cs:get>
-		<cs:set>
-			<xsl:apply-templates select="../clr:lazy" mode="field-name"/>
-			<xsl:text>.Key = value;</xsl:text>
-		</cs:set>
-	</xsl:template>
-	
-	<xsl:template match="m:hasA[clr:lazy]/clr:association" mode="property-accessors">
-		<cs:get>
-			<xsl:text>return </xsl:text>
-			<xsl:apply-templates select="../clr:lazy" mode="field-name"/>
-			<xsl:text>.Instance;</xsl:text>
-		</cs:get>
-		<cs:set>
-			<xsl:apply-templates select="../clr:lazy" mode="field-name"/>
-			<xsl:text>.Instance = value;</xsl:text>
-		</cs:set>
-	</xsl:template>
-	
-	<!-- fields -->
-	<xsl:template match="*" mode="field">
-		<cs:field>
-			<xsl:attribute name="name"><xsl:apply-templates select="." mode="field-name"/></xsl:attribute>
-			<xsl:apply-templates select="." mode="field-type"/>
-			<xsl:apply-templates select="." mode="field-initializer"/>
-		</cs:field>
-	</xsl:template>
-	
-	<!-- field-name  -->
-	<xsl:template match="m:hasA/clr:lazy" mode="field-name">
-		<xsl:text>m_lazy</xsl:text>
-		<xsl:apply-templates select=".." mode="property-name"/>
-	</xsl:template>
-	
-	<xsl:template match="clr:lazy[@field]" mode="field-name">
-		<xsl:value-of select="@field"/>
-	</xsl:template>
-	
-	<xsl:template match="*" mode="field-name">
-		<xsl:apply-templates select="." mode="member-name"/>
-	</xsl:template>
-	
-	<!-- field-type -->
-	<xsl:template match="m:hasA[@optional='true']/clr:lazy" mode="field-type">
-		<cs:type name="NullableReference" namespace="Pallada.Data">
-			<xsl:call-template name="getKeyType">
-				<xsl:with-param name="type" select="../@type"/>
-			</xsl:call-template>
-			<xsl:apply-templates select=".." mode="property-type"/>
-		</cs:type>
-	</xsl:template>
-	<xsl:template match="m:hasA[@optional!='true']/clr:lazy" mode="field-type">
-		<cs:type name="Reference" namespace="Pallada.Data">
-			<xsl:apply-templates select="../m:thisKey" mode="property-type"/>
-			<xsl:apply-templates select=".." mode="property-type"/>
-		</cs:type>
-	</xsl:template>
-	
-	<xsl:template match="*" mode="field-type">
-		<xsl:apply-templates select="." mode="member-type"/>
-	</xsl:template>
-	<!-- field-initializer -->
-	<xsl:template match="*|text()" mode="field-initializer"/>
-
-	<!-- primary key -->
-	
-	<!-- Resolves primaryKey information for the given type name.
-		@returns m:primaryKey node copy with additional attribute
-		@declaringType which points to the type where this primaryKey
-		was defined.
-	-->
-	<xsl:template name="getKey">
-		<xsl:param name="type" />
-		<xsl:for-each select="exsl:node-set($fragment)">
-			<xsl:apply-templates select="key('type', $type)"
-				mode="resolvePK" />
-		</xsl:for-each>
-	</xsl:template>
-	
-	<!--  -->
-	<xsl:template name="getKeyType">
-		<xsl:param name="type" />
-		<xsl:variable name="otherKey">
-			<xsl:call-template name="getKey">
-				<xsl:with-param name="type" select="$type" />
-			</xsl:call-template>
-		</xsl:variable>
-		<xsl:apply-templates select="exsl:node-set($otherKey)/m:primaryKey" mode="property-type"/>
-	</xsl:template>
-	
-	<xsl:template name="getKeyName">
-		<xsl:param name="type" />
-		<xsl:variable name="otherKey">
-			<xsl:call-template name="getKey">
-				<xsl:with-param name="type" select="$type" />
-			</xsl:call-template>
-		</xsl:variable>
-		<xsl:apply-templates select="exsl:node-set($otherKey)/m:primaryKey" mode="property-name"/>
-	</xsl:template>
-
-	<!-- internal. applied to the entity with a primaryKey node -->
-	<xsl:template match="m:entity[m:primaryKey]" mode="resolvePK">
-		<xsl:apply-templates select="m:primaryKey" mode="resolvePK" />
-	</xsl:template>
-
-	<!--
-	This template formats the result of 'resolvePk' template,
-	override this template to extend returned metadata, beware that
-	other templates rely on the resulting format of this template.
-	-->
-	<xsl:template match="m:primaryKey" mode="resolvePK">
-		<xsl:copy>
-			<xsl:copy-of select="@*" />
-			<xsl:attribute name="declaringType"><xsl:value-of select="../@name" /></xsl:attribute>
-			<xsl:copy-of select="*" />
-		</xsl:copy>
-	</xsl:template>
-
-	<!-- internal, used to traverse the hierarchy -->
-	<xsl:template match="m:entity" mode="resolvePK">
-		<xsl:apply-templates select="m:extends" mode="resolvePK" />
-	</xsl:template>
-
-	<!-- internal, used to traverse the hierarchy -->
-	<xsl:template match="m:extends" mode="resolvePK">
-		<xsl:apply-templates select="key('type', @type)"
-			mode="resolvePK" />
-	</xsl:template>
-
-	<!-- resolves CLR type for the given type -->
-	<xsl:template name="getClrType">
-		<xsl:param name="type" />
-		<xsl:param name="typeArgs" select="/.." />
-		<!-- <t:trace msg="resolveClrType {$type}"/> -->
-		<xsl:for-each select="exsl:node-set($fragment)">
-			<xsl:apply-templates select="key('type', $type)"
-				mode="clr-type">
-				<xsl:with-param name="typeArgs" select="$typeArgs"/>
-			</xsl:apply-templates>
-		</xsl:for-each>
-	</xsl:template>
-	
-	<!-- CLR type construction -->
-	<xsl:template match="*" mode="clr-type" />
-
-	<xsl:template match="m:type[clr:type]" mode="clr-type">
-		<xsl:param name="typeArgs" select="clr:type/*" />
-		<xsl:apply-templates select="clr:type" mode="clr-type">
-			<xsl:with-param name="typeArgs" select="$typeArgs" />
-		</xsl:apply-templates>
-	</xsl:template>
-
-	<xsl:template match="m:entity" mode="clr-type">
-		<cs:type name="{@clr:name | @name[not(../@clr:name)]}" namespace="{ancestor::*[@clr:namespace]/@clr:namespace}" />
-	</xsl:template>
-	
-	<xsl:template match="clr:type" mode="clr-type">
-		<xsl:apply-templates mode="clr-type"/>
-	</xsl:template>
-
-	<xsl:template match="clr:type[@ref]" mode="clr-type">
-		<xsl:param name="typeArgs" select="*" />
-		<xsl:call-template name="getClrType">
-			<xsl:with-param name="type" select="@ref" />
-			<xsl:with-param name="typeArgs" select="$typeArgs" />
-		</xsl:call-template>
-	</xsl:template>
-
-	<xsl:template match="clr:type[@cs:name or @name]" mode="clr-type">
-		<xsl:param name="typeArgs" select="*" />
-		<xsl:variable name="ns"
-			select="@cs:namespace | @namespace[not(../@cs:namespace)]" />
-		<cs:type name="{@cs:name | @name[not(../@cs:name)]}">
-			<xsl:if test="$ns">
-				<xsl:attribute name="namespace"><xsl:value-of select="$ns" /></xsl:attribute>
-			</xsl:if>
-			<xsl:copy-of select="@struct"/>
-			
-			<xsl:apply-templates select="$typeArgs | *[not($typeArgs)]"
-				mode="clr-type" />
-		</cs:type>
-	</xsl:template>
-
-	<xsl:template match="clr:arrayOf[@type]" mode="clr-type">
-		<xsl:param name="typeArgs" select="*" />
-		<cs:array>
-			<xsl:call-template name="getClrType">
-				<xsl:with-param name="type" select="@type" />
-				<xsl:with-param name="typeArgs" select="$typeArgs" />
-			</xsl:call-template>
-		</cs:array>
-	</xsl:template>
-
-	<xsl:template match="clr:arrayOf[@cs:name or @name]" mode="clr-type">
-		<xsl:param name="typeArgs" select="*" />
-		<xsl:variable name="ns"
-			select="@cs:namespace | @namespace[not(../@cs:namespace)]" />
-		<cs:array>
-			<cs:type name="{@cs:name | @name[not(../@cs:name)]}">
-				<xsl:if test="$ns">
-					<xsl:attribute name="namespace"><xsl:value-of
-						select="$ns" /></xsl:attribute>
-				</xsl:if>
-				<xsl:apply-templates select="$typeArgs | *[not($typeArgs)]"
-					mode="clr-type" />
-			</cs:type>
-		</cs:array>
-	</xsl:template>
-	
-	<!-- member resolution traits -->
-	
-	<!-- lookups for the specified member for the specified type
-	and returns the CLR property name  -->
-	<xsl:template name="getClrPropertyName">
-		<xsl:param name="type"/>
-		<xsl:param name="member"/>
-		<!-- prevents cyclic references -->
-		<xsl:param name="seen" select="/.."/>
-		<xsl:for-each select="exsl:node-set($fragment)">
-			<xsl:apply-templates select="key('type', $type)"
-				mode="member-lookup">
-				<xsl:with-param name="member" select="$member"/>
-				<xsl:with-param name="seen" select="$seen"/>
-			</xsl:apply-templates>
-		</xsl:for-each>
-	</xsl:template>
-	
-	<xsl:template match="*" mode="member-lookup"/>
-	
-	<xsl:template match="m:entity" mode="member-lookup">
-		<xsl:param name="member"/>
-		<xsl:param name="seen"/>
-		<xsl:variable name="self" select="."/>
-		
-		<xsl:if test="not($seen[generate-id() = generate-id($self)])">
-			<xsl:variable name="match" select="*[@name=$member]"/>
-			<xsl:choose>
-				<xsl:when test="$match">
-					<xsl:apply-templates select="$match" mode="member-lookup"/>
-				</xsl:when>
-				<xsl:when test="m:extends">
-					<xsl:call-template name="getClrPropertyName">
-						<xsl:with-param name="type" select="m:extends/@type"/>
-						<xsl:with-param name="member" select="$member"/>
-						<xsl:with-param name="seen" select="$seen | $self"/>
-					</xsl:call-template>
-				</xsl:when>
-				<xsl:otherwise>
-					<xsl:call-template name="warn">
-						<xsl:with-param name="msg">
-							<t:trace msg="failed to resolve {$member}"/>
-							<t:trace msg="inspected classes">
-							<xsl:for-each select="$seen | $self">
-								<t:trace msg="{name()} {@name}"/>
-							</xsl:for-each>
-							</t:trace>
-						</xsl:with-param>
-					</xsl:call-template>
-				</xsl:otherwise>
-			</xsl:choose>
-		</xsl:if>
-	</xsl:template>
-	
-	<xsl:template match="m:property | m:primaryKey" mode="member-lookup">
-		<xsl:apply-templates select="." mode="property-name" />
-	</xsl:template>
-	
-	<xsl:template match="m:hasA" mode="member-lookup">
-		<xsl:apply-templates select="m:thisKey" mode="property-name"/>
-	</xsl:template>
-	
-	<xsl:template name="getKeyMember">
-		<xsl:param name="explicitMemberName" select="''"/>
-		<xsl:param name="type"/>
-		<xsl:param name="short" select="false()"/>
-		<cs:member>
-			<xsl:choose>
-				<xsl:when test="$explicitMemberName">
-					<xsl:attribute name="name">
-						<xsl:value-of select="$explicitMemberName"/>
-					</xsl:attribute>
-				</xsl:when>
-				<xsl:otherwise>
-					<xsl:attribute name="name">
-						<xsl:call-template name="getKeyName">
-							<xsl:with-param name="type" select="$type"/>
-						</xsl:call-template>
-					</xsl:attribute>
-				</xsl:otherwise>
-			</xsl:choose>
-			<xsl:if test="not($short)">
-				<xsl:call-template name="getClrType">
-					<xsl:with-param name="type" select="$type"/>
-				</xsl:call-template>
-			</xsl:if>
-		</cs:member>
-	</xsl:template>
-	
-	<xsl:template name="getMember">
-		<xsl:param name="memberName"/>
-		<xsl:param name="type"/>
-		<xsl:param name="explicitMemberName" select="''"/>
-		<xsl:param name="short" select="false()"/>
-		<cs:member>
-			<xsl:choose>
-				<xsl:when test="$explicitMemberName">
-					<xsl:attribute name="name">
-						<xsl:value-of select="$explicitMemberName"/>
-					</xsl:attribute>
-				</xsl:when>
-				<xsl:otherwise>
-					<xsl:attribute name="name">
-						<xsl:call-template name="getClrPropertyName">
-							<xsl:with-param name="member" select="$memberName"/>
-							<xsl:with-param name="type" select="$type"/>
-						</xsl:call-template>
-					</xsl:attribute>
-				</xsl:otherwise>
-			</xsl:choose>
-			<xsl:if test="not($short)">
-				<xsl:call-template name="getClrType">
-					<xsl:with-param name="type" select="$type"/>
-				</xsl:call-template>
-			</xsl:if>
-		</cs:member>
-	</xsl:template>
-	
-	
-	
-</xsl:stylesheet>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xslt/preprocess.xsl	Sun Feb 25 17:12:33 2018 +0300
@@ -0,0 +1,78 @@
+<?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:exsl="http://exslt.org/common"
+	xmlns:t="http://implab.org/schemas/temp"
+>
+	<!--
+	This template provides 'module' variable which is the
+	result of processing imports from packages.
+	
+	Variable $module is a node set created from the result-tree
+	which is the result of applying templates in mode=preprocess  
+	-->
+	<xsl:output method="xml" indent="yes" />
+	<xsl:variable name="preprocessResult">
+		<t:module>
+			<xsl:call-template name="preprocess" />
+		</t:module>
+	</xsl:variable>
+	<xsl:variable name="module" select="exsl:node-set($preprocessResult)/t:module"/>
+
+	<xsl:template name="preprocess">
+		<xsl:param name="root" select="/" />
+		<xsl:param name="pending" select="$root/m:package/m:import[@href]" />
+		<xsl:param name="seen" select="/.." />
+		<xsl:param name="file" select="''" />
+		<xsl:param name="primary" select="true()" />
+
+		<xsl:if test="not($seen[generate-id() = generate-id($root)])">
+			<xsl:apply-templates select="$root" mode="preprocess">
+				<xsl:with-param name="primary" select="$primary" />
+				<xsl:with-param name="file" select="$file" />
+			</xsl:apply-templates>
+			<xsl:if test="$pending">
+				<xsl:variable name="doc" select="document($pending[1]/@href)" />
+				<xsl:call-template name="preprocess">
+					<xsl:with-param name="root" select="$doc" />
+					<xsl:with-param name="file" select="$pending[1]/@href" />
+					<xsl:with-param name="pending"
+						select="$pending[position() > 1] | $doc/m:package/m:import[@href]" />
+					<xsl:with-param name="seen" select="$root | $seen" />
+					<xsl:with-param name="primary" select="false()" />
+				</xsl:call-template>
+			</xsl:if>
+		</xsl:if>
+	</xsl:template>
+
+	<xsl:template match="m:package" mode="preprocess">
+		<xsl:param name="primary" />
+		<xsl:param name="file" />
+		<xsl:copy>
+			<xsl:copy-of select="@*" />
+			<xsl:if test="$primary">
+				<xsl:attribute name="primary"><xsl:value-of select="$primary" /></xsl:attribute>
+			</xsl:if>
+			<xsl:if test="$file">
+				<xsl:attribute name="file"><xsl:value-of select="$file" /></xsl:attribute>
+			</xsl:if>
+			<xsl:apply-templates mode="preprocess">
+				<xsl:with-param name="primary" select="$primary" />
+				<xsl:with-param name="file" select="$file" />
+			</xsl:apply-templates>
+		</xsl:copy>
+	</xsl:template>
+
+	<xsl:template match="*|/" mode="preprocess">
+		<xsl:param name="primary" />
+		<xsl:param name="file" />
+		<xsl:copy>
+			<xsl:copy-of select="@*" />
+			<xsl:apply-templates mode="preprocess">
+				<xsl:with-param name="primary" select="$primary" />
+				<xsl:with-param name="file" select="$file" />
+			</xsl:apply-templates>
+		</xsl:copy>
+	</xsl:template>
+</xsl:stylesheet>
\ No newline at end of file
--- a/xslt/preprocess.xslt	Thu Feb 22 19:14:00 2018 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<xsl:stylesheet version="1.0"
-	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-	xmlns:m="urn:implab.org:schemas:data-models:v1"
-	xmlns:dom="urn:implab.org:schemas:code-dom:v1"
-	xmlns:exsl="http://exslt.org/common">
-	<xsl:output method="xml" indent="yes" />
-	<xsl:template match="/">
-		<xsl:apply-templates mode="preprocess"/>
-	</xsl:template>
-	
-	<xsl:template match="package">
-		<xsl:apply-templates>
-	</xsl:template>
-	
-	<xsl:template match="m:include" mode="preprocess">
-		<xsl:variable name="doc" select="document(@href)"/>
-	</xsl:template>
-</xsl:stylesheet>
\ No newline at end of file