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