sysmodellibs/sysmodelgen/extra/makeabbrev.xsl
changeset 7 3c36c452f013
equal deleted inserted replaced
6:5b32dc297d05 7:3c36c452f013
       
     1 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       
     2 	xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exslt" >
       
     3 	<xsl:param name="dict"/> <!--  the dictionary file -->
       
     4 	<xsl:param name="include" select="1"/> <!-- boolean indicates if any linked abbrev files from a model file should be included. Can turn it off to avoid creating a file which linked (recursion bad) -->
       
     5 	<xsl:variable name="d" select="document($dict,/)/dict/word"/>
       
     6 
       
     7 <xsl:variable name="WordCut" select="10"/>
       
     8 <xsl:variable name="Cut" select="22"/>
       
     9 <xsl:variable name="Cut2" select="25"/>
       
    10 
       
    11 <xsl:template match="*" priority="-1"><xsl:apply-templates select="*|@href"/></xsl:template>
       
    12 
       
    13 <xsl:template match="@href">
       
    14 	<xsl:apply-templates select="document(.,.)/SystemDefinition/*"/>
       
    15 </xsl:template>
       
    16 
       
    17 <xsl:template match="meta/@href"/>
       
    18 
       
    19 <xsl:template match="/*" priority="1">
       
    20 <display-names><xsl:apply-templates select="*"/>
       
    21 </display-names>
       
    22 </xsl:template>
       
    23 
       
    24 <xsl:template match="/model/*" priority="5"/>
       
    25 <xsl:template match="/model/sysdef" priority="5">
       
    26 	<xsl:apply-templates select="@href"/>
       
    27 </xsl:template>
       
    28 <xsl:template match="/model/layout" priority="5">
       
    29 	<xsl:if test="$include">
       
    30 		<xsl:copy-of select="document(info[@type='abbrev']/@href,.)/display-names/*"/>
       
    31 	</xsl:if>
       
    32 </xsl:template>
       
    33 
       
    34 <xsl:template match="*[@name]">
       
    35 	<xsl:variable name="list">
       
    36 		<xsl:call-template name="wordlist"/>
       
    37 	</xsl:variable>
       
    38 	<xsl:variable name="w">
       
    39 		<xsl:apply-templates select="exslt:node-set($list)/w" mode="word"/>
       
    40 	</xsl:variable>
       
    41 	<xsl:variable name="v">
       
    42 		<xsl:call-template name="print">
       
    43 		<xsl:with-param name="words" select="exslt:node-set($w)"/>
       
    44 		</xsl:call-template>
       
    45 	</xsl:variable>
       
    46 	
       
    47 	<xsl:variable name="txt" >
       
    48 		<xsl:call-template name="breaks">
       
    49 			<xsl:with-param name="text">
       
    50 				<xsl:choose>
       
    51 					<xsl:when test="string-length($v) &gt; $Cut"> <!-- too long total, use abbreviation instead of display form, starting from the end -->
       
    52 						<xsl:variable name="w2">
       
    53 							<xsl:apply-templates select="exslt:node-set($w)/w[last()]" mode="pass2"/>
       
    54 						</xsl:variable>
       
    55 						<xsl:variable name="v2">
       
    56 							<xsl:call-template name="print">
       
    57 							<xsl:with-param name="words" select="exslt:node-set($w2)"/>
       
    58 							</xsl:call-template>
       
    59 						</xsl:variable>
       
    60 						<xsl:choose>
       
    61 							<xsl:when test="string-length($v2) &gt; $Cut2">
       
    62 								<xsl:variable name="w3">
       
    63 									<xsl:apply-templates select="exslt:node-set($w2)/w[last()]" mode="pass2">
       
    64 										<xsl:with-param name="at">s</xsl:with-param>
       
    65 									</xsl:apply-templates>
       
    66 								</xsl:variable>
       
    67 								<xsl:call-template name="print">
       
    68 									<xsl:with-param name="words" select="exslt:node-set($w3)"/>
       
    69 								</xsl:call-template>
       
    70 							</xsl:when>
       
    71 							<xsl:otherwise>
       
    72 								<xsl:value-of select="$v2"/>
       
    73 							</xsl:otherwise>
       
    74 						</xsl:choose>
       
    75 					</xsl:when>
       
    76 					<xsl:when test="$v != '' and $v != @name">
       
    77 						<xsl:value-of select="$v"/>
       
    78 					</xsl:when> 
       
    79 				</xsl:choose>
       
    80 			</xsl:with-param>
       
    81 		</xsl:call-template>
       
    82 	</xsl:variable>
       
    83 	
       
    84 	<xsl:if test="$txt != '' and $txt != @name">
       
    85 		<abbrev name="{@name}" abbrev="{$txt}"/>
       
    86 	</xsl:if>
       
    87 	
       
    88 	<xsl:apply-templates select="@href|*"/>
       
    89 </xsl:template>
       
    90 
       
    91 <xsl:template name="print">
       
    92 	<xsl:param name="words"/>
       
    93 	<xsl:apply-templates select="$words/w" mode="print"/>
       
    94 </xsl:template>
       
    95 
       
    96 
       
    97 
       
    98 <xsl:template mode="print" match="w">
       
    99 	<xsl:value-of select="@use"/>
       
   100 	<xsl:if test="not(@use)">
       
   101 		<xsl:value-of select="."/>
       
   102 	</xsl:if>
       
   103 	<xsl:if test="following-sibling::w">
       
   104 		<xsl:text> </xsl:text>
       
   105 	</xsl:if>
       
   106 </xsl:template>
       
   107 
       
   108 
       
   109 <xsl:template match="layer[string-length(@id) &lt; 10]" priority="2"><!-- don't abbreviate short layer names -->
       
   110 	<xsl:apply-templates select="*"/>
       
   111 </xsl:template>
       
   112 
       
   113 <xsl:template mode="word" match="w">
       
   114 	<xsl:variable name="match" select="$d[@term=current()]"/>
       
   115 	<xsl:copy>
       
   116 	<xsl:choose>
       
   117 		<xsl:when test="(string-length(.) &gt; $WordCut) and $match/@abbrev">
       
   118 			<xsl:attribute name="use">
       
   119 			<xsl:value-of select="$match/@abbrev"/>
       
   120 			</xsl:attribute>
       
   121 		</xsl:when>
       
   122 		<xsl:when test="$match/@d">
       
   123 		<xsl:attribute name="use">
       
   124 			<xsl:value-of select="$match/@d"/>
       
   125 			</xsl:attribute>
       
   126 		</xsl:when>
       
   127 	</xsl:choose>
       
   128 	<xsl:value-of select="."/>
       
   129 	</xsl:copy>
       
   130 </xsl:template>
       
   131 
       
   132 
       
   133 
       
   134 <xsl:template name="wordlist">
       
   135 	<xsl:param name="words" select="normalize-space(@name)"/>
       
   136 <w>
       
   137 		<xsl:choose>
       
   138 			<xsl:when test="contains($words,' ')">
       
   139 				<xsl:value-of select="substring-before($words,' ')"/>
       
   140 			</xsl:when>
       
   141 			<xsl:otherwise>
       
   142 				<xsl:value-of select="$words"/>
       
   143 			</xsl:otherwise>
       
   144 		</xsl:choose>
       
   145 </w>
       
   146 	<xsl:if test="contains($words,' ')">
       
   147 		<xsl:call-template name="wordlist">
       
   148 			<xsl:with-param name="words" select="substring-after($words,' ')"/>
       
   149 		</xsl:call-template>
       
   150 	</xsl:if>
       
   151 </xsl:template>
       
   152 
       
   153 
       
   154 <xsl:template match="w" mode="pass2"> <!-- 2nd pass -->
       
   155 	<xsl:param name="len" select="0"/>
       
   156 	<xsl:param name="at">abbrev</xsl:param>
       
   157 	<xsl:variable name="match" select="$d[@term=current()]"/>
       
   158 	<xsl:variable name="cur">
       
   159 		<xsl:copy>
       
   160 			<xsl:choose>
       
   161 				<xsl:when test="$match/@*[name()=$at]">
       
   162 					<xsl:attribute name="use">
       
   163 						<xsl:value-of select="$match/@*[name()=$at]"/>
       
   164 					</xsl:attribute>
       
   165 				</xsl:when>
       
   166 				<xsl:otherwise>
       
   167 					<xsl:copy-of select="@use"/>
       
   168 				</xsl:otherwise>
       
   169 			</xsl:choose>
       
   170 			<xsl:value-of select="."/>
       
   171 		</xsl:copy>
       
   172 	</xsl:variable>
       
   173 	
       
   174 	<xsl:variable name="mytxt">
       
   175 		<xsl:value-of select="exslt:node-set($cur)/w"/>
       
   176 	</xsl:variable>
       
   177 
       
   178 	<xsl:variable name="prev"> <!--  unprocessed text -->
       
   179 		<xsl:apply-templates select="preceding-sibling::w" mode="print"/>
       
   180 	</xsl:variable>
       
   181 	<xsl:choose>
       
   182 		<xsl:when test="string-length($prev) + string-length($mytxt) +$len &lt;= $Cut"> <!--  shrunk all that's needed. just output the words -->
       
   183 			<xsl:copy-of select="preceding-sibling::w"/>
       
   184 		</xsl:when>
       
   185 		<xsl:otherwise>
       
   186 			<xsl:apply-templates select="preceding-sibling::w[1]" mode="pass2">
       
   187 				<xsl:with-param name="len" select="string-length($mytxt) +$len"/> 
       
   188 				<xsl:with-param name="at" select="$at"/>
       
   189 			</xsl:apply-templates>
       
   190 		</xsl:otherwise>
       
   191 	</xsl:choose>
       
   192 	<xsl:copy-of select="$cur"/>
       
   193 </xsl:template>
       
   194 
       
   195 
       
   196 <xsl:template name="breaks"><xsl:param name="text"/>
       
   197 <xsl:value-of select="$text"/>
       
   198 </xsl:template>
       
   199 
       
   200 </xsl:stylesheet>