sysmodellibs/sysmodelgen/src/svg/Model.xsl
changeset 6 5b32dc297d05
parent 3 e7e0ae78773e
child 7 3c36c452f013
equal deleted inserted replaced
3:e7e0ae78773e 6:5b32dc297d05
     1 <?xml version="1.0"?>
       
     2  <!DOCTYPE XSLT  [
       
     3       <!ENTITY AZ  "ABCDEFGHIJKLMNOPQRSTUVWXYZ">
       
     4       <!ENTITY az  "abcdefghijklmnopqrstuvwxyz">
       
     5  ]>
       
     6  <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"  xmlns:s="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:date="http://exslt.org/dates-and-times" exclude-result-prefixes="date exslt"
       
     7 		xmlns:exslt="http://exslt.org/common">
       
     8 	<xsl:param name="xml-stylesheet"/><!-- set to default stylesheet-->
       
     9 	<xsl:param name="Verbose" select="0"/><!-- Verbosity level of messages. Set to 1 (or higher) to get runtime comments-->
       
    10 	<xsl:param name="Origin-attr">old_model</xsl:param>
       
    11 	<xsl:output method="xml" cdata-section-elements="script s:script" indent="yes"/>
       
    12 	  <xsl:key name="named" match="*" use="@name"/>
       
    13 
       
    14 
       
    15 <!-- ====== Computed values ============= -->
       
    16 
       
    17 <xsl:variable name="Version">
       
    18 	<xsl:choose>
       
    19 		<xsl:when test="/model/@ver"><xsl:value-of select="/model/@ver"/></xsl:when>
       
    20 		<xsl:when test="/model/@deps"><xsl:value-of select="document(/model/@deps,.)/SystemModelDeps/@version"/></xsl:when>
       
    21 		<xsl:when test="count(/model/sysdef/info[@type='s12'])=1">
       
    22 			<xsl:value-of select="document(/model/sysdef/info[@type='s12']/@href,.)/Schedule12/@OS_version"/>
       
    23 		</xsl:when>
       
    24 	</xsl:choose>
       
    25 </xsl:variable>
       
    26 
       
    27 
       
    28 <!-- for merging: -->
       
    29 
       
    30 <xsl:variable name="filter-nodes" select="//filter[not(@accept or @reject) and @select and @display]"/>
       
    31 	<!--  @value is optional, but should be set unless select=* -->
       
    32 
       
    33 <xsl:variable name="filter-in"> <!--comma-separated list of filters to accept -->
       
    34 	<xsl:text>,</xsl:text>
       
    35 	<xsl:if test="not($filter-nodes) and //filter[@accept|@reject]">
       
    36 		<xsl:value-of  select="concat($Version,',')"/> <!-- the version number -->
       
    37 	</xsl:if>
       
    38 	<xsl:for-each select="//filter[@accept]"><xsl:value-of select="@accept"/>,</xsl:for-each>
       
    39 	<xsl:for-each select="//filter[@reject]">!<xsl:value-of select="@reject"/>,</xsl:for-each>
       
    40 </xsl:variable>
       
    41 
       
    42 
       
    43 <xsl:variable name="ignore" select="//ignore"/>
       
    44 
       
    45 <xsl:variable name="abbrevs" select="document(/model/layout/info/@href,/)/display-names//abbrev"/> <!-- the abbreviations list -->
       
    46 
       
    47 <xsl:variable name="simple-filters" select="not(function-available('exslt:node-set'))"/>
       
    48 
       
    49 <!-- ====== Error checking ============= -->
       
    50 
       
    51 <!--  start error checking -->
       
    52 
       
    53 <xsl:template match="SystemDefinition/@schema" mode="check">
       
    54 	<!-- warning if schema is less than 1.4 -->
       
    55 	<xsl:if test="substring-before(.,'.') &lt; 1 or (substring-before(.,'.')=1 and substring-before(substring-after(.,'.'),'.') &lt; 4)">
       
    56 		<xsl:call-template name="Caller-Warning"><xsl:with-param name="text">using old System Definition schema (<xsl:value-of select="."/>)</xsl:with-param></xsl:call-template>
       
    57 	</xsl:if>
       
    58 <!--	<xsl:if test="starts-with(.,'1.4.')">
       
    59 		<xsl:call-template name="Caller-Note"><xsl:with-param name="text">using backwards compatible System Definition schema (<xsl:value-of select="."/>)</xsl:with-param></xsl:call-template>
       
    60 	</xsl:if> -->
       
    61 	<xsl:if test="substring-before(.,'.') &gt; 2">
       
    62 		<xsl:call-template name="Critical-Error"><xsl:with-param name="text">using incompatible System Definition schema (<xsl:value-of select="."/>)</xsl:with-param></xsl:call-template>
       
    63 	</xsl:if>
       
    64 </xsl:template>
       
    65 
       
    66 <xsl:template match="*" mode="check"/>
       
    67 
       
    68 <xsl:template match="block[ancestor::layer/@levels]|lsubblock[ancestor::layer/@levels] | logicalset[ancestor::layer/@levels]|logicalsubset[ancestor::layer/@levels]" mode="check">
       
    69 	<xsl:variable name="levels"><xsl:text> </xsl:text><xsl:apply-templates mode="layernames" select="."/><xsl:text> </xsl:text></xsl:variable>	
       
    70 	<xsl:for-each select="module | collection">
       
    71 	<xsl:if test="not(contains($levels,concat(' ',@level,' ')))">
       
    72 		<xsl:call-template name="Caller-Error"><xsl:with-param name="text">Invalid level: <xsl:value-of select="@level"/> in <xsl:value-of select="@name"/></xsl:with-param></xsl:call-template>
       
    73 	</xsl:if>
       
    74 	</xsl:for-each>
       
    75 </xsl:template>
       
    76 
       
    77 <xsl:template name="Caller-Debug"><xsl:param name="text"/>
       
    78 	<xsl:if test="$Verbose &gt; 5"><xsl:message>&#xa;Note: <xsl:value-of select="$text"/></xsl:message></xsl:if>
       
    79 </xsl:template>
       
    80 <xsl:template name="Caller-Note"><xsl:param name="text"/>
       
    81 	<xsl:message>&#xa;Note: <xsl:value-of select="$text"/></xsl:message>
       
    82 </xsl:template>
       
    83 <xsl:template name="Caller-Warning"><xsl:param name="text"/>
       
    84 	<xsl:message>&#xa;Warning: <xsl:value-of select="$text"/></xsl:message>
       
    85 </xsl:template>
       
    86 <xsl:template name="Caller-Error"><xsl:param name="text"/>
       
    87 	<xsl:message>&#xa;Error: <xsl:value-of select="$text"/></xsl:message>
       
    88 </xsl:template>
       
    89 <xsl:template name="Critical-Error"><xsl:param name="text"/>
       
    90 	<xsl:message terminate="yes">&#xa;Error: <xsl:value-of select="$text"/></xsl:message>
       
    91 </xsl:template>
       
    92 
       
    93 <!--- ==================== merge ==================== -->
       
    94 
       
    95 <!-- merge is used this is called on a model file -->
       
    96 
       
    97 <xsl:template match="@*" mode="copyroot" priority="-1"><xsl:copy-of select="."/></xsl:template>
       
    98 <xsl:template match="@detail" mode="copyroot">
       
    99 	<xsl:choose>
       
   100 		<xsl:when test=".='module'"><xsl:attribute name="detail">collection</xsl:attribute></xsl:when>
       
   101 		<xsl:when test=".='logicalsubset'"><xsl:attribute name="detail">subblock</xsl:attribute></xsl:when>
       
   102 		<xsl:when test=".='logicalset'"><xsl:attribute name="detail">block</xsl:attribute></xsl:when>
       
   103 		<xsl:otherwise><xsl:copy-of select="."/></xsl:otherwise>
       
   104 	</xsl:choose>
       
   105 </xsl:template>
       
   106 
       
   107 
       
   108 <xsl:template match="@revision[../@revision-type='date' and contains(.,'%') and function-available('date:month-abbreviation')]" mode="copyroot">
       
   109 <xsl:attribute name="revision"><xsl:call-template name="format-date"/></xsl:attribute>
       
   110 </xsl:template>
       
   111 
       
   112 <xsl:template match="/model">
       
   113 	<xsl:if test="$xml-stylesheet!='' "> <!-- start with an optional style sheet transform -->
       
   114 		<xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="<xsl:value-of select="$xml-stylesheet"/>"</xsl:processing-instruction>
       
   115 	</xsl:if>
       
   116 	<SystemDefinition>
       
   117 		<xsl:copy-of select="document(sysdef/@href, .)/SystemDefinition/@*"/>
       
   118 	<xsl:copy-of select="document(layout/info[@type='abbrev']/@href,.)/*/@xml:lang"/> <!-- copy localized language value -->
       
   119 		<xsl:apply-templates select="document(@deps, .)/SystemModelDeps" mode="merge"/>
       
   120 		<styling>
       
   121 			<xsl:apply-templates select="@shapes | layout/info" mode="styling"/>
       
   122 		</styling>
       
   123 		<xsl:apply-templates select="document(sysdef/@href, .)/SystemDefinition/@schema" mode="check"/>
       
   124 		  <systemModel>
       
   125 			<!-- copy all attributes from all the system models and from the model element -->
       
   126 			<xsl:apply-templates select="document(sysdef/@href, .)/SystemDefinition/systemModel/@*|@*|layout/@*" mode="copyroot"/>
       
   127 			<xsl:if test="$Version!='' and not(@ver)">
       
   128 				<xsl:attribute name="ver">
       
   129 					<xsl:value-of select="$Version"/>
       
   130 				</xsl:attribute>
       
   131 			</xsl:if>
       
   132 			<!-- special handing of revision details -->
       
   133 			<xsl:choose><!-- get revision details from deps if not otherwise specified -->
       
   134 				<xsl:when test="@deps and (not(@revision-type) or @revision-type='build') and not(@revision)">
       
   135 					<xsl:variable name="rt" select="@revision-type"/>
       
   136 					<xsl:for-each select="document(@deps,.)/SystemModelDeps[@number]">
       
   137 						<xsl:if test="not($rt)">
       
   138 							<xsl:attribute name="revision-type">build</xsl:attribute>
       
   139 						</xsl:if>
       
   140 						<xsl:attribute name="revision"><xsl:value-of select="@number"/></xsl:attribute>
       
   141 					</xsl:for-each>
       
   142 				</xsl:when>
       
   143 				<xsl:when test="@revision-type='date' and not(@revision) and function-available('date:date-time')">
       
   144 					<xsl:attribute name="revision"><xsl:value-of select="substring-before(date:date-time(),'T')"/></xsl:attribute>
       
   145 				</xsl:when>
       
   146 			</xsl:choose>
       
   147 			<xsl:apply-templates select="layout/legend" mode="merge"/> <!-- add legend as a layer -->
       
   148 			<xsl:apply-templates select="layout/logo" mode="merge"/> <!-- copy logos to use when drawing: put first so it's drawn last-->
       
   149 			<xsl:choose>
       
   150 				<xsl:when test="not(function-available('exslt:node-set'))">
       
   151 					<!-- can't do much, don't bother filtering -->
       
   152 					<xsl:apply-templates select="sysdef" mode="merge"/>
       
   153 				</xsl:when>
       
   154 				<xsl:otherwise>
       
   155 					<xsl:for-each select="/model"> <!-- hack for xsltproc which loses the current node -->				
       
   156 					<xsl:variable name="unfiltered">
       
   157 							<xsl:choose>
       
   158 								<xsl:when test="count(sysdef) &lt; 2"> <!-- no need to merge multi models -->
       
   159 									<xsl:apply-templates select="sysdef" mode="merge"/>
       
   160 								</xsl:when>
       
   161 								<xsl:otherwise>
       
   162 									<xsl:variable name="sysdefs">
       
   163 										<xsl:for-each select="sysdef">
       
   164 											<sysdef><xsl:apply-templates select="." mode="merge"/></sysdef>
       
   165 										</xsl:for-each>
       
   166 									</xsl:variable>
       
   167 									<xsl:apply-templates select="exslt:node-set($sysdefs)/sysdef[1]" mode="override-merge">
       
   168 										<xsl:with-param name="next" select="exslt:node-set($sysdefs)/sysdef[2]"/>
       
   169 									</xsl:apply-templates>
       
   170 								</xsl:otherwise>
       
   171 							</xsl:choose>
       
   172 						</xsl:variable>
       
   173 						<xsl:apply-templates select="exslt:node-set($unfiltered)/*" mode="filter-for-presence"/>
       
   174 					</xsl:for-each>
       
   175 				</xsl:otherwise>
       
   176 			</xsl:choose>
       
   177 			<xsl:copy-of select="layout/layer-group"/> <!-- copy layer groups to use when drawing: put last so it's drawn first-->
       
   178 		  </systemModel>
       
   179 	</SystemDefinition>
       
   180 </xsl:template>
       
   181 
       
   182 
       
   183 <!-- merge multiple models together -->
       
   184 
       
   185 <xsl:template match="sysdef" mode="override-merge"><xsl:param name="next"/>
       
   186 	<xsl:choose>
       
   187 		<xsl:when test="not($next)">
       
   188 			<xsl:copy-of select="*"/>
       
   189 		</xsl:when>
       
   190 		<xsl:otherwise>
       
   191 			<xsl:call-template name="Caller-Note"><xsl:with-param name="text">Merging with model <xsl:value-of select="1 +  count($next/preceding-sibling::sysdef)"/></xsl:with-param></xsl:call-template>			
       
   192 			<xsl:variable name="cur">
       
   193 					<sysdef>
       
   194 						<xsl:apply-templates select="*" mode="override-merge">
       
   195 							<xsl:with-param name="other" select="$next"/>
       
   196 						</xsl:apply-templates>
       
   197 						<xsl:apply-templates select="$next/*" mode="append">
       
   198 							<xsl:with-param name="main" select="."/>
       
   199 						</xsl:apply-templates>
       
   200 					</sysdef>
       
   201 			</xsl:variable>
       
   202 			<xsl:apply-templates select="exslt:node-set($cur)/sysdef" mode="override-merge">
       
   203 				<xsl:with-param name="next" select="$next/following-sibling::sysdef[1]"/>
       
   204 			</xsl:apply-templates>	
       
   205 		</xsl:otherwise>
       
   206 	</xsl:choose>
       
   207 </xsl:template>
       
   208 
       
   209 <xsl:template match="component/*" mode="override-merge" priority="7"><xsl:copy-of select="."/></xsl:template>
       
   210 
       
   211 <xsl:template match="*" mode="override-merge"><xsl:param name="other"/>
       
   212 	<xsl:variable name="n" select="@name"/>
       
   213 	<xsl:variable name="tag" select="name()"/>
       
   214 	<xsl:choose>
       
   215 		<xsl:when test="$other/*[@override=$n and name()=$tag]"> 
       
   216 			<!--replace this with the other one at the current location-->
       
   217 			<xsl:apply-templates select="$other/*[@override=$n and name()=$tag]" mode="safe-combine">
       
   218 				<xsl:with-param name="main-root" select="ancestor-or-self::sysdef"/>
       
   219 			</xsl:apply-templates>
       
   220 		</xsl:when>
       
   221 		<xsl:when test="$other/ancestor-or-self::sysdef/descendant::*[@override=$n and name()=$tag]">
       
   222 			<!-- it's overridden somewhere else (perhaps deleted)-->
       
   223 			<xsl:call-template name="Caller-Note"><xsl:with-param name="text">Deleting  <xsl:value-of select="$n"/></xsl:with-param></xsl:call-template>			
       
   224 		</xsl:when>
       
   225 		<xsl:when test="$other/*[@rename=$n or @move=$n and name()=$tag]"> <!-- leave as is at the current location-->
       
   226 			<xsl:if test="$other/*[@rename=$n or @move=$n and name()=$tag]/*">
       
   227 			<xsl:call-template name="Caller-Warning"><xsl:with-param name="text">ignoring children</xsl:with-param></xsl:call-template>
       
   228 			</xsl:if>
       
   229 			<xsl:copy>
       
   230 				<xsl:copy-of select="@*"/> <!-- own attributes -->
       
   231 				<xsl:copy-of select="$other/*[@rename=$n or @move=$n and name()=$tag]/@name"/> <!-- overwrite names -->
       
   232 			<!--leave as is at the current location-->					
       
   233 				<xsl:apply-templates select="*[not(self::name)]"  mode="override-merge"> <!-- do not copy deprecated localised name form-->
       
   234 					<xsl:with-param name="other" select="$other/ancestor-or-self::sysdef"/> <!-- use root of other, to catch any moved decendants -->
       
   235 				</xsl:apply-templates>
       
   236 			</xsl:copy>
       
   237 		</xsl:when>
       
   238 		<xsl:when test="$other/ancestor-or-self::sysdef/descendant::*[@rename=$n or @move=$n and $tag=name()]">
       
   239 			<!--it's moved to elsewhere-->
       
   240 			<xsl:call-template name="Caller-Note"><xsl:with-param name="text">Relocating <xsl:value-of select="$n"/></xsl:with-param></xsl:call-template>
       
   241 		</xsl:when>
       
   242 		<xsl:when test="$other/*[@name=$n and self::component]">
       
   243 			<!--use other at the current location-->
       
   244 			<xsl:copy>
       
   245 				<xsl:copy-of select="$other/*[@name=$n and name()=$tag]/@*"/> <!--other's attributes -->
       
   246 				<!-- should put something here to merge filters,classes properly -->
       
   247 				<xsl:copy-of select="*"/> <!--current conent -->
       
   248 					<xsl:variable name="this" select="."/>
       
   249 				<xsl:apply-templates mode="copy-merge-content"  select="$other/*[@name=$n and name()=$tag]/*">
       
   250 					<xsl:with-param name="main" select="$this"/>
       
   251 				</xsl:apply-templates> <!--other's conent -->
       
   252 			</xsl:copy>
       
   253 		</xsl:when>
       
   254 		<xsl:when test="$other/*[@name=$n and name()=$tag]"> <!-- leave as is at the current location, merge children-->
       
   255 			<xsl:for-each select="$other/*[@name=$n and name()=$tag]">
       
   256 				<xsl:if test="(self::module and $tag!='module') or (not(self::module) and $tag='module')   or   (self::collection and $tag!='collection') or (not(self::collection) and $tag='collection')">
       
   257 					<xsl:call-template name="Critical-Error"><xsl:with-param name="text">Cannot merge <xsl:value-of select="name()"/> "<xsl:value-of select="@name"/>" with <xsl:value-of select="$tag"/> "<xsl:value-of select="$n"/>"</xsl:with-param></xsl:call-template>				
       
   258 				</xsl:if>
       
   259 			</xsl:for-each>
       
   260 			<xsl:copy>
       
   261 				<xsl:copy-of select="@*"/> <!-- own attributes -->
       
   262 				<xsl:copy-of select="$other/*[@name=$n and name()=$tag]/@*"/> <!-- other's attributes -->
       
   263 				<xsl:apply-templates select="*"  mode="override-merge">
       
   264 					<xsl:with-param name="other" select="$other/*[@name=$n and name()=$tag]"/> <!-- use other version of this -->
       
   265 				</xsl:apply-templates>
       
   266 			</xsl:copy>
       
   267 		</xsl:when>
       
   268 		<xsl:when test="$other/ancestor-or-self::sysdef/descendant::*[@name=$n and name()=$tag]">
       
   269 			<!-- it's moved to elsewhere -->
       
   270 			<xsl:call-template name="Caller-Note"><xsl:with-param name="text">Relocating and merging <xsl:value-of select="$n"/></xsl:with-param></xsl:call-template>
       
   271 		</xsl:when>
       
   272 		<xsl:otherwise> <!--  there is no equivalent in 2nd, just copy this and descend -->
       
   273 			<xsl:copy>
       
   274 				<xsl:copy-of select="@*"/> <!-- own attributes -->	
       
   275 				<xsl:apply-templates select="*"  mode="override-merge">
       
   276 					<xsl:with-param name="other" select="$other/ancestor-or-self::sysdef"/> <!-- use root of other, to catch any moved decendants -->
       
   277 				</xsl:apply-templates>
       
   278 			</xsl:copy>		
       
   279 		</xsl:otherwise>
       
   280 	</xsl:choose>
       
   281 	<!-- this won't work if the main model has nothing in the group, but that's invalid, so it's ok. -->
       
   282 	<xsl:if test="position()=last() and name($other)!='sysdef'">
       
   283 		<!--Check if other has stuff to append that's not already been used   -->
       
   284 		<xsl:apply-templates select="$other/*" mode="append">
       
   285 			<xsl:with-param name="main" select=".."/>
       
   286 		</xsl:apply-templates>		
       
   287 	</xsl:if>
       
   288 </xsl:template>
       
   289 
       
   290 
       
   291 <xsl:template mode="copy-merge-content" match="*">
       
   292 <xsl:copy-of select="."/>
       
   293 </xsl:template>
       
   294 
       
   295 
       
   296 <xsl:template mode="copy-merge-content" match="component/*[starts-with(name(),'generator-')]"><xsl:param name="main"/>
       
   297 	<xsl:variable name="self" select="."/>
       
   298 	<!-- only copy if does not exist in original -->
       
   299 	<xsl:if test="not($main/*[name()=name($self)])">
       
   300 		<xsl:copy-of select="$self"/>
       
   301 	</xsl:if>
       
   302 </xsl:template>
       
   303 
       
   304 <xsl:template mode="copy-merge-content" match="unit"><xsl:param name="main"/>
       
   305 	<xsl:variable name="attr"><xsl:for-each select="@*"><xsl:value-of select="concat(name(),'=',.)"></xsl:value-of></xsl:for-each></xsl:variable>
       
   306 	<xsl:variable name="exists">
       
   307 		<xsl:for-each select="$main/unit">
       
   308 			<xsl:variable name="attr2"><xsl:for-each select="@*"><xsl:value-of select="concat(name(),'=',.)"></xsl:value-of></xsl:for-each></xsl:variable>
       
   309 			<xsl:if test="$attr2=$attr">*</xsl:if>
       
   310 		</xsl:for-each>
       
   311 	</xsl:variable>
       
   312 	<!-- only copy if does not exist in original's units-->
       
   313 	<xsl:if test="$exists=''">
       
   314 		<xsl:copy-of select="."/>
       
   315 	</xsl:if>
       
   316 </xsl:template>
       
   317 
       
   318 
       
   319 
       
   320 <xsl:template match="*[@override  and *]" mode="append" priority="5"><xsl:param name="main"/>
       
   321 	<xsl:call-template name="Caller-Debug"><xsl:with-param name="text">Append (5) <xsl:value-of select="@name"/></xsl:with-param></xsl:call-template>	
       
   322 
       
   323 	<xsl:variable name="n" select="@override"/>
       
   324 		<!-- if override is not in main, use this -->
       
   325 	<xsl:if test="@name and not($main/*[@name=$n])">
       
   326 		<!--  must have a name for renaming -->
       
   327 		<xsl:copy>
       
   328 			<xsl:copy-of select="@*[not(name()='override' or name()='rename' or name()='move')]"/>
       
   329 			<xsl:apply-templates select="*" mode="safe-combine">
       
   330 				<xsl:with-param name="main-root" select="$main/ancestor-or-self::sysdef"/>
       
   331 			</xsl:apply-templates>	
       
   332 		</xsl:copy>
       
   333 	</xsl:if>
       
   334 </xsl:template>
       
   335 
       
   336 <xsl:template match="*" mode="safe-combine" priority="-1"><xsl:param name="main-root"/>
       
   337 	<xsl:copy>
       
   338 		<xsl:copy-of select="@*"/>
       
   339 		<xsl:apply-templates select="*"  mode="safe-combine">
       
   340 			<xsl:with-param name="main-root" select="$main-root"/>
       
   341 		</xsl:apply-templates>
       
   342 	</xsl:copy>
       
   343 </xsl:template>
       
   344 
       
   345 <xsl:template match="*[@rename or @move]" mode="safe-combine" priority="3"><xsl:param name="main-root"/>
       
   346 	<xsl:variable name="n" select="@rename | @move"/>
       
   347 	<xsl:variable name="newname" select="@name"/>
       
   348 	<xsl:variable name="atts" select="@*[name()!='name' and name()!='move' and name()!='rename' and name()!='override']"/> <!-- if any non-oragnisational attributes are present on the "move" item, then they should override the values in the thing being moved -->
       
   349 	<xsl:variable name="other" select="ancestor-or-self::sysdef"/>
       
   350 	<!-- can't merge a component with a container -->
       
   351 	<xsl:for-each select="$main-root/descendant::*[@name=$n and (self::layer or self::block or self::subblock or self::logicalset or self::logicalsubset)]">
       
   352 		<xsl:copy>
       
   353 			<xsl:copy-of select="@*"/>
       
   354 			<xsl:copy-of select="$newname"/>	
       
   355 			<xsl:copy-of select="$atts"/> <!-- if any of these attributes are present on the moved collection, then use that value rather than the value in the one being moved -->
       
   356 			<xsl:apply-templates select="*"  mode="override-merge"> <!-- do not copy localised name -->
       
   357 				<xsl:with-param name="other" select="$other"/>
       
   358 			</xsl:apply-templates>
       
   359 		</xsl:copy>
       
   360 	</xsl:for-each>	
       
   361 </xsl:template>
       
   362 <!-- can only merge a component or module with component or module -->
       
   363 <xsl:template match="component[@rename or @move] | collection[@rename or @move] | module[@rename or @move]" mode="safe-combine" priority="4"><xsl:param name="main-root"/>
       
   364 	<xsl:variable name="n" select="@rename | @move"/>
       
   365 	<xsl:variable name="newname" select="@name"/>
       
   366 	<xsl:variable name="atts" select="@*[name()!='name' and name()!='move' and name()!='rename' and name()!='override']"/> <!-- if any non-oragnisational attributes are present on the "move" item, then they should override the values in the thing being moved -->
       
   367 	<xsl:variable name="tag" select="name()"/>
       
   368 	<xsl:variable name="other" select="ancestor-or-self::sysdef"/>	
       
   369 	<!-- can't merge a component with a container -->
       
   370 	<xsl:for-each select="$main-root/descendant::*[@name=$n and $tag=name()]">
       
   371 		<xsl:copy>
       
   372 			<xsl:copy-of select="@*"/>
       
   373 			<xsl:copy-of select="$newname"/>	
       
   374 			<xsl:copy-of select="$atts"/> <!-- if any of these attributes are present on the moved collection, then use that value rather than the value in the one being moved -->
       
   375 			<xsl:apply-templates select="*[not(self::name)]"  mode="override-merge"> <!-- do not copy localised name -->
       
   376 				<xsl:with-param name="other" select="$other"/>
       
   377 			</xsl:apply-templates>
       
   378 		</xsl:copy>
       
   379 	</xsl:for-each>	
       
   380 </xsl:template>
       
   381 
       
   382 
       
   383 <xsl:template match="*[@name]" mode="safe-combine" priority="2"><xsl:param name="main-root"/>
       
   384 	<xsl:if test="@override and (@rename or @move)">
       
   385 		<xsl:call-template name="Critical-Error"><xsl:with-param name="text">Error invalid combinations of attributes in <xsl:value-of select="@name"/></xsl:with-param></xsl:call-template>
       
   386 	</xsl:if>
       
   387 	<xsl:copy>
       
   388 		<xsl:variable name="cur" select="."/>
       
   389 		<xsl:copy-of select="@*[not(name()='override' or name()='rename' or name()='move')]"/>
       
   390 		<!-- copy other's attributes -->
       
   391 		<xsl:copy-of select="$main-root/descendant::*[@name=$cur/@name and name($cur)=name()]/@*[not(name()='override' or name()='rename' or name()='move' or name()='name')]"/>		
       
   392 		<xsl:apply-templates select="*"  mode="safe-combine">
       
   393 			<xsl:with-param name="main-root" select="$main-root"/>
       
   394 		</xsl:apply-templates>
       
   395 		<!-- now copy anything defined here -->
       
   396 		<xsl:variable name="other" select="ancestor-or-self::sysdef"/>	
       
   397 		<xsl:for-each select="$main-root/descendant::*[@name=$cur/@name and name($cur)=name()]">
       
   398 			<xsl:apply-templates select="*[not(self::name)]"  mode="override-merge"> <!-- do not copy localised name -->
       
   399 				<xsl:with-param name="other" select="$other"/>
       
   400 			</xsl:apply-templates>
       
   401 		</xsl:for-each>	
       
   402 	</xsl:copy>
       
   403 </xsl:template>
       
   404 
       
   405 <xsl:template match="component | component/*" mode="safe-combine" priority="2">
       
   406 	<!--a component is atomic: it cannot be merged. So it's always just copied.-->	
       
   407 	<xsl:copy-of select="."/>
       
   408 </xsl:template>
       
   409 
       
   410 
       
   411 <xsl:template match="*[@override and not(@name)]" mode="safe-combine" priority="5"/> <!-- ignore component -->
       
   412 
       
   413 
       
   414 <xsl:template match="*" mode="unused-merge"><xsl:param name="other"/>
       
   415 	<xsl:variable name="self" select="."/>
       
   416 	<xsl:if test="not($other/descendant::*[@name=$self/@name and name()=name($self)  and descendant-or-self::component])">
       
   417 		<!-- not in other, so include here -->
       
   418 		<xsl:copy>
       
   419 			<xsl:copy-of select="@*"/>
       
   420 			<xsl:apply-templates select="*"  mode="unused-merge">
       
   421 				<xsl:with-param name="other" select="$other"/>
       
   422 			</xsl:apply-templates>
       
   423 		</xsl:copy>
       
   424 	</xsl:if>
       
   425 </xsl:template>
       
   426 
       
   427 <xsl:template match="*[@name]" mode="append"><xsl:param name="main"/>
       
   428 	<!--  this handles all cases where this holds the data -->
       
   429 	<xsl:variable name="self" select="."/>
       
   430 		<!-- if $self is not in main, use this -->
       
   431 	<xsl:if test="not($main/*[@name=$self/@name])">
       
   432 		<xsl:variable name="other" select="ancestor-or-self::sysdef"/>
       
   433 		<xsl:for-each select="$main/ancestor-or-self::sysdef/descendant::*[@name=$self/@name and name()=name($self)  and descendant-or-self::component]">
       
   434 			<xsl:call-template name="Caller-Debug"><xsl:with-param name="text">To Merge <xsl:value-of select="$self/@name"/> with these: <xsl:for-each select="*">"<xsl:value-of select="@name"/>", </xsl:for-each></xsl:with-param></xsl:call-template>	
       
   435 			<xsl:copy>
       
   436 				<xsl:copy-of select="@*"/>
       
   437 				<xsl:apply-templates select="*"  mode="override-merge">
       
   438 					<xsl:with-param name="other" select="$other"/>
       
   439 				</xsl:apply-templates>
       
   440 	<xsl:comment>was not here</xsl:comment>
       
   441 				<xsl:apply-templates select="$other/descendant::*[@name=$self/@name and name()=name($self) and descendant-or-self::component]/*"  mode="unused-merge">
       
   442 					<xsl:with-param name="other" select="$main"/>
       
   443 				</xsl:apply-templates>
       
   444 	<xsl:comment>end not here</xsl:comment>
       
   445 			</xsl:copy>
       
   446 		</xsl:for-each>
       
   447 		<!-- if it does not already exist, create it and check all children -->
       
   448 		<xsl:if test="not($main/ancestor-or-self::sysdef/descendant::*[@name=$self/@name and name()=name($self) and descendant-or-self::component])">
       
   449 			<xsl:call-template name="Caller-Debug"><xsl:with-param name="text">Does not already exist: <xsl:value-of select="$self/@name"/> </xsl:with-param></xsl:call-template>	
       
   450 			<xsl:copy>
       
   451 				<xsl:copy-of select="@*"/>
       
   452 				<xsl:apply-templates select="*"  mode="safe-combine">
       
   453 					<xsl:with-param name="main-root" select="$main/ancestor-or-self::sysdef"/>
       
   454 				</xsl:apply-templates>
       
   455 			</xsl:copy>			
       
   456 		</xsl:if>
       
   457 	</xsl:if>
       
   458 </xsl:template>
       
   459 
       
   460 <xsl:template match="component[@name]" mode="append" priority="1"><xsl:param name="main"/>
       
   461 	<xsl:variable name="n" select="@name"/>
       
   462 		<!-- if $n is not in main, use this -->
       
   463 	<xsl:if test="not($main/*[@name=$n])">	
       
   464 		<xsl:copy-of select="."/>
       
   465 	</xsl:if>
       
   466 </xsl:template>
       
   467 
       
   468 
       
   469 <xsl:template match="*[@rename or @move]" mode="append" priority="4"><xsl:param name="main"/>
       
   470 	<xsl:call-template name="Caller-Debug"><xsl:with-param name="text">Append (4) <xsl:value-of select="@name"/></xsl:with-param></xsl:call-template>	
       
   471 	<xsl:variable name="n" select="@rename | @move"/>
       
   472 		<!-- if $n is not in main, use this -->
       
   473 	<xsl:if test="not($main/*[@name=$n])">	
       
   474 		<xsl:apply-templates select="." mode="safe-combine">
       
   475 			<xsl:with-param name="main-root" select="$main/ancestor-or-self::sysdef"/>
       
   476 		</xsl:apply-templates>	
       
   477 	</xsl:if>
       
   478 </xsl:template>
       
   479 
       
   480 <!-- merging data sources  -->
       
   481 <xsl:template match="sysdef" mode="merge">
       
   482 	<xsl:apply-templates select="document(@href)/SystemDefinition/systemModel/*" mode="merge">
       
   483 		<xsl:with-param name="extra-files" select="info|../@deps|../@ts|../layout/info[@type and document(@href,.)/values]|../layout/display|@root"/>
       
   484 	</xsl:apply-templates>
       
   485 </xsl:template>
       
   486 
       
   487 
       
   488 <xsl:template match="SystemModelDeps" mode="merge">
       
   489 	<xsl:if test="@name or @version or @number">
       
   490 		<release><xsl:copy-of select="@name | @version | @number"/></release>
       
   491 	</xsl:if>
       
   492 </xsl:template>
       
   493 
       
   494 <!-- merge model with extra -->
       
   495 
       
   496 <xsl:template match="layer" mode="merge"><xsl:param name="extra-files"/>
       
   497 	<xsl:variable name="e"><xsl:call-template name="is-present"/></xsl:variable>	
       
   498 	<xsl:if test="$e!=''">
       
   499 		<xsl:copy><xsl:copy-of select="@*"/> 
       
   500 			<xsl:variable name="name" select="@name"/>
       
   501 			<xsl:copy-of select="$extra-files[self::display and @name=$name]/@*[name()!='name']"/>
       
   502 			<xsl:if test="$extra-files[@type='levels']"><!-- levels hack for older models -->
       
   503 				<xsl:copy-of select="document($extra-files[@type='levels']/@href,$extra-files)//layer[@name=$name]/@*[name()='levels' or name()='span']"/>
       
   504 			</xsl:if>
       
   505 			<xsl:apply-templates select="$extra-files[@type='extra']" mode="merge-attributes">
       
   506 				<xsl:with-param name="cmp" select="."/>			
       
   507 			</xsl:apply-templates>
       
   508 			<xsl:if test="@name='Programming Support' and not(@span)"><xsl:attribute name="span">2</xsl:attribute></xsl:if> <!-- hack!!!!! -->
       
   509 			<xsl:call-template name="abbrev"/>
       
   510 		 	<xsl:apply-templates select="*" mode="merge">
       
   511 				<xsl:with-param name="extra-files" select="$extra-files"/>
       
   512 			</xsl:apply-templates>
       
   513 		</xsl:copy>
       
   514 	</xsl:if>
       
   515 </xsl:template>
       
   516 
       
   517 
       
   518 <!-- hack for old models -->
       
   519 <xsl:template match="logicalsubset[@name='J2ME']" mode="merge" priority="5"><xsl:param name="extra-files"/>
       
   520 	<xsl:apply-templates select="*" mode="merge">
       
   521 		<xsl:with-param name="extra-files" select="$extra-files"/>
       
   522 	</xsl:apply-templates>
       
   523 </xsl:template>
       
   524 
       
   525 <!-- update old model names, these could be done as a set of templates instead.
       
   526 	I've no idea which is more efficient -->
       
   527 <xsl:template match="*" mode="item-tag">
       
   528 	<xsl:choose>
       
   529 		<xsl:when test="self::module">collection</xsl:when>
       
   530 		<xsl:when test="self::logicalset">block</xsl:when>
       
   531 		<xsl:when test="self::logicalsubset">subblock</xsl:when>
       
   532 		<xsl:otherwise><xsl:value-of select="name()"/></xsl:otherwise>
       
   533 	</xsl:choose>
       
   534 </xsl:template>
       
   535 
       
   536 <xsl:template match="*" mode="alt-tag"> <!-- for compatability-->
       
   537 	<xsl:choose>
       
   538 		<xsl:when test="self::collection">module</xsl:when>
       
   539 		<xsl:when test="self::block">logicalset</xsl:when>
       
   540 		<xsl:when test="self::subblock">logicalsubset</xsl:when>
       
   541 		<xsl:otherwise><xsl:value-of select="name()"/></xsl:otherwise>
       
   542 	</xsl:choose>
       
   543 </xsl:template>
       
   544 
       
   545 <xsl:template match="collection|block|subblock | module|logicalset|logicalsubset" mode="merge"><xsl:param name="extra-files"/>
       
   546 	<xsl:variable name="e"><xsl:call-template name="is-present"/></xsl:variable>
       
   547 	<xsl:if test="$e!=''">	
       
   548 		<xsl:variable name="tag"><xsl:apply-templates mode="item-tag" select="."/></xsl:variable>
       
   549 		<xsl:variable name="alt-tag"><xsl:apply-templates mode="alt-tag" select="."/></xsl:variable>
       
   550 		<xsl:element name="{$tag}">
       
   551 			<xsl:copy-of select="@*"/>
       
   552 			<xsl:variable name="name" select="@name"/>
       
   553 			<xsl:copy-of select="$extra-files[self::display and @name=$name]/@*[name()!='name']"/>		
       
   554 			<xsl:if test="not(self::logicalsubset or self::subblock) and $extra-files[@type='levels']">
       
   555 				<xsl:copy-of select="document($extra-files[@type='levels']/@href,$extra-files)//*[@name=$name and (name()=$tag or name()=$alt-tag)]/@*[starts-with(name(),'level') or name()='span']"/>
       
   556 			</xsl:if>
       
   557 			<xsl:apply-templates select="$extra-files[@type='extra']" mode="merge-attributes">
       
   558 				<xsl:with-param name="cmp" select="."/>			
       
   559 			</xsl:apply-templates>			
       
   560 			<xsl:call-template name="abbrev"/>	
       
   561 			<xsl:apply-templates select="*" mode="merge">
       
   562 				<xsl:with-param name="extra-files" select="$extra-files"/>
       
   563 			</xsl:apply-templates>	  
       
   564 		</xsl:element>
       
   565 	</xsl:if>
       
   566 </xsl:template>
       
   567 
       
   568 <!-- sched 12 -->
       
   569 
       
   570 <xsl:template match="Schedule12/*[@name]" mode="merge">
       
   571 	<s12>
       
   572 		<!-- allow multiple s12 in a component so shapes can pick the right one for the supplied version-->
       
   573 		<xsl:attribute name="ver"><xsl:value-of select="/Schedule12/@OS_version"/></xsl:attribute>
       
   574 		<xsl:apply-templates select="." mode="osd"/>
       
   575 		<xsl:copy-of select="@name"/>
       
   576 	</s12>
       
   577 </xsl:template>
       
   578 
       
   579 <xsl:template match="*" mode="osd"> <!-- CR, OS, OR, CS or anything new -->
       
   580 	<xsl:attribute name="osd"><xsl:value-of select="name()"/></xsl:attribute>
       
   581 </xsl:template>
       
   582 
       
   583 <xsl:template match="REF" mode="osd">
       
   584 	<xsl:attribute name="osd">T-R</xsl:attribute>
       
   585 	<xsl:attribute name="ref">true</xsl:attribute>
       
   586 </xsl:template>
       
   587 
       
   588 <xsl:template match="TEST|RT" mode="osd">
       
   589 	<xsl:attribute name="osd">T-R</xsl:attribute>
       
   590 </xsl:template>
       
   591 
       
   592 <!--/sched 12 -->
       
   593 
       
   594 <!-- filters -->
       
   595 
       
   596 
       
   597 
       
   598 <xsl:template name="is-present">
       
   599 	<!-- use this for filtering if we need to use simple filtering
       
   600 		only necessary if we can't use node-set filters -->
       
   601 	<xsl:choose>
       
   602 		<xsl:when test="not($simple-filters)">1</xsl:when>
       
   603 		<xsl:otherwise>
       
   604 			<xsl:variable name="show">
       
   605 				<xsl:apply-templates select="." mode="filter-value"/>
       
   606 			</xsl:variable>
       
   607 			<!-- filter accept="..." takes precedence over "rich" filters, so if the rich filter
       
   608 				says the item is present, then we still need to check to see if it should be
       
   609 				shown via the mode="present" template	-->
       
   610 			<xsl:if test="$show='' or $show='show'">
       
   611 				<xsl:apply-templates select="." mode="present"/>
       
   612 			</xsl:if>
       
   613 		</xsl:otherwise>
       
   614 	</xsl:choose>
       
   615 </xsl:template>
       
   616 
       
   617 <xsl:template match="component" mode="present" priority="8">
       
   618 	<xsl:variable name="e"><xsl:apply-templates select="unit|package|prebuilt" mode="present"/></xsl:variable>
       
   619 	<xsl:if test="$e!='' or not(unit|package|prebuilt)">1</xsl:if>
       
   620 </xsl:template>
       
   621 
       
   622 <xsl:template match="layer|block|subblock|collection | module|logicalset|logicalsubset" mode="present" priority="9">
       
   623 	<xsl:variable name="t" select="name()"/><xsl:variable name="n" select="@name"/>
       
   624 	<xsl:variable name="alt"><xsl:apply-templates select="." mode="alt-tag"/></xsl:variable>
       
   625 	<xsl:if test="not($ignore[(@type=$t or @type=$alt) and @name=$n])"> <!-- matches either e.g. module or colelction -->
       
   626 		<xsl:apply-templates select="descendant::component" mode="present"/>
       
   627 	</xsl:if>
       
   628 </xsl:template>
       
   629 
       
   630 <xsl:template match="component[@filter]" mode="present" priority="9">
       
   631 	<!-- if there's a filter then check to see if this is filtered out -->
       
   632 	<xsl:variable name="e0"><xsl:call-template name="present"/></xsl:variable>
       
   633 	<xsl:if test="$e0!=''">
       
   634 		<xsl:variable name="e1"><xsl:apply-templates select="unit|package|prebuilt" mode="present"/></xsl:variable>
       
   635 		<xsl:if test="$e1!=''  or not(unit|package|prebuilt)">1</xsl:if>
       
   636 	</xsl:if>
       
   637 </xsl:template>
       
   638 
       
   639 <xsl:template match="*[not(@filter)]" mode="present" priority="5">1</xsl:template> <!-- if no filter, must always be valid -->
       
   640 <xsl:template match="*[@filter]" mode="present"><xsl:call-template name="present"/></xsl:template>
       
   641 
       
   642 <xsl:template match="*[contains(@filter,',')]" mode="present">	
       
   643 	<!-- if the number of comma-separated items is the same as the number of items present, then this is present (ie we're ANDing the items)-->
       
   644 	<xsl:variable name="present"><xsl:call-template name="present-list"/></xsl:variable>
       
   645 	<xsl:if test="string-length(@filter) - string-length(translate(@filter,',','')) + 1 = string-length($present)">1</xsl:if>
       
   646 </xsl:template>	
       
   647 
       
   648 <xsl:template name="present-list"><xsl:param name="filter" select="@filter"/>
       
   649 	<xsl:call-template name="present">
       
   650 		<xsl:with-param name="filter">	<xsl:choose>
       
   651 			<xsl:when test="contains($filter,',')"><xsl:value-of select="substring-before($filter,',')"/></xsl:when>
       
   652 			<xsl:otherwise><xsl:value-of select="$filter"/></xsl:otherwise>
       
   653 		</xsl:choose></xsl:with-param>
       
   654 	</xsl:call-template>
       
   655 	<xsl:if test="contains($filter,',')"><xsl:call-template name="present-list">
       
   656 		<xsl:with-param name="filter" select="substring-after($filter,',')"/>
       
   657 	</xsl:call-template></xsl:if>
       
   658 </xsl:template>
       
   659 
       
   660 <xsl:template name="present"><xsl:param name="filter" select="@filter"/>
       
   661 	<xsl:choose>
       
   662 		<xsl:when test="$filter-in=',' and $filter-nodes">1</xsl:when> <!-- accept everything -->
       
   663 		<xsl:when test="contains($filter-in,concat(',',$filter,','))">1</xsl:when> <!-- accept anything explictly on accept list -->
       
   664 		<xsl:when test="starts-with($filter,'!') and contains($filter-in,concat(',',substring($filter,2),','))"/> <!--reject if expliftly to accept a something with a "not" -->
       
   665 		<xsl:when test="starts-with($filter,'!')">1</xsl:when> <!--it's not on the must-have list, so accept it -->
       
   666 		<!-- reject otherwise -->
       
   667 	</xsl:choose>
       
   668 </xsl:template>
       
   669 <!-- node-set filters -->
       
   670 
       
   671 <xsl:template mode="filter-for-presence" match="comment() | node()" priority="-1"><xsl:copy-of select="."/></xsl:template>
       
   672 
       
   673 <xsl:template mode="filter-for-presence" match="layer|block|subblock|collection | logicalset|logicalsubset|module">
       
   674 	<xsl:variable name="t" select="name()"/>
       
   675 	<xsl:variable name="alt"><xsl:apply-templates select="." mode="alt-tag"/></xsl:variable>	<!-- for bkward compat -->
       
   676 	<xsl:variable name="n" select="@name"/>
       
   677 	<xsl:if test="not($ignore[(@type=$t or @type=$alt) and @name=$n])">
       
   678 		<xsl:variable name="content">
       
   679 			<xsl:apply-templates  select="comment() | *" mode="filter-for-presence"/>
       
   680 		</xsl:variable>
       
   681 		<xsl:if test="exslt:node-set($content)/*">
       
   682 			<xsl:copy><xsl:copy-of select="@*"/>
       
   683 				<xsl:copy-of select="$content"/>
       
   684 			</xsl:copy>
       
   685 		</xsl:if>
       
   686 	</xsl:if>
       
   687 </xsl:template>
       
   688 
       
   689 <xsl:template match="filter" mode="filter-values"><xsl:param name="c"/>
       
   690 	<xsl:variable name="att" select="@select"/>
       
   691 	<xsl:choose>
       
   692 		<xsl:when test="not($c[self::component])"/>
       
   693 		<xsl:when test="@select='*'"> <!-- always matches -->
       
   694 				<xsl:value-of select="concat(' ',@display)"/>
       
   695 		</xsl:when>
       
   696 		<xsl:when test="not($c/@*[name()=$att] or $c/*[self::unit or self::package or self::prebuilt]/@*[name()=$att])"/> <!-- this filter does not match this component-->
       
   697 		<xsl:when test="not(@value) or @value='*'">	<!-- true if just checking for the presence of the attribute on the item -->
       
   698 				<xsl:value-of select="concat(' ',@display)"/>
       
   699 		</xsl:when>
       
   700 		<xsl:when test="@select='filter'">
       
   701 			<!--  @value must be in the comma-separated list of filter on this or child-->
       
   702 			<xsl:if test="contains(concat(',',$c/@filter,',',$c/*/@filter,','),concat(',',@value,','))">
       
   703 				<xsl:value-of select="concat(' ',@display)"/>
       
   704 			</xsl:if>
       
   705 		</xsl:when>
       
   706 		<xsl:when test="@select='class'">
       
   707 			<!--  @value must be in the whitespace-separated list -->
       
   708 			<xsl:if test="contains(concat(' ',normalize-space($c/@class),' '),concat(' ',@value,' '))">
       
   709 				<xsl:value-of select="concat(' ',@display)"/>
       
   710 			</xsl:if>
       
   711 		</xsl:when>
       
   712 		<!--  any other attribute must match exactly on the component -->
       
   713 		<xsl:when test="@value= $c/@*[name()=$att]">
       
   714 				<xsl:value-of select="concat(' ',@display)"/>
       
   715 		</xsl:when>
       
   716 		<!--  other attributes on unit children must also match exactly -->
       
   717 		<xsl:when test="@value= $c/*/@*[name()=$att]">
       
   718 				<xsl:value-of select="concat(' ',@display)"/>
       
   719 		</xsl:when>
       
   720 	</xsl:choose>	
       
   721 </xsl:template>
       
   722 
       
   723 
       
   724 <xsl:template match="*" mode="filter-value"/> <!-- no filter value unless otherwise specified -->
       
   725 <xsl:template match="component" priority="1"  mode="filter-value"> <!-- only components can have "rich" filters, though some attributes can apply to units-->
       
   726 	<xsl:variable name="this" select="."/>
       
   727 	<xsl:call-template name="last-in-list">
       
   728 		<xsl:with-param name="str">
       
   729 			<xsl:apply-templates select="$filter-nodes" mode="filter-values">
       
   730 				<xsl:with-param name="c" select="$this"/>
       
   731 			</xsl:apply-templates>
       
   732 		</xsl:with-param>
       
   733 	</xsl:call-template>
       
   734 </xsl:template>
       
   735 
       
   736 <!-- this will add a display="..." attribute to the item for any display value other than
       
   737 	'hide' (which does not include the item in the output) and
       
   738 	'show' (which includes the item, but the attribtue is not needed, since it should appear like any other item) -->
       
   739 <xsl:template mode="filter-for-presence" match="component|unit|package|prebuilt" priority="1">
       
   740 	<xsl:variable name="show">
       
   741 		<xsl:apply-templates select="." mode="filter-value"/>
       
   742 	</xsl:variable>
       
   743 	<xsl:if test="$show='' or $show!='hide'">	
       
   744 		<xsl:variable name="this" select="."/>
       
   745 		<xsl:if test="not($ignore[@type=name($this) and $this/@name=@name])"> 	<!-- hide component if specifically told to ignore -->
       
   746 			<xsl:variable name="e"><xsl:apply-templates select="." mode="present"/></xsl:variable>
       
   747 			<!-- show component if any CBRs survive the filters or this is a placeholder with no content -->
       
   748 			<xsl:choose>
       
   749 				<xsl:when test="$e='' "/> <!-- filtered out, don't show -->
       
   750 				<xsl:when test="self::component">
       
   751 					<xsl:variable name="content">
       
   752 						<xsl:apply-templates  select="comment() | *" mode="filter-for-presence"/>
       
   753 					</xsl:variable>
       
   754 					<xsl:if test="exslt:node-set($content)/* or not(*)"> <!-- show all empty components -->
       
   755 						<xsl:copy><xsl:copy-of select="@*"/>
       
   756 							<xsl:if test="$show!='' and $show!='show'"><xsl:attribute name="display"><xsl:value-of select="$show"/></xsl:attribute></xsl:if>
       
   757 							<xsl:copy-of select="$content"/>
       
   758 						</xsl:copy>
       
   759 					</xsl:if>
       
   760 				</xsl:when>
       
   761 				<xsl:otherwise><xsl:copy-of select="."/></xsl:otherwise>
       
   762 			</xsl:choose>
       
   763 		</xsl:if>
       
   764 	</xsl:if>
       
   765 </xsl:template>
       
   766 
       
   767 <!-- /filters -->
       
   768 
       
   769 <!-- ======= rules to merge various aux data ============== -->
       
   770 <xsl:template match="*" mode="merge-attributes"/>
       
   771 <xsl:template match="*" mode="merge-content"/>
       
   772 
       
   773 
       
   774 <xsl:template match="/values" mode="merge-attributes"><xsl:param name="cmp"/><xsl:param name="base"/>
       
   775 	<xsl:if test="$cmp[self::component]"> <!-- only valid for components -->
       
   776 		<xsl:if test="$base[@type!='style' and @type!='color']"> <!-- style and colour can have any number of values, so it needs to be captured in an element -->
       
   777 			<xsl:variable name="join"><xsl:if test="$base/@type!=''">-</xsl:if></xsl:variable>
       
   778 			<xsl:variable name="value" select="key('named',$cmp/@name)[self::component]/.."/>
       
   779 			<xsl:if test="$value/@value">
       
   780 				<xsl:attribute name="generator{$join}{$base/@type}">
       
   781 					<xsl:apply-templates select="$value" mode="style-id">
       
   782 						<xsl:with-param select="$base" name="base"/>
       
   783 					</xsl:apply-templates>
       
   784 				</xsl:attribute>
       
   785 			</xsl:if>
       
   786 		</xsl:if>
       
   787 	</xsl:if>
       
   788 </xsl:template>
       
   789 
       
   790 <xsl:template match="/values" mode="merge-content"><xsl:param name="cmp"/><xsl:param name="base"/>
       
   791 	<xsl:if test="$cmp[self::component]"> <!-- only valid for components -->
       
   792 		<xsl:if test="$base[@type='style' or @type='color']"> <!-- at this point, only style and color can have multiple values -->
       
   793 			<xsl:variable name="join"><xsl:if test="$base/@type!=''">-</xsl:if></xsl:variable>
       
   794 			<xsl:variable name="value" select="key('named',$cmp/@name)[self::component]/.."/>
       
   795 			<xsl:choose>
       
   796 				<xsl:when test="$value">
       
   797 					<xsl:for-each select="$value">
       
   798 						<xsl:element name="generator{$join}{$base/@type}">
       
   799 						 	<xsl:attribute name="ref">
       
   800 								<xsl:apply-templates select="." mode="style-id">
       
   801 									<xsl:with-param select="$base" name="base"/>
       
   802 								</xsl:apply-templates>
       
   803 							</xsl:attribute>
       
   804 						</xsl:element>
       
   805 					</xsl:for-each>
       
   806 				</xsl:when>
       
   807 				<xsl:when test="@default">
       
   808 					<xsl:element name="generator{$join}{$base/@type}">
       
   809 					 	<xsl:attribute name="ref">
       
   810 							<xsl:apply-templates select="$base" mode="style-id"/>
       
   811 						</xsl:attribute>
       
   812 					</xsl:element>
       
   813 				</xsl:when>
       
   814 			</xsl:choose>
       
   815 		</xsl:if>
       
   816 	</xsl:if>
       
   817 </xsl:template>
       
   818 
       
   819 
       
   820 <!-- unsupported proprietary format -->
       
   821 <xsl:template match="/techstreams" mode="merge-attributes"><xsl:param name="cmp"/>
       
   822 	<xsl:if test="$cmp[self::component]"> <!-- only valid for components -->
       
   823 		<xsl:attribute name="ts"><xsl:value-of select="key('named',$cmp/@name)[name()='component' and not(@type)]/../@name"/></xsl:attribute>
       
   824 	</xsl:if>
       
   825 </xsl:template>
       
   826 
       
   827 <xsl:template match="/techstreams" mode="merge-content"><xsl:param name="cmp"/>
       
   828 	<xsl:if test="$cmp[self::component]"> <!-- only valid for components -->
       
   829 		<xsl:for-each select="key('named',$cmp/@name)[name()='component']">
       
   830 			<xsl:for-each select="self::component[not(@type)]/ancestor::group">
       
   831 				<ts-group><xsl:copy-of select="@*"/></ts-group>
       
   832 			</xsl:for-each>	
       
   833 			<xsl:for-each select="owners">
       
   834 				<xsl:copy><xsl:copy-of select="@*"/>
       
   835 					<xsl:if test="@type"><xsl:attribute name="ts"><xsl:value-of select="../../@name"/></xsl:attribute></xsl:if>
       
   836 				<xsl:copy-of select="*"/>
       
   837 				</xsl:copy>
       
   838 			</xsl:for-each>	
       
   839 		</xsl:for-each>	
       
   840 	</xsl:if>
       
   841 </xsl:template>
       
   842 
       
   843 <!-- unsupported proprietary format -->
       
   844 <xsl:template match="/attributes" mode="merge-attributes"><xsl:param name="cmp"/>
       
   845 	<xsl:for-each select="key('named',$cmp/ancestor::layer/@name)[self::layer and @inherit='yes']">
       
   846 		<xsl:copy-of select="attrs/@*"/>
       
   847 	</xsl:for-each>
       
   848 	<xsl:for-each select="key('named',$cmp/ancestor::block/@name)[self::block and @inherit='yes']">
       
   849 		<xsl:copy-of select="attrs/@*"/>
       
   850 	</xsl:for-each>
       
   851 	<xsl:for-each select="key('named',$cmp/ancestor::subblock/@name)[self::subblock and @inherit='yes']">
       
   852 		<xsl:copy-of select="attrs/@*"/>
       
   853 	</xsl:for-each>
       
   854 	<xsl:for-each select="key('named',$cmp/ancestor::collection/@name)[self::coll and @inherit='yes']">
       
   855 		<xsl:copy-of select="attrs/@*"/>
       
   856 	</xsl:for-each>
       
   857 	<xsl:for-each select="key('named',$cmp/@name)[starts-with(local-name($cmp),local-name())]">
       
   858 		<xsl:if test="not(@location) or (@location=$cmp/../@name)">
       
   859 			<xsl:copy-of select="attrs/@*"/>
       
   860 		</xsl:if>
       
   861 	</xsl:for-each>
       
   862 </xsl:template>
       
   863 <xsl:template match="/attributes" mode="merge-content"/>
       
   864 
       
   865 
       
   866 
       
   867 <xsl:template match="/model" mode="merge-attributes"><xsl:param name="cmp"/>
       
   868 	<xsl:if test="$cmp[self::component]"> <!-- only valid for components -->
       
   869 		<!-- only use location if there is ambiguity -->
       
   870 		<xsl:variable name="loc">
       
   871 			<xsl:variable name="name" select="$cmp/@name"/>
       
   872 			<xsl:if test="count($cmp/ancestor::systemModel//component[@name=$name])!=1">
       
   873 				<xsl:for-each select="$cmp"><xsl:call-template name="location"/></xsl:for-each>
       
   874 			</xsl:if>
       
   875 		</xsl:variable>
       
   876 	<!--	<xsl:copy-of select="key('named',$cmp/@name)[name()='c' and ($loc='' or location=$loc)]/@*"/>-->
       
   877 		<xsl:for-each select="key('named',$cmp/@name)[name()='c' and ($loc='' or location=$loc)]/@*">
       
   878 			<xsl:copy-of select="."/>
       
   879 			<xsl:if test="name()='introduced'">   <!-- keep this while still in beta -->
       
   880 				<xsl:attribute name="since"><xsl:value-of select="."/></xsl:attribute>
       
   881 			</xsl:if>
       
   882 		</xsl:for-each>
       
   883 	</xsl:if>
       
   884 </xsl:template>
       
   885 
       
   886 <xsl:template match="/model" mode="merge-content"><xsl:param name="cmp"/>
       
   887 	<xsl:if test="$cmp[self::component]"> <!-- only valid for components -->
       
   888 		<xsl:variable name="loc">
       
   889 			<xsl:variable name="name" select="$cmp/@name"/>
       
   890 			<xsl:if test="count($cmp/ancestor::systemModel//component[@name=$name])!=1">
       
   891 				<xsl:for-each select="$cmp"><xsl:call-template name="location"/></xsl:for-each>
       
   892 			</xsl:if>
       
   893 		</xsl:variable>
       
   894 		<xsl:for-each select="key('named',$cmp/@name)[name()='c' and ($loc='' or location=$loc)]">
       
   895 			<xsl:copy-of select="*[name()!='location' and name()!='bld' and name()!='ts']"/>
       
   896 			<xsl:apply-templates select="bld" mode="merge"/>
       
   897 		</xsl:for-each>
       
   898 	</xsl:if>
       
   899 </xsl:template>
       
   900 	
       
   901 
       
   902 <xsl:template match="/SQL" mode="merge-content"><xsl:param name="cmp"/>
       
   903 	<xsl:if test="$cmp[self::component]"> <!-- only valid for components -->
       
   904 		<xsl:variable name="loc"><xsl:for-each select="$cmp"><xsl:call-template name="location"/></xsl:for-each></xsl:variable>
       
   905 		<xsl:for-each select="//ROW[translate(component,'&AZ;','&az;')=translate($cmp/name,'&AZ;','&az;')]">
       
   906 			<xsl:variable name="match">
       
   907 				<xsl:if test="layer"><xsl:value-of select="layer"/>/</xsl:if>
       
   908 				<xsl:if test="layer"><xsl:value-of select="block"/>/</xsl:if>
       
   909 				<xsl:if test="layer"><xsl:value-of select="subblock"/>/</xsl:if>
       
   910 				<xsl:if test="layer"><xsl:value-of select="collection"/>/</xsl:if>
       
   911 			</xsl:variable>
       
   912 			<xsl:if test="$match = concat($loc,'/') or 1 "><xsl:apply-templates mode="sql" select="*"/></xsl:if>
       
   913 		</xsl:for-each>
       
   914 	</xsl:if>
       
   915 </xsl:template>
       
   916 
       
   917 <xsl:template mode="sql" match="*"><xsl:copy-of select="."/></xsl:template>
       
   918 <xsl:template mode="sql" match="layer|block|subblock|collection|component"/>
       
   919 
       
   920 
       
   921 <xsl:template match="/Schedule12" mode="merge-content"><xsl:param name="cmp"/>
       
   922 	<xsl:if test="$cmp[self::component]"> <!-- only valid for components -->
       
   923 		<xsl:variable name="match">
       
   924 			<xsl:call-template name="merge-Schedule12">
       
   925 				<xsl:with-param name="cmp" select="$cmp"/>
       
   926 			</xsl:call-template>
       
   927 		</xsl:variable>
       
   928 		<xsl:if test="$match='' ">
       
   929 			<xsl:call-template name="Caller-Warning"><xsl:with-param name="text"><xsl:value-of select="$cmp/@name"/> not in Schedule 12</xsl:with-param></xsl:call-template>
       
   930 		</xsl:if>
       
   931 		<xsl:if test="$match!='' ">
       
   932 			<xsl:apply-templates select="//system_model[$match=@entry]/.." mode="merge"/>
       
   933 		</xsl:if>
       
   934 	</xsl:if>
       
   935 </xsl:template>
       
   936 
       
   937 <xsl:template name="merge-Schedule12"><xsl:param name="path"/><xsl:param name="cmp"/>
       
   938 	<xsl:choose>
       
   939 	<!--  match this, then try matching parents -->
       
   940 		<xsl:when test="//system_model[@entry=$cmp/@name]">
       
   941 			<xsl:value-of select="$cmp/@name"/>
       
   942 		</xsl:when>
       
   943 		<xsl:when test="$cmp/parent::*[@name]">
       
   944 			<xsl:call-template name="merge-Schedule12">
       
   945 				<xsl:with-param name="cmp" select="$cmp/parent::*"/>
       
   946 			</xsl:call-template>			
       
   947 		</xsl:when>
       
   948 	</xsl:choose>
       
   949 </xsl:template>
       
   950 
       
   951 <xsl:template match="/LicensesFile" mode="merge-content"><xsl:param name="cmp"/>
       
   952 	<xsl:if test="$cmp[self::component]"> <!-- only valid for components -->
       
   953 		<xsl:for-each select="//Supplier/Contract">
       
   954 			<xsl:variable name="n" select="@name"/>
       
   955 			<xsl:variable name="s" select="../@name"/>
       
   956 			<xsl:if test="not(following-sibling::Contract[@name=$n])">
       
   957 			<xsl:for-each select="$cmp/*[@name]"> <!-- if there is somthing without a contract use Symbian_Default -->
       
   958 				<xsl:if test="(not(@contract) and $n='Symbian_Default') or @contract=$n"><supplier name="{$s}" for="{@name}"/></xsl:if>
       
   959 			</xsl:for-each>
       
   960 			</xsl:if>
       
   961 		</xsl:for-each>
       
   962 	</xsl:if>
       
   963 </xsl:template>
       
   964 
       
   965 
       
   966 <xsl:template match="/SystemModelDeps" mode="merge-content"><xsl:param name="cmp"/>
       
   967 	<xsl:if test="$cmp[self::component]"> <!-- only valid for components -->
       
   968 		<Build>
       
   969 			<xsl:variable name="name">
       
   970 				<xsl:choose>
       
   971 					<xsl:when test="starts-with(@component,'techview_')"><xsl:message/><xsl:value-of select="substring-after(@component,'techview_')"/></xsl:when>
       
   972 					<xsl:otherwise><xsl:value-of select="@component"/></xsl:otherwise>
       
   973 				</xsl:choose>
       
   974 			</xsl:variable>
       
   975 			<xsl:apply-templates select="//Executable[$cmp/@name=@component or ($cmp/*[contains(@filter,'techview')] and concat('techview_',$cmp/@name)=@component)]"/>
       
   976 		</Build>
       
   977 	</xsl:if>
       
   978 </xsl:template>
       
   979 <!-- /aux rules -->
       
   980 
       
   981 <xsl:template match="@root" mode="merge-attributes"/>
       
   982 <xsl:template match="@root" mode="merge-content"/>
       
   983 
       
   984 <xsl:template match="@*" mode="merge-attributes"><xsl:param name="cmp"/>
       
   985 	<xsl:apply-templates mode="merge-attributes" select="document(.,/)/*">
       
   986 		<xsl:with-param name="cmp" select="$cmp"/>		
       
   987 	</xsl:apply-templates>
       
   988 </xsl:template>
       
   989 
       
   990 <xsl:template match="info[@type!='symsym']" mode="merge-attributes"><xsl:param name="cmp"/> 
       
   991 	<!-- not called for symsym snice symsym only adds components -->
       
   992 	<xsl:apply-templates mode="merge-attributes" select="document(@href,/)/*">
       
   993 		<xsl:with-param name="cmp" select="$cmp"/>
       
   994 		<xsl:with-param name="base" select="."/>
       
   995 	</xsl:apply-templates>
       
   996 </xsl:template>
       
   997 
       
   998 <xsl:template match="@*" mode="merge-content"><xsl:param name="cmp"/>
       
   999 	<xsl:apply-templates mode="merge-content" select="document(.,/)/*">
       
  1000 		<xsl:with-param name="cmp" select="$cmp"/>
       
  1001 	</xsl:apply-templates>
       
  1002 </xsl:template>
       
  1003 
       
  1004 <xsl:template match="info" mode="merge-content"><xsl:param name="cmp"/>
       
  1005 	<xsl:apply-templates mode="merge-content" select="document(@href,/)/*">
       
  1006 		<xsl:with-param name="cmp" select="$cmp"/>		
       
  1007 		<xsl:with-param name="base" select="."/>
       
  1008 	</xsl:apply-templates>
       
  1009 </xsl:template>
       
  1010 
       
  1011 <xsl:template match="info[@type='symsym']" mode="merge-content"><xsl:param name="cmp"/>
       
  1012 	<xsl:variable name="filter" select="@dbfilter"/>
       
  1013 	<xsl:variable name="use" select="@use"/>
       
  1014 	<xsl:variable name="include" select="concat(',',@include,',')"/>
       
  1015 	<SQL>
       
  1016 		<xsl:for-each select="document(@href)/SQL/ROW/*[name()=$filter]">
       
  1017 			<xsl:variable name="name" select="translate(.,'&AZ;','&az;')"/>
       
  1018 			<xsl:if test="($use='mrp' and $cmp/*[translate(@name,'&AZ;','&az;')=$name]) or ($use='component' and translate($cmp/@name,'&AZ;','&az;')=$name)">
       
  1019 			 	<xsl:for-each select="..">
       
  1020 					<xsl:copy>
       
  1021 						<xsl:for-each select="*">
       
  1022 							<xsl:if test="$include=',,' or contains($include,concat(',',name(),','))"><xsl:copy-of select="."/></xsl:if>
       
  1023 						</xsl:for-each>
       
  1024 					</xsl:copy>
       
  1025 				</xsl:for-each>
       
  1026 			</xsl:if>
       
  1027 		</xsl:for-each>
       
  1028 	</SQL>
       
  1029 </xsl:template>
       
  1030 
       
  1031 <xsl:template match="component" mode="merge"><xsl:param name="extra-files"/>
       
  1032 	<xsl:variable name="e"><xsl:call-template name="is-present"/></xsl:variable>
       
  1033 	<xsl:if test="$e!=''">
       
  1034 		<xsl:copy><xsl:copy-of select="@*"/>
       
  1035 			<xsl:if test="$Origin-attr!='' and not(@*[local-name()=$Origin-attr]) and /SystemDefinition/@name!=''">
       
  1036 				<xsl:attribute name="{$Origin-attr}">	<!-- indicate which model this came from -->
       
  1037 					<xsl:value-of select="/SystemDefinition/@name"/>
       
  1038 				</xsl:attribute>
       
  1039 			</xsl:if>
       
  1040 			<xsl:apply-templates select="$extra-files" mode="merge-attributes">
       
  1041 				<xsl:with-param name="cmp" select="."/>			
       
  1042 			</xsl:apply-templates>
       
  1043 			<xsl:call-template name="abbrev"/>	
       
  1044 			<xsl:apply-templates select="$extra-files" mode="merge-content">
       
  1045 				<xsl:with-param name="cmp" select="."/>
       
  1046 			</xsl:apply-templates>
       
  1047 			<xsl:apply-templates select="*" mode="merge">
       
  1048 				<xsl:with-param name="extra-files" select="$extra-files"/>
       
  1049 			</xsl:apply-templates>	 
       
  1050 		</xsl:copy>
       
  1051 	</xsl:if>
       
  1052 </xsl:template>
       
  1053 
       
  1054 
       
  1055 <xsl:template match="component/*" mode="merge"><xsl:param name="extra-files"/>
       
  1056 <xsl:variable name="e"><xsl:call-template name="is-present"/></xsl:variable>
       
  1057 <xsl:if test="$e!=''">
       
  1058 		<xsl:copy><xsl:copy-of select="@*"/> <!-- only put root attribute on units with a path in them - does not make sense otherwise -->
       
  1059 		<xsl:if test="not(@root) and (@mrp or @bldFile)"><xsl:copy-of select="$extra-files[name()='root']"/></xsl:if>
       
  1060 		<xsl:copy-of select="node()"/>
       
  1061 	</xsl:copy>
       
  1062 	</xsl:if>
       
  1063 </xsl:template>
       
  1064 
       
  1065 <xsl:template name="abbrev">
       
  1066 	<xsl:variable name="n" select="@name"/>
       
  1067 	<xsl:variable name="match" select="$abbrevs[@name=$n]"/>
       
  1068 	<xsl:choose>
       
  1069 		<xsl:when test="not($match)"/>
       
  1070 		<xsl:when test="self::techstream">
       
  1071 			<xsl:attribute name="label"><xsl:value-of select="$match/@abbrev"/></xsl:attribute>
       
  1072 		</xsl:when>
       
  1073 	<!-- 	<xsl:when test="self::component">
       
  1074 			<name><xsl:copy-of select="$match/@font|$match/../@font"/><xsl:value-of select="$match/@abbrev"/></name>
       
  1075 		</xsl:when>-->
       
  1076 		<xsl:otherwise><xsl:copy-of select="$match/@abbrev|$match/@font|$match/../@font"/></xsl:otherwise>
       
  1077 	</xsl:choose>
       
  1078 </xsl:template>
       
  1079 
       
  1080 <xsl:template name="location">
       
  1081 	<xsl:if test="../../@name"><xsl:for-each select="parent::*[@name]"><xsl:call-template name="location"/>/</xsl:for-each></xsl:if>
       
  1082 	<xsl:if test="../@name"><xsl:value-of select="../@name"/></xsl:if>
       
  1083 </xsl:template>
       
  1084 
       
  1085 <xsl:template match="Executable">
       
  1086 	<Bin><xsl:copy-of select="@*[name()!='component']|*"/></Bin>
       
  1087 </xsl:template>
       
  1088 
       
  1089 <!-- /merge model with extra -->
       
  1090 
       
  1091 <!-- merge logo -->
       
  1092 <xsl:template match="layout/logo" mode="merge">
       
  1093 	<xsl:copy><xsl:copy-of select="@width | @height"/>
       
  1094 	<xsl:choose>
       
  1095 		<xsl:when test="@embed='yes' or @embed='true' ">
       
  1096 			<xsl:for-each select="document(@src,.)/s:svg">
       
  1097 				<xsl:copy-of select="@viewBox"/>
       
  1098 				<xsl:copy-of select="*"/>
       
  1099 			</xsl:for-each>
       
  1100 		</xsl:when>
       
  1101 		<xsl:otherwise><xsl:copy-of select="@src"/></xsl:otherwise>
       
  1102 	</xsl:choose>
       
  1103 	</xsl:copy>
       
  1104 </xsl:template>
       
  1105 
       
  1106 <!-- merge legend -->
       
  1107 <xsl:template match="layout/legend" mode="merge">
       
  1108 	<legend-layer><xsl:copy-of select="@*[name()!='literal']"/>
       
  1109 		<xsl:if test="not(@literal='yes' or @literal='true') and contains(@label,'{')">
       
  1110 			<xsl:attribute name="label-ref"><xsl:apply-templates mode="style-id" select="."/></xsl:attribute>
       
  1111 		</xsl:if>	
       
  1112 	<!-- only draw footer items if they are not referenced elsewhere in a label -->
       
  1113 		<xsl:variable name="footer">
       
  1114 			<xsl:for-each select="/model/@copyright|/model/@distribution">
       
  1115 				<xsl:variable name="a" select="concat('@',name())"/>
       
  1116 				<xsl:if test="not(/model/layout//legend[contains(@label,$a)]) and not(/model/layout//legend/note[contains(.,$a)])">
       
  1117 					<xsl:value-of select="concat(name(),' ')"/>
       
  1118 				</xsl:if>
       
  1119 			</xsl:for-each>
       
  1120 		</xsl:variable>
       
  1121 		<xsl:if test="$footer!=''"><xsl:attribute name="footer"><xsl:value-of select="normalize-space($footer)"/></xsl:attribute></xsl:if>
       
  1122 		<xsl:apply-templates select="*" mode="merge"/>
       
  1123 	</legend-layer>
       
  1124 </xsl:template>
       
  1125 
       
  1126 <xsl:template match="legend" mode="merge">
       
  1127 	<xsl:copy>
       
  1128 		<xsl:copy-of select="@*[name()!='literal']"/>
       
  1129 		<xsl:if test="not(@literal='yes' or @literal='true') and contains(@label,'{')">
       
  1130 			<xsl:attribute name="label-ref"><xsl:apply-templates mode="style-id" select="."/></xsl:attribute>
       
  1131 		</xsl:if>	
       
  1132 		<xsl:apply-templates select="*" mode="merge"/>
       
  1133 	</xsl:copy>
       
  1134 </xsl:template>
       
  1135 
       
  1136 <xsl:template match="legend/note|legend/s:svg" mode="merge">
       
  1137 	<xsl:copy><xsl:copy-of select="@*[name()!='literal']"/>
       
  1138 		<xsl:if test="self::note and not(@literal='yes' or @literal='true') and contains(.,'{')">
       
  1139 			<xsl:attribute name="label-ref"><xsl:apply-templates mode="style-id" select="."/></xsl:attribute>
       
  1140 		</xsl:if>
       
  1141 		<xsl:copy-of select="node()"/>
       
  1142 	</xsl:copy>
       
  1143 </xsl:template>
       
  1144 
       
  1145 <xsl:template match="cmp" mode="merge"><xsl:param name="model"/>
       
  1146 	<xsl:copy>
       
  1147 		<xsl:apply-templates select="@*" mode="generated-value">
       
  1148 			<xsl:with-param name="model" select="$model"/>		
       
  1149 		</xsl:apply-templates>
       
  1150 		<xsl:if test="not(@literal='yes' or @literal='true') and contains(.,'{')">
       
  1151 			<xsl:attribute name="label-ref"><xsl:apply-templates mode="style-id" select="."/></xsl:attribute>
       
  1152 		</xsl:if>
       
  1153 		<xsl:copy-of select="text()"/>
       
  1154 	</xsl:copy>
       
  1155 </xsl:template>
       
  1156 
       
  1157 <!--not sure if this bit is actually used anywhere -->
       
  1158 <xsl:template match="@*" mode="generated-value"/>
       
  1159 <xsl:template match="@overlay|@border|@style" mode="generated-value">
       
  1160 	<xsl:variable name="n" select="name()"/>
       
  1161 	<xsl:variable name="v" select="."/>
       
  1162 	<xsl:variable name="m" select="//*[name()=$n and @label=$v]"/>
       
  1163 	<xsl:if test="count($m)">
       
  1164 		<xsl:attribute name="generator-{$n}">
       
  1165 			<xsl:apply-templates select="$m" mode="style-id"/>
       
  1166 		</xsl:attribute>
       
  1167 	</xsl:if>
       
  1168 </xsl:template>
       
  1169 <!--not sure if this bit is actually used anywhere -->
       
  1170 <xsl:template match="@color" mode="generated-value"><xsl:param name="model"/>
       
  1171 	<xsl:variable name="v" select="."/>
       
  1172 	<xsl:variable name="m" select="//color[@label=$v]"/>
       
  1173 	<xsl:choose>
       
  1174 		<xsl:when test="count($m)">
       
  1175 			<xsl:attribute name="generator-color"><xsl:apply-templates select="$m" mode="style-id"/></xsl:attribute>
       
  1176 		</xsl:when>
       
  1177 		<xsl:when test="$model//info[@type='color']">
       
  1178 			<xsl:attribute name="generator-color">
       
  1179 				<xsl:variable name="m0" select="$model//info[@type='color']/@href"/>
       
  1180 				<xsl:apply-templates select="document($m0/@href,$m0)//item[@label=$v]" mode="style-id">
       
  1181 					<xsl:with-param name="base" select="$m0"/>
       
  1182 				</xsl:apply-templates>
       
  1183 			</xsl:attribute>
       
  1184 		</xsl:when>		
       
  1185 		<xsl:when test="//colors[not(*) and @match='@ts']">
       
  1186 			<xsl:variable name="id"><xsl:apply-templates select="//colors[not(*) and @match='@ts']" mode="style-id"/></xsl:variable>
       
  1187 			<xsl:for-each select="document($model/@ts)//techstream[@name=$v]">
       
  1188 				<xsl:attribute name="generator-color"><xsl:value-of select="concat($id,'-color',count(preceding::techstream))"/></xsl:attribute>
       
  1189 			</xsl:for-each>
       
  1190 		</xsl:when>
       
  1191 	</xsl:choose>
       
  1192 </xsl:template>
       
  1193 
       
  1194 <xsl:template match="title" mode="merge"><xsl:copy-of select="."/></xsl:template>
       
  1195 <xsl:template match="ts" mode="merge"><cbox><xsl:copy-of select="@*|node()"/></cbox></xsl:template>
       
  1196 
       
  1197 <xsl:template match="legend[s:g]" mode="merge">
       
  1198 <xsl:copy-of select="."/>
       
  1199 </xsl:template>
       
  1200 
       
  1201 <xsl:template match="legend" mode="copy">
       
  1202 	<xsl:copy-of select="@*[name()!='use' or name()='literal']"/>
       
  1203 	<xsl:if test="not(@literal='yes' or @literal='true') and contains(@label,'{')">
       
  1204 		<xsl:attribute name="label-ref"><xsl:apply-templates mode="style-id" select="."/></xsl:attribute>
       
  1205 	</xsl:if>	
       
  1206 </xsl:template>
       
  1207 
       
  1208 <xsl:template match="legend[@use]" mode="merge">
       
  1209 	
       
  1210 	<!-- 	$tag = bit after the # (can be empty)
       
  1211 		$pre is bit before the # (can be empty)
       
  1212 		$file is file pointed to by pre (can be empty) -->
       
  1213 
       
  1214 	<xsl:variable name="tag" select="substring-after(@use,'#')"/>
       
  1215 	<xsl:variable name="pre">
       
  1216 		<xsl:choose>
       
  1217 			<xsl:when test="$tag!=''"><xsl:value-of select="substring-before(@use,'#')"/></xsl:when>
       
  1218 			<xsl:otherwise><xsl:value-of select="@use"/></xsl:otherwise>
       
  1219 		</xsl:choose>
       
  1220 	</xsl:variable>
       
  1221 
       
  1222 	<xsl:variable name="file">
       
  1223 		<xsl:choose>
       
  1224 			<xsl:when test="starts-with($pre,'@')"><xsl:value-of select="//model/@*[name()=substring($pre,2)]"/></xsl:when>
       
  1225 			<xsl:otherwise><xsl:value-of select="$pre"/></xsl:otherwise>
       
  1226 		</xsl:choose>
       
  1227 	</xsl:variable>
       
  1228 	
       
  1229 	<xsl:variable name="model" select="/model"/>  <!--  hack for xsltproc -->
       
  1230 	<xsl:variable name="legend" select="."/>  <!--  hack for xsltproc -->
       
  1231 
       
  1232 	<xsl:variable name="this" select="."/>  <!--  save node just in case need to use more than once -->
       
  1233 
       
  1234 	
       
  1235 	<xsl:choose>
       
  1236 		<xsl:when test="$tag!='' and $pre=''">
       
  1237 			<!-- pre is empty, but tag is not, so this legend is an info in this file -->
       
  1238 			<xsl:for-each select="ancestor::layout/info[@type=$tag]">
       
  1239 				<legend>
       
  1240 					<xsl:apply-templates select="$this" mode="copy"/>
       
  1241 					<xsl:apply-templates select="." mode="merge">
       
  1242 						<xsl:with-param name="model" select="$model"/>
       
  1243 						<xsl:with-param name="legend" select="$legend"/>
       
  1244 					</xsl:apply-templates>
       
  1245 				</legend>
       
  1246 			</xsl:for-each>
       
  1247 		</xsl:when>
       
  1248 		<xsl:when test="$tag!=''">
       
  1249 			<xsl:for-each select="document($file,/)/*/*[name()=$tag]">
       
  1250 				<legend>
       
  1251 					<xsl:apply-templates select="$this" mode="copy"/>
       
  1252 					<xsl:apply-templates select="." mode="merge">
       
  1253 						<xsl:with-param name="model" select="$model"/>
       
  1254 						<xsl:with-param name="legend" select="$legend"/>
       
  1255 					</xsl:apply-templates>
       
  1256 				</legend>			
       
  1257 			</xsl:for-each>
       
  1258 		</xsl:when>
       
  1259 		<xsl:otherwise>
       
  1260 			<xsl:for-each select="document($file,/)/*">
       
  1261 				<legend>
       
  1262 					<xsl:apply-templates select="$this" mode="copy"/>
       
  1263 					<xsl:apply-templates select="." mode="merge">
       
  1264 						<xsl:with-param name="model" select="$model"/>
       
  1265 						<xsl:with-param name="legend" select="$legend"/>
       
  1266 					</xsl:apply-templates>
       
  1267 				</legend>			
       
  1268 			</xsl:for-each>			
       
  1269 			</xsl:otherwise>
       
  1270 	</xsl:choose>
       
  1271 </xsl:template>
       
  1272 
       
  1273 <xsl:template match="info[(@type='color'  or @type='overlay' or @type='style' or @type='border') and document(@href,.)/values]" mode="merge">
       
  1274 	<xsl:attribute name="use"><xsl:apply-templates select="." mode="style-id"/></xsl:attribute>
       
  1275 	<xsl:attribute name="type">
       
  1276 		<xsl:choose>
       
  1277 			<xsl:when test="@type='color'">cbox</xsl:when>
       
  1278 			<xsl:otherwise>cmp</xsl:otherwise>
       
  1279 		</xsl:choose>
       
  1280 	</xsl:attribute>
       
  1281 </xsl:template>
       
  1282 
       
  1283 <xsl:template match="text()" mode="merge"/>
       
  1284 
       
  1285 <!-- Any ref with a type attribute that starts with a # indicates a literal ref, so just copy it -->
       
  1286 <xsl:template match="*[starts-with(@type,'#')]" mode="ref" priority="1"><xsl:value-of select="@type"/></xsl:template>
       
  1287 
       
  1288 <!-- Borders in legend -->
       
  1289 
       
  1290 <xsl:template match="border" mode="ref">#Border<xsl:choose>
       
  1291 		<xsl:when test="@type"><xsl:value-of select="@type"/></xsl:when>
       
  1292 		<xsl:otherwise>Shape<xsl:value-of select="count(preceding::border)"/></xsl:otherwise>
       
  1293 	</xsl:choose>
       
  1294 </xsl:template>
       
  1295 
       
  1296 <xsl:template match="color[../@type='highlight' or ../@type='text-highlight']" mode="ref">
       
  1297 	<xsl:value-of select="concat('#',../@type,count(preceding::color[../@type]))"/>
       
  1298 </xsl:template>
       
  1299 
       
  1300 <!-- Colours in legend -->
       
  1301 
       
  1302 <xsl:template match="/shapes/colors/color[not(@value|@label)]" mode="merge" priority="3"/> 	<!-- use value if no label, but don't show if neither -->
       
  1303 
       
  1304 
       
  1305 <!-- can have any number of these, so put in a sub-legend -->
       
  1306 <xsl:template match="/shapes/examples" mode="merge"><xsl:param name="model"/>
       
  1307 	<xsl:param name="legend"/>	<!-- label on legend overrides label in values document -->
       
  1308 	<xsl:variable name="tag" select="name()"/>
       
  1309 	<xsl:variable name="content">
       
  1310 		<!-- don't show this label if there is a label defined in the Model XML doc *and* this is the first legend item of this type (e.g colors, styles, etc)	 -->
       
  1311 		<xsl:choose>
       
  1312 			<xsl:when test="@sort='yes'">
       
  1313 				<xsl:apply-templates mode="merge">
       
  1314 					<xsl:sort select="@label"/>
       
  1315 					<xsl:with-param name="model" select="$model"/>
       
  1316 				</xsl:apply-templates>
       
  1317 			</xsl:when>
       
  1318 			<xsl:otherwise>
       
  1319 				<xsl:apply-templates mode="merge"><xsl:with-param name="model" select="$model"/></xsl:apply-templates>
       
  1320 			</xsl:otherwise>
       
  1321 		</xsl:choose>
       
  1322 	</xsl:variable>
       
  1323 	<xsl:choose>
       
  1324 		<xsl:when test="count(//*[name()=$tag]) != 1">
       
  1325 			<legend>
       
  1326 				<xsl:if test="not($legend) or not($legend/@label) or preceding-sibling::*[name()=$tag]"><xsl:apply-templates select="@label"  mode="legend-abbrev"/></xsl:if>
       
  1327 				<xsl:copy-of select="$content"/>
       
  1328 			</legend>
       
  1329 		</xsl:when>
       
  1330 		<xsl:otherwise>
       
  1331 			<xsl:copy-of select="@sort|@show-unused"/>
       
  1332 			<xsl:if test="not($legend) or not($legend/@label) or preceding-sibling::*[name()=$tag]"><xsl:apply-templates select="@label"  mode="legend-abbrev"/></xsl:if>
       
  1333 			<xsl:copy-of select="$content"/></xsl:otherwise>
       
  1334 	</xsl:choose>
       
  1335 </xsl:template>
       
  1336 
       
  1337 <!-- can have any number of these, so put in a sub-legend -->
       
  1338 <xsl:template match="/shapes/styles|/shapes/colors|/shapes/patterns|/shapes/borders" mode="merge">
       
  1339 	<xsl:variable name="tag" select="name()"/>
       
  1340 
       
  1341 	
       
  1342 	<xsl:choose>
       
  1343 		<xsl:when test="count(//*[name()=$tag]) != 1">
       
  1344 			<legend>
       
  1345 				<xsl:copy-of select="@sort|@show-unused"/>
       
  1346 				<xsl:attribute name="use"><xsl:apply-templates select="." mode="style-id"/></xsl:attribute>		
       
  1347 				<xsl:attribute name="type">cmp</xsl:attribute>
       
  1348 			</legend>
       
  1349 		</xsl:when>
       
  1350 		<xsl:otherwise>
       
  1351 			<xsl:copy-of select="@sort|@show-unused"/>
       
  1352 			<xsl:attribute name="use"><xsl:apply-templates select="." mode="style-id"/></xsl:attribute>		
       
  1353 			<xsl:attribute name="type">cmp</xsl:attribute>		
       
  1354 		</xsl:otherwise>
       
  1355 	</xsl:choose>
       
  1356 
       
  1357 	
       
  1358 </xsl:template>
       
  1359 
       
  1360 
       
  1361 <xsl:template match="overlay" mode="ref">#Pattern<xsl:choose>
       
  1362 		<xsl:when test="@type"><xsl:value-of select="@type"/></xsl:when>
       
  1363 		<xsl:otherwise>Overlay<xsl:value-of select="count(preceding::overlay)"/></xsl:otherwise>
       
  1364 	</xsl:choose>
       
  1365 </xsl:template>
       
  1366 
       
  1367 	
       
  1368 <xsl:template match="/shapes/*/*[not(@label)]" mode="merge"/> <!--  don't show legend items with no label -->
       
  1369 	
       
  1370 
       
  1371 <xsl:template match="/shapes" mode="as-example"><xsl:param name="at"/>
       
  1372 	<xsl:choose>
       
  1373 		<xsl:when test="name($at)='border' or name($at)='overlay'">
       
  1374 			<xsl:attribute name="generator-{name($at)}">
       
  1375 				<xsl:apply-templates  select="//*[name()=name($at) and @label=$at]" mode="style-id"/>
       
  1376 			</xsl:attribute>
       
  1377 		</xsl:when>
       
  1378 		<xsl:when test="name($at)='style'">
       
  1379 			<xsl:attribute name="generator-{name($at)}"><xsl:apply-templates  select="//style[@label=$at]" mode="style-id"/></xsl:attribute>			
       
  1380 		</xsl:when>	
       
  1381 		<xsl:when test="name($at)='color-highlight'">
       
  1382 			<xsl:attribute name="generator-highlight">
       
  1383 				<xsl:apply-templates select="//colors[@type='highlight']/color[@label=$at]" mode="style-id"/>
       
  1384 			</xsl:attribute>
       
  1385 		</xsl:when>	
       
  1386 		<xsl:when test="name($at)='color-text-highlight'">
       
  1387 			<xsl:attribute name="generator-text-highlight">
       
  1388 				<xsl:apply-templates select="//colors[@type='text-highlight']/color[@label=$at]" mode="style-id"/>
       
  1389 			</xsl:attribute>
       
  1390 		</xsl:when>	
       
  1391 		<xsl:when test="name($at)='color'">
       
  1392 			<xsl:attribute name="generator-color">
       
  1393 				<xsl:apply-templates select="//colors[not(@type) or @type='background']/color[@label=$at]/@color" mode="style-id"/>
       
  1394 			</xsl:attribute>
       
  1395 		</xsl:when>	
       
  1396 	</xsl:choose>
       
  1397 </xsl:template>
       
  1398 
       
  1399 
       
  1400 <xsl:template match="examples/cmp" mode="merge"><xsl:param name="model"/><xsl:param name="shapes" select="/shapes"/>
       
  1401 	<cmp>
       
  1402 		<xsl:if test="not(@literal='yes' or @literal='true') and contains(.,'{')">
       
  1403 			<xsl:attribute name="label-ref"><xsl:apply-templates mode="style-id" select="."/></xsl:attribute>
       
  1404 		</xsl:if>	
       
  1405 		<xsl:for-each select="@*">
       
  1406 			<xsl:apply-templates select="$shapes" mode="as-example">
       
  1407 				<xsl:with-param name="at" select="."/>
       
  1408 			</xsl:apply-templates>
       
  1409 		</xsl:for-each>
       
  1410 		<xsl:apply-templates mode="generated-value" select="@color">
       
  1411 			<xsl:with-param name="model" select="$model"/>
       
  1412 		</xsl:apply-templates>
       
  1413 		<xsl:copy-of select="text()"/>
       
  1414 	</cmp>
       
  1415 </xsl:template>
       
  1416 
       
  1417 
       
  1418 <xsl:template match="/shapes/colors[not(*)]" mode="merge"><xsl:param name="model"/>
       
  1419 <!-- special known type which can be generated -->
       
  1420 	<xsl:choose>
       
  1421 		<xsl:when test="count(//colors) != 1 and @match='@ts' and $model">
       
  1422 			<legend>
       
  1423 				<xsl:attribute name="use"><xsl:apply-templates select="." mode="style-id"/></xsl:attribute>
       
  1424 				<xsl:attribute name="sort">yes</xsl:attribute>
       
  1425 				<xsl:attribute name="type">cbox</xsl:attribute>
       
  1426 			</legend>
       
  1427 		</xsl:when>
       
  1428 		<xsl:when test="@match='@ts' and $model">
       
  1429 			<xsl:attribute name="use"><xsl:apply-templates select="." mode="style-id"/></xsl:attribute>
       
  1430 			<xsl:attribute name="sort">yes</xsl:attribute>
       
  1431 			<xsl:attribute name="type">cbox</xsl:attribute>
       
  1432 		</xsl:when>
       
  1433 		<xsl:otherwise>
       
  1434 			<xsl:call-template name="Caller-Warning"><xsl:with-param name="text">no colour data</xsl:with-param></xsl:call-template>
       
  1435 		</xsl:otherwise>
       
  1436 	</xsl:choose>
       
  1437 </xsl:template>
       
  1438 
       
  1439 <!-- /merge legend -->
       
  1440 
       
  1441 <!-- new experimetal stuff  -->
       
  1442 <xsl:template match="node()" mode="styling"/>
       
  1443 
       
  1444 <xsl:template match="@shapes" mode="styling">
       
  1445 	<xsl:variable name="model" select=".."/> <!-- hack for xsltproc -->
       
  1446 	<xsl:apply-templates select="document(.,.)/shapes/*" mode="styling">
       
  1447 	<xsl:with-param name="model" select="$model"/>
       
  1448 	</xsl:apply-templates>
       
  1449 </xsl:template>
       
  1450 
       
  1451 
       
  1452 <xsl:template match="info" mode="style-id">
       
  1453 	<xsl:value-of select="concat('i',count(preceding-sibling::info))"/>
       
  1454 </xsl:template>
       
  1455 
       
  1456 
       
  1457 <xsl:template match="/values" mode="style-id"><xsl:param name="base"/>
       
  1458 	<xsl:apply-templates select="$base" mode="style-id"/>
       
  1459 </xsl:template>
       
  1460 
       
  1461 <xsl:template match="/values/item" mode="style-id"><xsl:param name="base"/>
       
  1462 	<xsl:apply-templates select="$base" mode="style-id"/>-<xsl:value-of select="name()"/>
       
  1463 	<xsl:value-of select="count(preceding-sibling::*)"/>
       
  1464 </xsl:template>
       
  1465 
       
  1466 <xsl:template match="colors|borders|patterns|styles" mode="style-id">
       
  1467 	<xsl:value-of select="concat('s',count(preceding-sibling::*))"/>
       
  1468 </xsl:template>
       
  1469 
       
  1470 <xsl:template match="examples/cmp" mode="style-id">
       
  1471 	<xsl:value-of select="concat('e',count(preceding::cmp[parent::examples]))"/>
       
  1472 </xsl:template>
       
  1473 	
       
  1474 
       
  1475 <xsl:template match="note" mode="style-id">
       
  1476 	<xsl:value-of select="concat('n',count(preceding::note))"/>
       
  1477 </xsl:template>
       
  1478 
       
  1479 <xsl:template match="legend" mode="style-id">
       
  1480 	<xsl:value-of select="concat('L',count(preceding::legend))"/>
       
  1481 </xsl:template>
       
  1482 
       
  1483 <xsl:template match="colors/color|borders/border|patterns/overlay|styles/style" mode="style-id">
       
  1484 	<xsl:apply-templates select=".." mode="style-id"/>-<xsl:value-of select="name()"/>
       
  1485 	<xsl:value-of select="count(preceding-sibling::*)"/>
       
  1486 </xsl:template>
       
  1487 
       
  1488 <xsl:template match="layout/info[@type='color' or @type='border' or @type='overlay' or @type='style']" mode="styling">
       
  1489 	<xsl:variable name="id"><xsl:apply-templates select="." mode="style-id"/></xsl:variable>
       
  1490 	<xsl:variable name="base" select="."/>
       
  1491 	<group style-id="{$id}" detail="component">
       
  1492 		<xsl:copy-of select="@type | @show-unused"/>
       
  1493 		<xsl:for-each select="document(@href,.)/values">
       
  1494 			<xsl:copy-of select="@default"/>
       
  1495 			<xsl:apply-templates select="@label"  mode="legend-abbrev"/>				
       
  1496 			<xsl:for-each select="item">
       
  1497 				<xsl:variable name="el">
       
  1498 					<xsl:choose>
       
  1499 						<xsl:when test="$base/@type='color'">cbox</xsl:when>
       
  1500 						<xsl:otherwise>cmp</xsl:otherwise>
       
  1501 					</xsl:choose>
       
  1502 				</xsl:variable>
       
  1503 				<xsl:element name="{$el}">
       
  1504 					<xsl:attribute name="style-id">
       
  1505 						<xsl:apply-templates select="." mode="style-id">
       
  1506 							<xsl:with-param name="base" select="$base"/>
       
  1507 						</xsl:apply-templates>
       
  1508 					</xsl:attribute>
       
  1509 					<xsl:apply-templates select="@label"  mode="legend-abbrev"/>	
       
  1510 					<xsl:attribute name="value">
       
  1511 					<xsl:choose>
       
  1512 						<xsl:when test="not(starts-with(@value,'#')) and $base/@type='border' ">#Border<xsl:value-of select="@value"/></xsl:when> 
       
  1513 						<xsl:when test="not(starts-with(@value,'#')) and $base/@type='overlay' ">#Pattern<xsl:value-of select="@value"/></xsl:when>
       
  1514 							<!-- highlight not allowed in values files, but put here anyway -->
       
  1515 						<xsl:when test="contains($base/@type,'highlight')">#<xsl:value-of select="$base/@type"/>
       
  1516 							<xsl:apply-templates select="." mode="style-id">
       
  1517 								<xsl:with-param name="base" select="$base"/>
       
  1518 							</xsl:apply-templates></xsl:when>
       
  1519 						<xsl:otherwise><xsl:value-of select="@value"/></xsl:otherwise>
       
  1520 					</xsl:choose>
       
  1521 					</xsl:attribute>
       
  1522 				</xsl:element>
       
  1523 			</xsl:for-each>
       
  1524 		</xsl:for-each>
       
  1525 	</group>
       
  1526 </xsl:template>
       
  1527 
       
  1528 <xsl:template match="/shapes/colors[not(@type) or @type='background']" mode="styling"><xsl:param name="model"/>
       
  1529 	<xsl:variable name="id"><xsl:apply-templates select="." mode="style-id"/></xsl:variable>
       
  1530 	<group type="color" style-id="{$id}">
       
  1531 		<xsl:copy-of select="@default"/>
       
  1532 		<xsl:call-template name="lgd-group-detail"/>
       
  1533 		<xsl:apply-templates select="@label"  mode="legend-abbrev"/>			
       
  1534 		<xsl:choose>
       
  1535 		<xsl:when test="not(*) and @match='@ts' and $model">
       
  1536 			<xsl:apply-templates select="document($model/@ts,$model)/*" mode="styling">
       
  1537 				<xsl:with-param name="id" select="$id"/>
       
  1538 			</xsl:apply-templates>
       
  1539 		</xsl:when>
       
  1540 		<xsl:otherwise>
       
  1541 			<xsl:apply-templates mode="styling"><xsl:with-param name="id" select="$id"/></xsl:apply-templates>
       
  1542 		</xsl:otherwise>
       
  1543 		</xsl:choose>
       
  1544 	</group>
       
  1545 </xsl:template>
       
  1546 
       
  1547 <xsl:template name="lgd-group-detail">
       
  1548 	<xsl:choose>
       
  1549 		<xsl:when test="self::borders or self::patterns or  not(@match)"><xsl:attribute name="detail">component</xsl:attribute></xsl:when>
       
  1550 		<xsl:when test="@match='component' or @match='collection' or @match='block' or @match='subblock' or @match='layer'">
       
  1551 			<xsl:attribute name="detail"><xsl:value-of select="@match"/></xsl:attribute>
       
  1552 		</xsl:when>
       
  1553 	</xsl:choose>
       
  1554 </xsl:template>
       
  1555 
       
  1556 <xsl:template match="@label" mode="legend-abbrev">
       
  1557 	<xsl:variable name="n" select="."/>
       
  1558 	<xsl:variable name="match" select="$abbrevs[@name=$n]"/>
       
  1559 	<xsl:choose>
       
  1560 		<xsl:when test="not($match)"><xsl:copy-of select="."/></xsl:when>
       
  1561 		<xsl:otherwise>
       
  1562 			<xsl:attribute name="label"><xsl:value-of select="$match/@abbrev"/></xsl:attribute>
       
  1563 			<xsl:copy-of select="$match/@font"/>
       
  1564 			<xsl:if test="not($match/@font)">
       
  1565 				<xsl:copy-of select="$match/ancestor::display-names/@font"/>
       
  1566 			</xsl:if>
       
  1567 		</xsl:otherwise>
       
  1568 	</xsl:choose>
       
  1569 </xsl:template>
       
  1570 
       
  1571 
       
  1572 <xsl:template match="/shapes/colors[@type='highlight' or @type='text-highlight']" mode="styling"><xsl:param name="model"/>
       
  1573 	<xsl:variable name="id"><xsl:apply-templates select="." mode="style-id"/></xsl:variable>
       
  1574 	<group type="{@type}" style-id="{$id}">
       
  1575 		<xsl:call-template name="lgd-group-detail"/>
       
  1576 		<xsl:copy-of select="@default"/>
       
  1577 		<xsl:apply-templates select="@label"  mode="legend-abbrev"/>	
       
  1578 		<xsl:apply-templates mode="styling"><xsl:with-param name="id" select="$id"/></xsl:apply-templates>
       
  1579 	</group>
       
  1580 </xsl:template>
       
  1581 
       
  1582 
       
  1583 <xsl:template match="/shapes/borders|/shapes/patterns|/shapes/styles" mode="styling"><xsl:param name="model"/>
       
  1584 	<xsl:variable name="id"><xsl:apply-templates select="." mode="style-id"/></xsl:variable>
       
  1585 	<group type="{name(*)}" style-id="{$id}">
       
  1586 		<xsl:call-template name="lgd-group-detail"/>
       
  1587 		<xsl:copy-of select="@default"/>
       
  1588 		<xsl:apply-templates select="@label"  mode="legend-abbrev"/>	
       
  1589 		<xsl:apply-templates mode="styling"><xsl:with-param name="id" select="$id"/></xsl:apply-templates>
       
  1590 	</group>
       
  1591 </xsl:template>
       
  1592 
       
  1593 <xsl:template match="/shapes/colors/color|/shapes/borders/border|/shapes/patterns/overlay|/shapes/styles/style" mode="styling">
       
  1594 	<xsl:variable name="my-id"><xsl:apply-templates select="." mode="style-id"/></xsl:variable>
       
  1595 	<xsl:variable name="el">
       
  1596 		<xsl:choose>
       
  1597 			<xsl:when test="self::color[../@type='highlight' or ../@type='text-highlight']">cmp</xsl:when>
       
  1598 			<xsl:when test="self::color">cbox</xsl:when>
       
  1599 			<xsl:otherwise>cmp</xsl:otherwise>
       
  1600 		</xsl:choose>
       
  1601 	</xsl:variable>
       
  1602 	<xsl:element name="{$el}">
       
  1603 		<xsl:attribute name="style-id"><xsl:value-of select="$my-id"/></xsl:attribute>
       
  1604 		<xsl:apply-templates select="@label"  mode="legend-abbrev"/>
       
  1605 		<xsl:if test="contains(@label,'{')">
       
  1606 			<xsl:attribute name="label-ref"><xsl:value-of select="$my-id"/></xsl:attribute>
       
  1607 		</xsl:if>
       
  1608 		<xsl:if test="@value">
       
  1609 			<xsl:attribute name="lookup"><xsl:value-of select="@value"/></xsl:attribute>
       
  1610 		</xsl:if>
       
  1611 		<xsl:attribute name="value">	
       
  1612 			<xsl:choose>
       
  1613 				<xsl:when test="self::style"><xsl:value-of select="."/></xsl:when>
       
  1614 				<xsl:when test="self::color[not(../@type) or ../@type='background']"><xsl:value-of select="@color"/></xsl:when>
       
  1615 				<xsl:otherwise><xsl:apply-templates select="." mode="ref"/></xsl:otherwise>
       
  1616 			</xsl:choose>
       
  1617 		</xsl:attribute>
       
  1618 	</xsl:element>
       
  1619 </xsl:template>
       
  1620 
       
  1621 <xsl:template match="/techstreams" mode="styling">
       
  1622 	<xsl:param name="id"/>
       
  1623 	<xsl:for-each select="//techstream">
       
  1624 		<cbox value="{@color}" lookup="{@name}" style-id="{$id}-color{count(preceding::techstream)}" detail="component">
       
  1625 			<xsl:variable name="n" select="@name"/>
       
  1626 			<xsl:variable name="match" select="$abbrevs[@name=$n]"/>
       
  1627 			<xsl:if test="$match">
       
  1628 				<xsl:attribute name="label"><xsl:value-of select="$match/@abbrev"/></xsl:attribute>
       
  1629 				<xsl:copy-of select="$match/@font"/>
       
  1630 				<xsl:if test="not($match/@font)">
       
  1631 					<xsl:copy-of select="$match/ancestor::display-names/@font"/>
       
  1632 				</xsl:if>				
       
  1633 			</xsl:if>
       
  1634 		</cbox>
       
  1635 	</xsl:for-each>
       
  1636 </xsl:template>
       
  1637 
       
  1638 <!-- for date formatting: only use if the date functions are *fully* supported (ie not by xalan)
       
  1639 Uses unix date %-encoding, but only a few are supported (see comments)
       
  1640 -->  
       
  1641 <xsl:template name="format-date"><xsl:param name="date" select="."/>
       
  1642 	<xsl:choose>
       
  1643 		<xsl:when test="contains($date,'%')">
       
  1644 			<xsl:value-of select="substring-before($date,'%')"/>
       
  1645 			<xsl:variable name="rest" select="substring-after($date,'%')"/>
       
  1646 			<xsl:choose>
       
  1647 				<xsl:when test="starts-with($rest,'%') or $rest=''">%</xsl:when> <!-- %%     a literal % -->
       
  1648 				<xsl:when test="starts-with($rest,'a')"><xsl:value-of select="date:day-abbreviation()"/></xsl:when> <!--      %a     locale’s abbreviated weekday name (e.g., Sun) -->
       
  1649 				<xsl:when test="starts-with($rest,'A')"><xsl:value-of select="date:day-name()"/></xsl:when> <!--  %A     locale’s full weekday name (e.g., Sunday)-->
       
  1650 				<xsl:when test="starts-with($rest,'b') or starts-with($rest,'h')"><xsl:value-of select="date:month-abbreviation()"/></xsl:when><!--       %b     locale’s abbreviated month name (e.g., Jan)-->
       
  1651 				<xsl:when test="starts-with($rest,'B')"><xsl:value-of select="date:month-name()"/></xsl:when><!--       %B     locale’s full month name (e.g., January)-->
       
  1652 				<xsl:when test="starts-with($rest,'d')"><xsl:number format="01" value="date:day-in-month()"/></xsl:when><!--%d     day of month (e.g, 01)-->
       
  1653 				<xsl:when test="starts-with($rest,'e')"><xsl:number format=" 1" value="date:day-in-month()"/></xsl:when><!--%d     day of month (e.g, 01)-->
       
  1654 				<xsl:when test="starts-with($rest,'F')"><xsl:value-of select="date:date()"/></xsl:when><!--%F     full date; same as %Y-%m-%d-->
       
  1655 				<xsl:when test="starts-with($rest,'H')"><xsl:number format="01" value="date:hour-in-day()"/></xsl:when><!--%H     hour (00..23)-->
       
  1656 				<xsl:when test="starts-with($rest,'I')"><xsl:number format="01" value="((24 + date:hour-in-day() - 1) mod 12) + 1"/></xsl:when><!--%I     hour (01..12)-->
       
  1657 				<xsl:when test="starts-with($rest,'j')"><xsl:number format="001" value="date:day-in-year()"/></xsl:when><!--%j     day of year (001..366)-->
       
  1658 				<xsl:when test="starts-with($rest,'k')"><xsl:number format=" 1" value="date:hour-in-day()"/></xsl:when><!--%k     hour ( 0..23)-->
       
  1659 				<xsl:when test="starts-with($rest,'l')"><xsl:number format=" 1" value="((24 + date:hour-in-day() - 1) mod 12) + 1"/></xsl:when><!--%l     hour ( 1..12)-->
       
  1660 				<xsl:when test="starts-with($rest,'m')"><xsl:number format="01" value="date:month-in-year()"/></xsl:when><!--%m     month (01..12)-->
       
  1661 				<xsl:when test="starts-with($rest,'M')"><xsl:number format="01" value="date:minute-in-hour()"/></xsl:when><!--%M     minute (00..59)-->
       
  1662 				<xsl:when test="starts-with($rest,'p') and date:hour-in-day() &lt; 12">AM</xsl:when><!--%p     locale’s equivalent of either AM or PM; blank if not known-->
       
  1663 				<xsl:when test="starts-with($rest,'p')">PM</xsl:when><!--%p     locale’s equivalent of either AM or PM; blank if not known-->
       
  1664 				<xsl:when test="starts-with($rest,'P') and date:hour-in-day() &lt; 12">am</xsl:when><!--%P     like %p, but lower case-->
       
  1665 				<xsl:when test="starts-with($rest,'P')">pm</xsl:when><!--%P     like %p, but lower case-->
       
  1666 				<xsl:when test="starts-with($rest,'S')"><xsl:number format="01" value="date:second-in-minute()"/></xsl:when><!--%S     second (00..60)-->
       
  1667 				<xsl:when test="starts-with($rest,'T')"><xsl:value-of select="date:time()"/></xsl:when><!--%T     time; same as %H:%M:%S-->
       
  1668 				<xsl:when test="starts-with($rest,'u')"><xsl:value-of select="((date:day-in-week() +5) mod 7) + 1"/></xsl:when><!--%u     day of week (1..7); 1 is Monday-->
       
  1669 				<xsl:when test="starts-with($rest,'V')"><xsl:value-of select="date:week-in-year()"/></xsl:when><!--%V     ISO week number, with Monday as first day of week (01..53)-->
       
  1670 				<xsl:when test="starts-with($rest,'w')"><xsl:value-of select="date:day-in-week() - 1"/></xsl:when><!--%w     day of week (0..6); 0 is Sunday-->
       
  1671 				<xsl:when test="starts-with($rest,'y')"><xsl:value-of select="date:year() mod 100"/></xsl:when><!--%y     last two digits of year (00..99)-->
       
  1672 				<xsl:when test="starts-with($rest,'Y')"><xsl:value-of select="date:year()"/></xsl:when><!--%Y     year-->
       
  1673 				<xsl:otherwise><xsl:value-of select="substring($rest,1,1)"/></xsl:otherwise>
       
  1674 			</xsl:choose>
       
  1675 			<xsl:if test="string-length($rest) &gt; 1">
       
  1676 				<xsl:call-template name="format-date">
       
  1677 					<xsl:with-param name="date" select="substring($rest,2)"/>
       
  1678 				</xsl:call-template>
       
  1679 			</xsl:if>
       
  1680 		</xsl:when>
       
  1681 		<xsl:otherwise><xsl:value-of select="$date"/></xsl:otherwise>
       
  1682 	</xsl:choose>
       
  1683 </xsl:template>
       
  1684 
       
  1685 <xsl:include href="draw.xsl"/>
       
  1686 </xsl:stylesheet>