sysmodellibs/sysmodelgen/core/joinsysdef-module.xsl
changeset 7 3c36c452f013
child 8 a2e65c705db8
equal deleted inserted replaced
6:5b32dc297d05 7:3c36c452f013
       
     1 <?xml version="1.0"?>
       
     2  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       
     3  	xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exslt">
       
     4 <!--Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     5 	All rights reserved.
       
     6 	This component and the accompanying materials are made available
       
     7 	under the terms of the License "Eclipse Public License v1.0"
       
     8 	which accompanies this distribution, and is available
       
     9 	at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    10 
       
    11 	Initial Contributors:
       
    12 	Nokia Corporation - initial contribution.
       
    13 	Contributors:
       
    14 	Description:
       
    15 	XSLT module which contains the logic to join a system definition file
       
    16 -->
       
    17 	<!-- save SF namespace as a constant to avoid the risk of typos-->
       
    18  <xsl:variable name="defaultns">http://www.symbian.org/system-definition</xsl:variable>
       
    19  
       
    20 <!-- create a stand-alone sysdef from a linked set of fragments -->
       
    21 
       
    22 <xsl:template match="/*" mode="join">
       
    23 	<xsl:param name="filename"/>
       
    24 	<xsl:message terminate="yes">ERROR: Cannot process this document<xsl:if test="$filename !=''"> (<xsl:value-of select="$filename"/>)</xsl:if>
       
    25 		<xsl:choose>
       
    26 			<xsl:when test="self::SystemDefinition/@schema">. Unrecognised syntax schema="<xsl:value-of select="@schema"/>"</xsl:when>
       
    27 			<xsl:when test="self::SystemDefinition">. Missing schema</xsl:when>
       
    28 			<xsl:otherwise>. Invalid file type: <xsl:value-of select="name()"/></xsl:otherwise>
       
    29 		</xsl:choose>
       
    30 	</xsl:message>
       
    31 </xsl:template>
       
    32 
       
    33 <!-- anything in schemas 3.0.x won't add new functional attributes that need processing here, just blindly copy them-->
       
    34 
       
    35 <xsl:template match="/SystemDefinition[starts-with(@schema,'3.0.') and count(*)=1]" mode="join">
       
    36 	<xsl:param name="origin" select="/.."/>
       
    37 	<xsl:param name="root"/>
       
    38 	<xsl:param name="filename"/>
       
    39 	<xsl:param name="namespaces"/>
       
    40 	<xsl:param name="data" select="/.."/>
       
    41 	<xsl:choose>
       
    42 		<xsl:when test="$origin">	<!-- this sysdef fragment was linked from a parent sysdef -->
       
    43 			<xsl:for-each select="*"> <!-- can be only one -->
       
    44 				<xsl:variable name="upid"><xsl:apply-templates select="$origin/@id" mode="my-id"/></xsl:variable>		<!-- namespaceless ID of this in parent doc -->
       
    45 				<xsl:variable name="id"><xsl:apply-templates select="@id" mode="my-id"/></xsl:variable>						<!-- namespaceless ID of this here -->
       
    46 				<xsl:variable name="upns"><xsl:apply-templates select="$origin/@id" mode="my-namespace"/></xsl:variable>	<!-- ID's namespace in parent doc -->
       
    47 				<xsl:variable name="ns"><xsl:apply-templates select="@id" mode="my-namespace"/></xsl:variable>	<!-- ID's namespace -->
       
    48 				<xsl:if test="$id!=$upid or $ns!=$upns">
       
    49 					<xsl:message terminate="yes">ERROR: Linked ID "<xsl:value-of select="$id"/>" (<xsl:value-of select="$ns"/>) must match linking document "<xsl:value-of select="$upid"/>" (<xsl:value-of select="$upns"/>)</xsl:message>
       
    50 				</xsl:if>
       
    51 				<!-- copy any attributes not already defined (parent doc overrides child doc)-->
       
    52 				<xsl:for-each select="@*">
       
    53 					<xsl:variable name="n" select="name()"/>
       
    54 					<xsl:choose>
       
    55 						<xsl:when test="$n='id'"/> <!-- never copy this, always set -->
       
    56 						<xsl:when test="$origin/@*[name()=$n]"> <!-- don't copy if already set -->
       
    57 							<xsl:message>Note: Cannot set "<xsl:value-of select="$n"/>", already set on <xsl:value-of select="$origin/@id"/>. Ignoring linked value</xsl:message>
       
    58 						</xsl:when>
       
    59 						<xsl:when test="$n='before' or $n='replace'">
       
    60 							<!-- ensure ns is correct (if any future attribtues will ever use an ID, process it here too)-->
       
    61 							<xsl:apply-templates select="." mode="join">
       
    62 								<xsl:with-param name="namespaces" select="$namespaces"/>
       
    63 							</xsl:apply-templates>
       
    64 						</xsl:when> 
       
    65 						<xsl:otherwise><xsl:copy-of select="."/></xsl:otherwise> <!-- just copy anything else -->
       
    66 					</xsl:choose>
       
    67 				</xsl:for-each>
       
    68 				<xsl:copy-of select="../namespace::*[not(.=$namespaces)]"/> <!-- set any namespaces not already set (they should all alreayd be, but some XSLT processors are quirky) -->
       
    69 			 	<xsl:apply-templates select="$data" mode="overlay-attributes">
       
    70 					<xsl:with-param name="item" select="current()"/>
       
    71 				</xsl:apply-templates>
       
    72 				<xsl:variable name="content">									
       
    73 					<xsl:apply-templates select="*|comment()" mode="join">
       
    74 						<xsl:with-param name="root" select="$root"/>
       
    75 						<xsl:with-param name="filename" select="$filename"/>
       
    76 						<xsl:with-param name="data" select="$data"/>
       
    77 						<xsl:with-param name="namespaces" select="$namespaces | ../namespace::*[not(.=$namespaces)]"/>
       
    78 					</xsl:apply-templates>
       
    79 				</xsl:variable>
       
    80 				<xsl:apply-templates select="." mode="is-content-filtered"> <!-- optionally add filtered="yes" if some content has been removed -->
       
    81 					<xsl:with-param name="content" select="$content"/>
       
    82 				</xsl:apply-templates>
       
    83 			 	<xsl:apply-templates select="$data" mode="overlay-meta">
       
    84 					<xsl:with-param name="item" select="current()"/>
       
    85 				</xsl:apply-templates>
       
    86 				<xsl:copy-of select="$content"/>
       
    87 			</xsl:for-each>
       
    88 		</xsl:when>
       
    89 		<xsl:when test="function-available('exslt:node-set')"> <!-- this is the root element of a root sysdef -->
       
    90 			<!--try to put all namespaces in root element -->
       
    91 			<xsl:variable name="nss">
       
    92 				<!-- contains node set of namespaces to add to root element.
       
    93 					May panic if there are too many single-letter namespaces and this can't create a new one -->
       
    94 				<xsl:call-template name="needed-namespaces">
       
    95 					<xsl:with-param name="foundns">
       
    96 						<xsl:apply-templates select="//*[(self::component or self::collection or self::package or self::layer) and @href]" mode="scan-for-namespaces"/>
       
    97 					</xsl:with-param>
       
    98 				</xsl:call-template>
       
    99 			</xsl:variable>
       
   100 			<xsl:variable name="ns" select="@id-namespace | namespace::* | exslt:node-set($nss)/*"/>
       
   101 			<xsl:copy><xsl:copy-of select="@*[name()!='schema']"/><xsl:call-template name="set-schema"/>
       
   102 				<xsl:apply-templates select="self::*[not(@id-namespace)]" mode="add-id-ns"/>
       
   103 				<xsl:for-each select="exslt:node-set($nss)/*"> <!-- add namespace definitions -->
       
   104 					<xsl:attribute name="xmlns:{name()}">
       
   105 						<xsl:value-of select="."/>
       
   106 					</xsl:attribute>
       
   107 				</xsl:for-each>
       
   108 				<!-- no need to call is-content-filtered, it never will be from this element --> 
       
   109 				<xsl:apply-templates select="*|comment()" mode="join">
       
   110 					<xsl:with-param name="namespaces" select="$ns"/>
       
   111 					<xsl:with-param name="root" select="$root"/>
       
   112 					<xsl:with-param name="data" select="$data"/>
       
   113 					<xsl:with-param name="filename" select="$filename"/>
       
   114 				</xsl:apply-templates>
       
   115 			</xsl:copy>
       
   116 		</xsl:when>
       
   117 		<xsl:otherwise> <!-- can't handle node-set() so put the namespaces in the document instead of the root element-->
       
   118 			<xsl:variable name="ns" select="@id-namespace | namespace::*"/>
       
   119 			<xsl:copy><xsl:copy-of select="@*[name()!='schema']"/><xsl:call-template name="set-schema"/>
       
   120 				<!-- no need to call is-content-filtered, it never will be from this element -->
       
   121 				<xsl:apply-templates select="*|comment()" mode="join">
       
   122 					<xsl:with-param name="namespaces" select="$ns"/>
       
   123 					<xsl:with-param name="root" select="$root"/>
       
   124 					<xsl:with-param name="data" select="$data"/>
       
   125 					<xsl:with-param name="filename" select="$filename"/>
       
   126 				</xsl:apply-templates>
       
   127 			</xsl:copy>
       
   128 		</xsl:otherwise>
       
   129 	</xsl:choose>
       
   130 </xsl:template>
       
   131 
       
   132 <xsl:template match="*" mode="scan-for-namespaces"/> <!-- just in case of errors, consider replacing by terminate -->
       
   133 <xsl:template match="*[@href and not(self::meta)]" mode="scan-for-namespaces">
       
   134 	<!-- produce a list of namespace-prefix namespace pairs separated by newlines, in reverse order found in documents 
       
   135 		reverse order so we can try to use the first namespace prefix defined if it's available-->
       
   136 	<xsl:variable name="linked" select="document(@href,.)/*"/>
       
   137 	<xsl:for-each select="$linked">
       
   138 		<xsl:apply-templates select="//*[(self::component or self::collection or self::package or self::layer) and @href]" mode="scan-for-namespaces"/>
       
   139 		<xsl:for-each select="//namespace::*[.!='http://www.w3.org/XML/1998/namespace'] | @id-namespace">
       
   140 			<!-- ignore XML namespace -->
       
   141 			<xsl:value-of select="concat(name(),' ',.,'&#xa;')"/>
       
   142 		</xsl:for-each>
       
   143 	</xsl:for-each>
       
   144 	<xsl:if test="not($linked)">
       
   145 	<xsl:message>Note: The link to <xsl:value-of select="@href"/> from <xsl:value-of select="concat(name(),' ',@id)"/> could not be resolved. Perhaps there's an error in the XML?</xsl:message>
       
   146 	</xsl:if>
       
   147 </xsl:template>
       
   148 
       
   149 <xsl:template name="needed-namespaces">
       
   150 	<xsl:param name="foundns"/>
       
   151 	<xsl:param name="usedpre"/>
       
   152 
       
   153 	<xsl:if test="$foundns!=''">
       
   154 		<xsl:variable name="line" select="substring-before($foundns,'&#xa;')"/> <!-- always has trailing newline -->
       
   155 		<xsl:variable name="name" select="substring-after($line,' ')"/> <!-- namespace prefix -->
       
   156 		<xsl:variable name="remainder" select="substring-after($foundns,'&#xa;')"/>
       
   157 		<xsl:variable name="newprefix">
       
   158 			<xsl:if test="not(contains(concat('&#xa;',$remainder),concat('&#xa;',$line,'&#xa;'))) and
       
   159 				not(//namespace::*[.=$name] or @id-namespace[.=$name] or (not(@id-namespace) and $defaultns=$name))">
       
   160 						<xsl:apply-templates select="." mode="ns-prefix">
       
   161 							<xsl:with-param name="ns" select="$name"/>
       
   162 							<xsl:with-param name="pre" select="substring-before($line,' ')"/>
       
   163 							<xsl:with-param name="dontuse" select="$usedpre"/>
       
   164 						</xsl:apply-templates>
       
   165 			</xsl:if>
       
   166 		</xsl:variable>
       
   167 		<xsl:if test="$newprefix!=''">
       
   168 			<!-- can treat this as if it were a namespace node -->
       
   169 			<xsl:element name="{$newprefix}">
       
   170 				<xsl:value-of select="$name"/>
       
   171 			</xsl:element>
       
   172 		</xsl:if>
       
   173 		<xsl:if test="$remainder!=''">
       
   174 			<xsl:call-template name="needed-namespaces">
       
   175 				<xsl:with-param name="foundns" select="$remainder"/>
       
   176 				<xsl:with-param name="usedpre" select="concat($usedpre,' ',$newprefix,' ')"/>
       
   177 			</xsl:call-template>
       
   178 		</xsl:if>
       
   179 	</xsl:if>
       
   180 </xsl:template>
       
   181 
       
   182 <xsl:template match="/SystemDefinition" mode="ns-prefix">
       
   183 	<!-- should be able to replace this with mechanism that uses the XSLT processor's own ability to generate namespaces -->
       
   184 	<xsl:param name="ns"/>
       
   185 	<xsl:param name="pre"/>
       
   186 	<xsl:param name="dontuse"/>
       
   187 	<xsl:param name="chars">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz</xsl:param>
       
   188 	<xsl:variable name="name" select="substring(substring-after($ns,'http://www.'),1,1)"/>
       
   189 	<xsl:choose>
       
   190 		<xsl:when test="$pre!='' and $pre!='id-namespace' and not(//namespace::*[name()=$pre]) and not(contains($dontuse,concat(' ',$pre,' ')))">
       
   191 			<xsl:value-of select="$pre"/>
       
   192 		</xsl:when>
       
   193 		<xsl:when test="$ns='' and $chars=''">
       
   194 			<xsl:message terminate="yes">ERROR: Cannot create namespace prefix for downstream default namespace in <xsl:value-of select="*/@id"/></xsl:message>
       
   195 		</xsl:when>
       
   196 		<xsl:when test="$name!='' and not(contains($dontuse,concat(' ',$name,' ')))"><xsl:value-of select="$name"/></xsl:when>
       
   197 		<xsl:when test="namespace::*[name()=substring($chars,1,1)] or contains($dontuse,concat(' ',substring($chars,1,1),' '))">
       
   198 			<xsl:apply-templates mode="ns-prefix">
       
   199 				<xsl:with-param name="chars" select="substring($chars,2)"/>
       
   200 			</xsl:apply-templates>
       
   201 		</xsl:when>
       
   202 		<xsl:otherwise>
       
   203 			<xsl:value-of select="substring($chars,1,1)"/>
       
   204 		</xsl:otherwise>
       
   205 	</xsl:choose>
       
   206 </xsl:template>
       
   207 
       
   208 
       
   209 <!-- schema handling -->
       
   210 
       
   211 <xsl:template name="set-schema">
       
   212 <xsl:attribute name="schema">
       
   213 	<xsl:apply-templates mode="my-schema" select="/SystemDefinition"/>
       
   214 </xsl:attribute>
       
   215 </xsl:template>
       
   216 
       
   217 <xsl:template name="compare-versions"><xsl:param name="v1"/><xsl:param name="v2"/>
       
   218 			<xsl:choose>
       
   219 				<xsl:when test="$v1=''"><xsl:value-of select="$v2"/></xsl:when>
       
   220 				<xsl:when test="$v1=$v2 or $v2=''"><xsl:value-of select="$v1"/></xsl:when>
       
   221 				<xsl:when test="substring-before($v1,'.') &gt; substring-before($v2,'.')"><xsl:value-of select="$v1"/></xsl:when>
       
   222 				<xsl:when test="substring-before($v1,'.') &lt; substring-before($v2,'.')"><xsl:value-of select="$v2"/></xsl:when>
       
   223 				<xsl:when test="substring-before(substring-after($v1,'.'),'.') &gt; substring-before(substring-after($v2,'.'),'.')"><xsl:value-of select="$v1"/></xsl:when>
       
   224 				<xsl:when test="substring-before(substring-after($v1,'.'),'.') &lt; substring-before(substring-after($v2,'.'),'.')"><xsl:value-of select="$v2"/></xsl:when>
       
   225 				<xsl:when test="substring-after(substring-after($v1,'.'),'.') &gt; substring-after(substring-after($v2,'.'),'.')"><xsl:value-of select="$v1"/></xsl:when>
       
   226 				<xsl:when test="substring-after(substring-after($v1,'.'),'.') &lt; substring-after(substring-after($v2,'.'),'.')"><xsl:value-of select="$v2"/></xsl:when>
       
   227 				<xsl:otherwise><xsl:value-of select="$v1"/></xsl:otherwise>
       
   228 			</xsl:choose>
       
   229 </xsl:template>
       
   230 
       
   231 <xsl:template name="compare-version-list"><xsl:param name="list"/>
       
   232 	<xsl:variable name="cur" select="substring-before($list,' ')"/>
       
   233 	<xsl:variable name="remaining" select="substring-after($list,' ')"/>
       
   234 	<xsl:choose>
       
   235 		<xsl:when test="$remaining=''"><xsl:value-of select="$cur"/></xsl:when>
       
   236 		<xsl:otherwise>
       
   237 			<xsl:variable name="nextbig">
       
   238 				<xsl:call-template name="compare-version-list">
       
   239 					<xsl:with-param name="list" select="$remaining"/>
       
   240 				</xsl:call-template>
       
   241 			</xsl:variable>
       
   242 			<xsl:call-template name="compare-versions">
       
   243 				<xsl:with-param name="v1" select="$cur"/>
       
   244 				<xsl:with-param name="v2" select="$nextbig"/>
       
   245 			</xsl:call-template>
       
   246 		</xsl:otherwise>
       
   247 	</xsl:choose>
       
   248 </xsl:template>
       
   249 
       
   250 <xsl:template match="/SystemDefinition" mode="my-schema"><xsl:param name="biggest" select="@schema"/>
       
   251 	<xsl:variable name="linked" select="//*[(self::component or self::collection or self::package or self::layer) and @href]"/>
       
   252 	<xsl:choose>
       
   253 		<xsl:when test="not($linked)"> <!-- no need to go further -->
       
   254 			<xsl:call-template name="compare-versions">
       
   255 				<xsl:with-param name="v1" select="@schema"/>
       
   256 				<xsl:with-param name="v2" select="$biggest"/>
       
   257 			</xsl:call-template>
       
   258 		</xsl:when>
       
   259 		<xsl:otherwise>
       
   260 				<xsl:call-template name="compare-version-list">
       
   261 					<xsl:with-param name="list">
       
   262 						<xsl:for-each select="$linked">
       
   263 						<xsl:call-template name="compare-versions">
       
   264 							<xsl:with-param name="v1">
       
   265 								<xsl:apply-templates mode="my-schema" select="document(@href,.)/*"/>
       
   266 							</xsl:with-param>
       
   267 							<xsl:with-param name="v2" select="$biggest"/>
       
   268 						</xsl:call-template>
       
   269 						<xsl:text> </xsl:text>
       
   270 					</xsl:for-each>
       
   271 				</xsl:with-param>
       
   272 				</xsl:call-template>
       
   273 		</xsl:otherwise>
       
   274 	</xsl:choose>
       
   275 </xsl:template>
       
   276 
       
   277 
       
   278 <xsl:template match="unit" mode="join">	<xsl:param name="root"/><xsl:param name="filename"/><xsl:param name="data"/>
       
   279 	 <xsl:variable name="display">
       
   280 	 	<xsl:apply-templates select="$data" mode="filter">
       
   281 			<xsl:with-param name="item" select="current()"/>
       
   282 		</xsl:apply-templates>
       
   283 	 </xsl:variable>
       
   284 	 
       
   285 	 <xsl:if test="$display != 'hide' "> <!-- if hide, remove completely from the output-->	 
       
   286 		<xsl:element name="{name()}">
       
   287 			<xsl:apply-templates select="@*" mode="join">
       
   288 				<xsl:with-param name="root" select="$root"/>
       
   289 				<xsl:with-param name="filename" select="$filename"/>
       
   290 			</xsl:apply-templates>
       
   291 		</xsl:element>
       
   292 	</xsl:if>
       
   293 </xsl:template>
       
   294 
       
   295 <!-- override mode="meta" to translate metadata sections. By default, include -->
       
   296 <xsl:template match="meta" priority="2" mode="join"><xsl:param name="data"/>
       
   297 	 <xsl:variable name="display">
       
   298 	 	<xsl:apply-templates select="$data" mode="filter">
       
   299 			<xsl:with-param name="item" select="current()"/>
       
   300 		</xsl:apply-templates>
       
   301 	 </xsl:variable>
       
   302 	 
       
   303 	<xsl:if test="$display != 'hide' "> <!-- if hide, remove completely from the output-->	
       
   304 		<xsl:apply-templates select="." mode="meta"> 
       
   305 			<xsl:with-param name="display" select="$display"/>
       
   306 			<xsl:with-param name="data" select="$data"/>
       
   307 		</xsl:apply-templates>
       
   308 	</xsl:if>
       
   309 </xsl:template>
       
   310 
       
   311 <xsl:template match="meta[@rel='link-mapping']" priority="3" mode="join"/> <!--these are only used in the joining process, so remove from output -->
       
   312 
       
   313 
       
   314 <xsl:template match="*" mode="join">
       
   315 	<xsl:param name="root"/><xsl:param name="filename"/><xsl:param name="namespaces"/><xsl:param name="data"/>
       
   316 	<!-- get attribtues from overlay -->
       
   317 	<!-- test for presence, if filtered out, just return -->
       
   318 	<!-- test for children, if it has some, but they're filtered out, either return or leave as empty, dependening on filter rule
       
   319 		if had items and now has none, options:
       
   320 			still has meta: keep / delete
       
   321 			still has comments: keep / delete 
       
   322 	 -->
       
   323 	 <xsl:variable name="display">
       
   324 	 	<xsl:apply-templates select="$data" mode="filter">
       
   325 			<xsl:with-param name="item" select="current()"/>
       
   326 		</xsl:apply-templates>
       
   327 	 </xsl:variable>
       
   328 	 
       
   329 	 <xsl:if test="$display != 'hide' "> <!-- if hide, remove completely from the output-->
       
   330 		 <xsl:variable name="href">
       
   331 		 	<xsl:apply-templates select="." mode="link">
       
   332 				<xsl:with-param name="data" select="$data"/>
       
   333 			</xsl:apply-templates>
       
   334 		 </xsl:variable>
       
   335 	 		 
       
   336 		<xsl:element name="{name()}"> <!-- use this instead of <copy> so xalan doesn't add extra wrong namespaces -->
       
   337 			<xsl:apply-templates select="@*" mode="join">
       
   338 				<xsl:with-param name="namespaces" select="$namespaces"/>
       
   339 			</xsl:apply-templates>
       
   340 			<xsl:if test="$display != '' ">
       
   341 				<!-- custom attribute to indicate how this is to be represented. Blank indicates normal, hide removes from the output (see above), anything else is put in the attribute --> 
       
   342 				<xsl:attribute name="display"><xsl:value-of select="$display"/></xsl:attribute>
       
   343 			</xsl:if>
       
   344 		 	<xsl:apply-templates select="$data" mode="overlay-attributes">
       
   345 				<xsl:with-param name="item" select="current()"/>
       
   346 			</xsl:apply-templates>			
       
   347 			<xsl:choose>
       
   348 				<xsl:when test="$href !='' ">
       
   349 					<xsl:variable name="prefixmap" select="ancestor::SystemDefinition/*/meta[@rel='link-mapping']/map-prefix[starts-with($href,@link)]"/>
       
   350 					<xsl:variable name="origin" select="."/>
       
   351 					<xsl:apply-templates select="document($href,.)/*" mode="join">
       
   352 						<xsl:with-param name="origin" select="$origin"/>
       
   353 						<xsl:with-param name="data" select="$data"/>
       
   354 						<xsl:with-param name="namespaces" select="$namespaces"/>
       
   355 						<xsl:with-param name="filename">
       
   356 							<xsl:call-template name="joinpath">
       
   357 								<xsl:with-param name="file" select="$filename"/>
       
   358 								<xsl:with-param name="rel">
       
   359 									<xsl:choose>
       
   360 										<xsl:when test="$prefixmap">
       
   361 											<xsl:value-of select="$prefixmap/@to"/>
       
   362 											<xsl:value-of select="substring-after($href,$prefixmap/@link)"/>
       
   363 										</xsl:when>
       
   364 										<xsl:otherwise>
       
   365 											<xsl:value-of select="$href"/>
       
   366 										</xsl:otherwise>
       
   367 									</xsl:choose>								
       
   368 								 </xsl:with-param>
       
   369 							</xsl:call-template>					
       
   370 						</xsl:with-param>
       
   371 						<xsl:with-param name="root">
       
   372 							<xsl:value-of select="$root"/>/<xsl:call-template name="lastbefore">
       
   373 								<xsl:with-param name="string" select="$href"/>
       
   374 							</xsl:call-template>
       
   375 						</xsl:with-param>
       
   376 					</xsl:apply-templates> 
       
   377 				</xsl:when>
       
   378 				<xsl:otherwise>
       
   379 					<xsl:variable name="content">
       
   380 						<xsl:apply-templates select="*|comment()" mode="join">
       
   381 							<xsl:with-param name="root" select="$root"/>
       
   382 							<xsl:with-param name="filename" select="$filename"/>
       
   383 							<xsl:with-param name="namespaces" select="$namespaces"/>
       
   384 							<xsl:with-param name="data" select="$data"/>
       
   385 						</xsl:apply-templates>
       
   386 					</xsl:variable>
       
   387 					<xsl:apply-templates select="." mode="is-content-filtered"> <!-- add filtered="yes" if some content has been removed -->
       
   388 						<xsl:with-param name="content" select="$content"/>
       
   389 					</xsl:apply-templates>
       
   390 				 	<xsl:apply-templates select="$data" mode="overlay-meta">
       
   391 						<xsl:with-param name="item" select="current()"/>
       
   392 					</xsl:apply-templates>
       
   393 					<xsl:copy-of select="$content"/>
       
   394 				</xsl:otherwise>
       
   395 			</xsl:choose>
       
   396 		</xsl:element>
       
   397 	</xsl:if>
       
   398 </xsl:template>
       
   399 
       
   400 <!-- By default, do nothing. Can override template to add filtered="yes" if need to track what's a placeholder and what's been filtered 
       
   401 	implement with param name="content"
       
   402 -->
       
   403 <xsl:template mode="is-content-filtered" match="*" priority="-2"/>
       
   404 
       
   405 
       
   406 <xsl:template match="@mrp[starts-with(.,'/')] | @bldFile[starts-with(.,'/')] | @base[starts-with(.,'/')]" mode="join">
       
   407 	<xsl:copy-of select="."/>
       
   408 </xsl:template>
       
   409 
       
   410 
       
   411 <xsl:template match="@mrp|@bldFile|@base" mode="join">	<xsl:param name="root"/><xsl:param name="filename"/>
       
   412 	<xsl:attribute name="{name()}">
       
   413 		<xsl:call-template name="joinpath">
       
   414 			<xsl:with-param name="file" select="$filename"/>
       
   415 			<xsl:with-param name="rel" select="."/>
       
   416 		</xsl:call-template>	
       
   417 	</xsl:attribute>	
       
   418 </xsl:template>
       
   419 
       
   420 
       
   421 <xsl:template match="@href" mode="join"/> <!--never copy this into the generated doc, that's the whole point of this module -->
       
   422 
       
   423 <xsl:template match="@*" mode="my-namespace"> <!-- the namespace of an ID -->
       
   424 	<xsl:choose>
       
   425 		<xsl:when test="contains(.,':')">
       
   426 			<xsl:value-of select="ancestor::*/namespace::*[name()=substring-before(current(),':')]"/>
       
   427 		</xsl:when>
       
   428 		<xsl:when test="/SystemDefinition/@id-namespace">
       
   429 			<xsl:value-of select="/SystemDefinition/@id-namespace"/>
       
   430 		</xsl:when>
       
   431 		<xsl:otherwise>
       
   432 			<xsl:value-of select="$defaultns"/>
       
   433 		</xsl:otherwise>
       
   434 	</xsl:choose>
       
   435 </xsl:template>
       
   436 
       
   437 
       
   438 <xsl:template match="@*" mode="my-id"> <!-- the ID with namespace prefix removed -->
       
   439 	<xsl:choose>
       
   440 		<xsl:when test="contains(.,':')">
       
   441 			<xsl:value-of select="substring-after(.,':')"/>
       
   442 		</xsl:when>
       
   443 		<xsl:otherwise>
       
   444 			<xsl:value-of select="."/>
       
   445 		</xsl:otherwise>
       
   446 	</xsl:choose>
       
   447 </xsl:template>
       
   448 
       
   449 <xsl:template match="*" mode="err-path">
       
   450 <xsl:if test="../@id">
       
   451 	<xsl:apply-templates select=".." mode="err-path"/>/</xsl:if>
       
   452 <xsl:value-of select="@id"/>
       
   453 </xsl:template>
       
   454 
       
   455 
       
   456 <xsl:template match="@id|@before|@replace" mode="join">
       
   457 	<xsl:param name="namespaces"/>
       
   458 	<!-- this will change the namespace prefixes for all IDs to match the root document -->
       
   459 	<xsl:variable name="ns">
       
   460 		<xsl:apply-templates select="." mode="my-namespace"/>
       
   461 	</xsl:variable>
       
   462 	<xsl:if test="$ns=''">
       
   463 		<xsl:message terminate="yes">ERROR: Could not find namespace for <xsl:value-of select="name()"/> "<xsl:value-of select="."/>" in <xsl:apply-templates select="../.." mode="err-path"/>
       
   464 		<xsl:text>&#xa;</xsl:text>
       
   465 		</xsl:message>
       
   466 	</xsl:if>
       
   467 	<xsl:variable name="prefix" select="name($namespaces[.=$ns])"/>
       
   468 	<xsl:attribute name="{name()}">
       
   469 	<xsl:choose>
       
   470 		<xsl:when test="$prefix = 'id-namespace' or  (not($namespaces[name()='id-namespace']) and $ns=$defaultns)"/> <!-- it's the default namespace, no prefix -->
       
   471 		<xsl:when test="$prefix='' and contains(.,':')">
       
   472 			<!-- complex: copy id and copy namespace (namespace should be copied already)-->
       
   473 			<xsl:value-of select="."/>
       
   474 		</xsl:when>
       
   475 		<xsl:when test="$prefix='' and $ns=$defaultns"/> <!-- no prefix and it's the default --> 
       
   476 		<xsl:when test="$prefix!=''">			<!-- just change the prefix -->
       
   477 			<xsl:value-of select="concat($prefix,':')"/>
       
   478 		</xsl:when>
       
   479 		<xsl:otherwise>
       
   480 		<xsl:message terminate="yes">ERROR: Joining error in resolving namespace for <xsl:value-of select="name()"/> "<xsl:value-of select="."/>" in <xsl:apply-templates select="../.." mode="err-path"/>
       
   481 		<xsl:text>&#xa;</xsl:text></xsl:message>
       
   482 		</xsl:otherwise>
       
   483 	</xsl:choose>
       
   484 		<xsl:apply-templates select="." mode="my-id"/>
       
   485 	</xsl:attribute>
       
   486 </xsl:template>
       
   487 
       
   488 
       
   489 
       
   490 <xsl:template match="@*|comment()" mode="join"><xsl:copy-of select="."/></xsl:template>
       
   491 
       
   492 
       
   493 <xsl:include href="path-module.xsl"/>
       
   494 
       
   495 <!-- overridable templates follow -->
       
   496 
       
   497 
       
   498 <xsl:template match="*" mode="filter" priority="-9"/> <!-- by default show --> 
       
   499 <xsl:template match="*" mode="overlay-attributes" priority="-9"/> <!-- by default do nothing --> 
       
   500 <xsl:template match="*" mode="overlay-meta" priority="-9"/> <!-- by default do nothing --> 
       
   501 <xsl:template match="/SystemDefinition" mode="add-id-ns" priority="-9"/> <!-- some tools may have an easier job if this were always present, but, by default, assume it can just stay implied -->
       
   502 
       
   503 <xsl:template match="*" mode="link" priority="-1"> <!-- can be overriden to allow custom changes to href values --> 
       
   504 <xsl:value-of select="@href"/>
       
   505 </xsl:template>
       
   506 
       
   507 
       
   508 <xsl:template match="*" mode="meta" priority="-9"><xsl:param name="data"/><xsl:param name="display"/>
       
   509 	<xsl:element name="{name()}">
       
   510 		<xsl:copy-of select="@*[name()!='href']"/> <!-- copy all attributes as is, always drop href -->
       
   511 		<xsl:choose>
       
   512 			<xsl:when test="$display='local' and @href and contains(@href,':') and not(starts-with(@href,'file:'))">
       
   513 				<!-- non-local URL: only want local URLs, so keep href as is-->
       
   514 				<xsl:copy-of select="@href"/> 
       
   515 			</xsl:when>
       
   516 			<xsl:when test="@href">
       
   517 				<xsl:copy-of select="document(@href,.)/*"/> 
       
   518 			</xsl:when>
       
   519 			<xsl:otherwise>
       
   520 				<xsl:copy-of select="*|comment()"/>
       
   521 			</xsl:otherwise>
       
   522 		</xsl:choose>
       
   523 	</xsl:element>
       
   524 </xsl:template>
       
   525 
       
   526 
       
   527 </xsl:stylesheet>