topicmerge.xsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!--
  3. Copyright © 2004-2005 by Idiom Technologies, Inc. All rights reserved.
  4. IDIOM is a registered trademark of Idiom Technologies, Inc. and WORLDSERVER
  5. and WORLDSTART are trademarks of Idiom Technologies, Inc. All other
  6. trademarks are the property of their respective owners.
  7. IDIOM TECHNOLOGIES, INC. IS DELIVERING THE SOFTWARE "AS IS," WITH
  8. ABSOLUTELY NO WARRANTIES WHATSOEVER, WHETHER EXPRESS OR IMPLIED, AND IDIOM
  9. TECHNOLOGIES, INC. DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
  10. BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11. PURPOSE AND WARRANTY OF NON-INFRINGEMENT. IDIOM TECHNOLOGIES, INC. SHALL NOT
  12. BE LIABLE FOR INDIRECT, INCIDENTAL, SPECIAL, COVER, PUNITIVE, EXEMPLARY,
  13. RELIANCE, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF
  14. ANTICIPATED PROFIT), ARISING FROM ANY CAUSE UNDER OR RELATED TO OR ARISING
  15. OUT OF THE USE OF OR INABILITY TO USE THE SOFTWARE, EVEN IF IDIOM
  16. TECHNOLOGIES, INC. HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  17. Idiom Technologies, Inc. and its licensors shall not be liable for any
  18. damages suffered by any person as a result of using and/or modifying the
  19. Software or its derivatives. In no event shall Idiom Technologies, Inc.'s
  20. liability for any damages hereunder exceed the amounts received by Idiom
  21. Technologies, Inc. as a result of this transaction.
  22. These terms and conditions supersede the terms and conditions in any
  23. licensing agreement to the extent that such terms and conditions conflict
  24. with those set forth herein.
  25. This file is part of the DITA Open Toolkit project hosted on Sourceforge.net.
  26. See the accompanying license.txt file for applicable licenses.
  27. -->
  28. <!-- An adaptation of the Toolkit topicmerge.xsl for FO plugin use. -->
  29. <xsl:stylesheet version="2.0"
  30. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  31. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  32. exclude-result-prefixes="xs">
  33. <xsl:output indent="no"/>
  34. <xsl:key name="topic" match="dita-merge/*[contains(@class,' topic/topic ')]" use="concat('#',@id)"/>
  35. <xsl:key name="topic" match="dita-merge/dita" use="concat('#',*[contains(@class, ' topic/topic ')][1]/@id)"/>
  36. <xsl:key name="topicref" match="//*[contains(@class,' map/topicref ')]" use="generate-id()"/>
  37. <!--
  38. <xsl:template match="/">
  39. <xsl:copy-of select="."/>
  40. </xsl:template>
  41. -->
  42. <xsl:template match="dita-merge">
  43. <xsl:element name="{name(*[contains(@class,' map/map ')])}">
  44. <xsl:copy-of select="*[contains(@class,' map/map ')]/@*"/>
  45. <xsl:apply-templates select="*[contains(@class,' map/map ')]" mode="build-tree"/>
  46. </xsl:element>
  47. </xsl:template>
  48. <xsl:template match="dita-merge/*[contains(@class,' map/map ')]" mode="build-tree">
  49. <opentopic:map xmlns:opentopic="http://www.idiominc.com/opentopic">
  50. <xsl:apply-templates/>
  51. </opentopic:map>
  52. <xsl:apply-templates mode="build-tree"/>
  53. </xsl:template>
  54. <xsl:template match="*[contains(@class,' map/topicref ')]" mode="build-tree">
  55. <xsl:choose>
  56. <xsl:when test="@print='no'" />
  57. <xsl:when test="not(normalize-space(@href) = '')">
  58. <xsl:apply-templates select="key('topic',@href)">
  59. <xsl:with-param name="parentId" select="generate-id()"/>
  60. </xsl:apply-templates>
  61. </xsl:when>
  62. <xsl:when test="(normalize-space(@href) = '') and
  63. (normalize-space(@navtitle) != '' or *[contains(@class,' map/topicmeta ')]/*[contains(@class,' topic/navtitle ')])">
  64. <xsl:variable name="isNotTopicRef">
  65. <xsl:call-template name="isNotTopicRef">
  66. <xsl:with-param name="class" select="@class"/>
  67. </xsl:call-template>
  68. </xsl:variable>
  69. <xsl:if test="contains($isNotTopicRef,'false')">
  70. <topic id="{generate-id()}" class="- topic/topic ">
  71. <title class=" topic/title ">
  72. <xsl:choose>
  73. <xsl:when test="*[contains(@class,' map/topicmeta ')]/*[contains(@class,' topic/navtitle ')]">
  74. <xsl:copy-of select="*[contains(@class,' map/topicmeta ')]/*[contains(@class,' topic/navtitle ')]/text()|
  75. *[contains(@class,' map/topicmeta ')]/*[contains(@class,' topic/navtitle ')]/*"/>
  76. </xsl:when>
  77. <xsl:otherwise>
  78. <xsl:value-of select="@navtitle"/>
  79. </xsl:otherwise>
  80. </xsl:choose>
  81. </title>
  82. <body class=" topic/body "/>
  83. <xsl:apply-templates mode="build-tree"/>
  84. </topic>
  85. </xsl:if>
  86. </xsl:when>
  87. <xsl:otherwise>
  88. <xsl:apply-templates mode="build-tree"/>
  89. </xsl:otherwise>
  90. </xsl:choose>
  91. </xsl:template>
  92. <xsl:template match="*[contains(@class, ' map/topicref ') and @print='no']" priority="5"/>
  93. <xsl:template match="*[contains(@class,' topic/topic ')] | dita-merge/dita">
  94. <xsl:param name="parentId"/>
  95. <xsl:variable name="idcount" as="xs:integer?">
  96. <!--for-each is used to change context. There's only one entry with a key of $parentId-->
  97. <xsl:for-each select="key('topicref',$parentId)">
  98. <xsl:value-of select="count(preceding::*[@href = current()/@href][not(ancestor::*[contains(@class, ' map/reltable ')])]) + count(ancestor::*[@href = current()/@href])"/>
  99. </xsl:for-each>
  100. </xsl:variable>
  101. <xsl:copy>
  102. <xsl:apply-templates select="@*[name() != 'id']"/>
  103. <xsl:variable name="new_id">
  104. <xsl:choose>
  105. <xsl:when test="$idcount &gt; 0">
  106. <xsl:value-of select="concat(@id,'_ssol',$idcount)"/>
  107. </xsl:when>
  108. <xsl:otherwise>
  109. <xsl:value-of select="@id"/>
  110. </xsl:otherwise>
  111. </xsl:choose>
  112. </xsl:variable>
  113. <xsl:attribute name="id">
  114. <xsl:value-of select="$new_id"/>
  115. </xsl:attribute>
  116. <xsl:apply-templates>
  117. <xsl:with-param name="newid" select="$new_id"/>
  118. </xsl:apply-templates>
  119. <xsl:apply-templates select="key('topicref',$parentId)/*" mode="build-tree"/>
  120. </xsl:copy>
  121. </xsl:template>
  122. <xsl:template match="*[contains(@class,' map/topicref ')]/@id" priority="5"/>
  123. <xsl:template match="@href">
  124. <xsl:param name="newid"/>
  125. <xsl:variable name="topic-rest" select="substring-after(., '#')"/>
  126. <xsl:variable name="topic-id" select="substring-before($topic-rest, '/')"/>
  127. <xsl:variable name="element-id" select="substring-after($topic-rest, '/')"/>
  128. <xsl:attribute name="href">
  129. <xsl:choose>
  130. <xsl:when test="$element-id = '' or not(starts-with(., '#unique'))">
  131. <xsl:value-of select="."/>
  132. </xsl:when>
  133. <xsl:when test="ancestor::*[contains(@class, ' topic/topic ')][1]/@id = $topic-id">
  134. <xsl:text>#</xsl:text>
  135. <xsl:value-of select="$newid"/>
  136. <xsl:text>/</xsl:text>
  137. <xsl:value-of select="$newid"/>
  138. <xsl:text>_Connect_42_</xsl:text>
  139. <xsl:value-of select="$element-id"/>
  140. </xsl:when>
  141. <xsl:otherwise>
  142. <xsl:value-of select="concat('#',$topic-id,'/',$topic-id,'_Connect_42_',$element-id)"/>
  143. </xsl:otherwise>
  144. </xsl:choose>
  145. </xsl:attribute>
  146. </xsl:template>
  147. <xsl:template match="*[contains(@class,' map/topicref ')]/@href">
  148. <xsl:copy-of select="."/>
  149. <xsl:attribute name="id">
  150. <xsl:variable name="fragmentId" select="substring-after(.,'#')"/>
  151. <xsl:variable name="idcount" select="count(../preceding::*[@href = current()][not(ancestor::*[contains(@class, ' map/reltable ')])]) + count(../ancestor::*[@href = current()])"/>
  152. <xsl:choose>
  153. <xsl:when test="$idcount &gt; 0">
  154. <xsl:value-of select="concat($fragmentId,'_ssol',$idcount)"/>
  155. </xsl:when>
  156. <xsl:otherwise>
  157. <xsl:value-of select="$fragmentId"/>
  158. </xsl:otherwise>
  159. </xsl:choose>
  160. </xsl:attribute>
  161. </xsl:template>
  162. <xsl:template match="*" mode="build-tree" priority="-1">
  163. <xsl:apply-templates mode="build-tree"/>
  164. </xsl:template>
  165. <xsl:template match="text()" mode="build-tree" priority="-1"/>
  166. <xsl:template match="*" priority="-1">
  167. <xsl:param name="newid"/>
  168. <xsl:copy>
  169. <xsl:apply-templates select="@*">
  170. <xsl:with-param name="newid" select="$newid"/>
  171. </xsl:apply-templates>
  172. <xsl:apply-templates select="*|text()|processing-instruction()">
  173. <xsl:with-param name="newid" select="$newid"/>
  174. </xsl:apply-templates>
  175. </xsl:copy>
  176. </xsl:template>
  177. <xsl:template match="@*" priority="-1">
  178. <xsl:copy-of select="."/>
  179. </xsl:template>
  180. <xsl:template match="processing-instruction()" priority="-1">
  181. <xsl:copy-of select="."/>
  182. </xsl:template>
  183. <xsl:template match="@id[not(parent::*[contains(@class, ' topic/topic ')])]">
  184. <xsl:param name="newid"/>
  185. <xsl:attribute name="id">
  186. <xsl:value-of select="$newid"/>
  187. <xsl:text>_Connect_42_</xsl:text>
  188. <xsl:value-of select="."/>
  189. </xsl:attribute>
  190. </xsl:template>
  191. <xsl:template name="isNotTopicRef">
  192. <xsl:param name="class"/>
  193. <xsl:choose>
  194. <xsl:when test="contains($class,' bookmap/abbrevlist ')"/>
  195. <xsl:when test="contains($class,' bookmap/amendments ')"/>
  196. <xsl:when test="contains($class,' bookmap/backmatter ')"/>
  197. <xsl:when test="contains($class,' bookmap/bookabstract ')"/>
  198. <xsl:when test="contains($class,' bookmap/booklist ')"/>
  199. <xsl:when test="contains($class,' bookmap/booklists ')"/>
  200. <xsl:when test="contains($class,' bookmap/colophon ')"/>
  201. <xsl:when test="contains($class,' bookmap/dedication ')"/>
  202. <xsl:when test="contains($class,' bookmap/figurelist ')"/>
  203. <xsl:when test="contains($class,' bookmap/frontmatter ')"/>
  204. <xsl:when test="contains($class,' bookmap/indexlist ')"/>
  205. <xsl:when test="contains($class,' bookmap/tablelist ')"/>
  206. <xsl:when test="contains($class,' bookmap/toc ')"/>
  207. <xsl:when test="contains($class,' bookmap/trademarklist ')"/>
  208. <xsl:when test="contains($class,' bookmap/glossarylist ') and not(*)">
  209. <!-- When glossarylist has children, don't treat it as an empty non-topic -->
  210. </xsl:when>
  211. <xsl:otherwise>
  212. <xsl:value-of select="'false'"/>
  213. </xsl:otherwise>
  214. </xsl:choose>
  215. </xsl:template>
  216. <xsl:template match="*[contains(@class, ' map/reltable ')]" mode="build-tree"/>
  217. </xsl:stylesheet>