topicmergeImpl.xsl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.
  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. xmlns:ot-placeholder="http://suite-sol.com/namespaces/ot-placeholder"
  33. xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
  34. exclude-result-prefixes="xs dita-ot">
  35. <xsl:import href="plugin:org.dita.base:xsl/common/dita-utilities.xsl"/>
  36. <xsl:import href="plugin:org.dita.base:xsl/common/output-message.xsl"/>
  37. <!-- Deprecated since 2.3 -->
  38. <xsl:variable name="msgprefix" select="'PDFX'"/>
  39. <xsl:output indent="no"/>
  40. <xsl:key name="topic" match="dita-merge/*[contains(@class,' topic/topic ')]" use="concat('#',@id)"/>
  41. <xsl:key name="topic" match="dita-merge/dita" use="concat('#',*[contains(@class, ' topic/topic ')][1]/@id)"/>
  42. <xsl:key name="topicref" match="//*[contains(@class,' map/topicref ')]" use="generate-id()"/>
  43. <!--
  44. <xsl:template match="/">
  45. <xsl:copy-of select="."/>
  46. </xsl:template>
  47. -->
  48. <xsl:template match="dita-merge">
  49. <xsl:variable name="map" select="(*[contains(@class,' map/map ')])[1]"/>
  50. <xsl:element name="{name($map)}">
  51. <xsl:copy-of select="$map/@*"/>
  52. <xsl:apply-templates select="$map" mode="build-tree"/>
  53. </xsl:element>
  54. </xsl:template>
  55. <xsl:template match="dita-merge/*[contains(@class,' map/map ')]" mode="build-tree">
  56. <opentopic:map xmlns:opentopic="http://www.idiominc.com/opentopic">
  57. <xsl:apply-templates/>
  58. </opentopic:map>
  59. <xsl:apply-templates mode="build-tree"/>
  60. </xsl:template>
  61. <xsl:template match="*[contains(@class,' map/topicref ')]" mode="build-tree">
  62. <xsl:choose>
  63. <xsl:when test="not(normalize-space(@first_topic_id) = '')">
  64. <xsl:apply-templates select="key('topic',@first_topic_id)">
  65. <xsl:with-param name="parentId" select="generate-id()"/>
  66. </xsl:apply-templates>
  67. <xsl:if test="@first_topic_id != @href">
  68. <xsl:apply-templates select="key('topic',@href)">
  69. <xsl:with-param name="parentId" select="generate-id()"/>
  70. </xsl:apply-templates>
  71. </xsl:if>
  72. </xsl:when>
  73. <xsl:when test="not(normalize-space(@href) = '')">
  74. <xsl:apply-templates select="key('topic',@href)">
  75. <xsl:with-param name="parentId" select="generate-id()"/>
  76. </xsl:apply-templates>
  77. </xsl:when>
  78. <xsl:when test="contains(@class, ' bookmap/part ') or
  79. (normalize-space(@navtitle) != '' or *[contains(@class,' map/topicmeta ')]/*[contains(@class,' topic/navtitle ')])">
  80. <xsl:variable name="isNotTopicRef" as="xs:boolean">
  81. <xsl:call-template name="isNotTopicRef"/>
  82. </xsl:variable>
  83. <xsl:if test="not($isNotTopicRef)">
  84. <topic id="{generate-id()}" class="+ topic/topic pdf2-d/placeholder ">
  85. <title class="- topic/title ">
  86. <xsl:choose>
  87. <xsl:when test="*[contains(@class,' map/topicmeta ')]/*[contains(@class,' topic/navtitle ')]">
  88. <xsl:copy-of select="*[contains(@class,' map/topicmeta ')]/*[contains(@class,' topic/navtitle ')]/node()"/>
  89. </xsl:when>
  90. <xsl:when test="@navtitle">
  91. <xsl:value-of select="@navtitle"/>
  92. </xsl:when>
  93. </xsl:choose>
  94. </title>
  95. <!--body class=" topic/body "/-->
  96. <xsl:apply-templates mode="build-tree"/>
  97. </topic>
  98. </xsl:if>
  99. </xsl:when>
  100. <xsl:otherwise>
  101. <xsl:apply-templates mode="build-tree"/>
  102. </xsl:otherwise>
  103. </xsl:choose>
  104. </xsl:template>
  105. <xsl:template match="*[@print = 'no']" priority="5" mode="build-tree"/>
  106. <xsl:template match="*[contains(@class,' bookmap/backmatter ')] |
  107. *[contains(@class,' bookmap/booklists ')] |
  108. *[contains(@class,' bookmap/frontmatter ')]" priority="2" mode="build-tree">
  109. <xsl:apply-templates mode="build-tree"/>
  110. </xsl:template>
  111. <xsl:template match="*[contains(@class,' bookmap/toc ')][not(@href)]" priority="2" mode="build-tree">
  112. <ot-placeholder:toc id="{generate-id()}">
  113. <xsl:apply-templates mode="build-tree"/>
  114. </ot-placeholder:toc>
  115. </xsl:template>
  116. <xsl:template match="*[contains(@class,' bookmap/indexlist ')][not(@href)]" priority="2" mode="build-tree">
  117. <ot-placeholder:indexlist id="{generate-id()}">
  118. <xsl:apply-templates mode="build-tree"/>
  119. </ot-placeholder:indexlist>
  120. </xsl:template>
  121. <xsl:template match="*[contains(@class,' bookmap/glossarylist ')][not(@href)]" priority="2" mode="build-tree">
  122. <ot-placeholder:glossarylist id="{generate-id()}">
  123. <xsl:apply-templates mode="build-tree"/>
  124. </ot-placeholder:glossarylist>
  125. </xsl:template>
  126. <xsl:template match="*[contains(@class,' bookmap/tablelist ')][not(@href)]" priority="2" mode="build-tree">
  127. <ot-placeholder:tablelist id="{generate-id()}">
  128. <xsl:apply-templates mode="build-tree"/>
  129. </ot-placeholder:tablelist>
  130. </xsl:template>
  131. <xsl:template match="*[contains(@class,' bookmap/figurelist ')][not(@href)]" priority="2" mode="build-tree">
  132. <ot-placeholder:figurelist id="{generate-id()}">
  133. <xsl:apply-templates mode="build-tree"/>
  134. </ot-placeholder:figurelist>
  135. </xsl:template>
  136. <xsl:template match="*[contains(@class, ' map/topicref ') and @print='no']" priority="6"/>
  137. <xsl:template match="*[contains(@class,' topic/topic ')] | dita-merge/dita">
  138. <xsl:param name="parentId"/>
  139. <xsl:variable name="idcount">
  140. <!--for-each is used to change context. There's only one entry with a key of $parentId-->
  141. <xsl:for-each select="key('topicref',$parentId)">
  142. <xsl:value-of select="count(preceding::*[@href = current()/@href][not(ancestor::*[contains(@class, ' map/reltable ')])]) + count(ancestor::*[@href = current()/@href])"/>
  143. </xsl:for-each>
  144. </xsl:variable>
  145. <xsl:copy>
  146. <xsl:apply-templates select="@*[name() != 'id']"/>
  147. <xsl:variable name="new_id">
  148. <xsl:choose>
  149. <xsl:when test="number($idcount) &gt; 0">
  150. <xsl:value-of select="concat(@id,'_ssol',$idcount)"/>
  151. </xsl:when>
  152. <xsl:otherwise>
  153. <xsl:value-of select="@id"/>
  154. </xsl:otherwise>
  155. </xsl:choose>
  156. </xsl:variable>
  157. <xsl:attribute name="id">
  158. <xsl:value-of select="$new_id"/>
  159. </xsl:attribute>
  160. <xsl:apply-templates>
  161. <xsl:with-param name="newid" select="$new_id"/>
  162. </xsl:apply-templates>
  163. <xsl:apply-templates select="key('topicref',$parentId)/*" mode="build-tree"/>
  164. </xsl:copy>
  165. </xsl:template>
  166. <!-- Linkless topicref or topichead -->
  167. <xsl:template match="*[contains(@class,' map/topicref ')][not(@href)]" priority="5">
  168. <xsl:param name="newid"/>
  169. <xsl:copy>
  170. <xsl:attribute name="id">
  171. <xsl:value-of select="generate-id()"/>
  172. </xsl:attribute>
  173. <xsl:apply-templates select="@*">
  174. <xsl:with-param name="newid" select="$newid"/>
  175. </xsl:apply-templates>
  176. <xsl:apply-templates select="*|text()|processing-instruction()">
  177. <xsl:with-param name="newid" select="$newid"/>
  178. </xsl:apply-templates>
  179. </xsl:copy>
  180. </xsl:template>
  181. <xsl:template match="*[contains(@class,' map/topicref ')]/@id" priority="5"/>
  182. <xsl:template match="@href">
  183. <xsl:param name="newid"/>
  184. <xsl:variable name="topic-id" select="dita-ot:get-topic-id(.)" as="xs:string?"/>
  185. <xsl:variable name="element-id" select="dita-ot:get-element-id(.)" as="xs:string?"/>
  186. <xsl:attribute name="href">
  187. <xsl:choose>
  188. <xsl:when test="empty($element-id) or not(starts-with(., '#unique'))">
  189. <xsl:value-of select="."/>
  190. </xsl:when>
  191. <xsl:when test="ancestor::*[contains(@class, ' topic/topic ')][1]/@id = $topic-id">
  192. <xsl:text>#</xsl:text>
  193. <xsl:value-of select="$newid"/>
  194. <xsl:text>/</xsl:text>
  195. <xsl:value-of select="$newid"/>
  196. <xsl:text>_Connect_42_</xsl:text>
  197. <xsl:value-of select="$element-id"/>
  198. </xsl:when>
  199. <xsl:otherwise>
  200. <xsl:value-of select="concat('#',$topic-id,'/',$topic-id,'_Connect_42_',$element-id)"/>
  201. </xsl:otherwise>
  202. </xsl:choose>
  203. </xsl:attribute>
  204. </xsl:template>
  205. <xsl:template match="*[contains(@class,' map/topicref ')]/@href">
  206. <xsl:copy-of select="."/>
  207. <xsl:attribute name="id">
  208. <xsl:variable name="fragmentId" select="substring-after(.,'#')"/>
  209. <xsl:variable name="idcount" select="count(../preceding::*[@href = current()][not(ancestor::*[contains(@class, ' map/reltable ')])]) + count(../ancestor::*[@href = current()])"/>
  210. <xsl:choose>
  211. <xsl:when test="$idcount &gt; 0">
  212. <xsl:value-of select="concat($fragmentId,'_ssol',$idcount)"/>
  213. </xsl:when>
  214. <xsl:otherwise>
  215. <xsl:value-of select="$fragmentId"/>
  216. </xsl:otherwise>
  217. </xsl:choose>
  218. </xsl:attribute>
  219. </xsl:template>
  220. <xsl:template match="*" mode="build-tree" priority="-1">
  221. <xsl:apply-templates mode="build-tree"/>
  222. </xsl:template>
  223. <xsl:template match="text()" mode="build-tree" priority="-1"/>
  224. <xsl:template match="*" priority="-1">
  225. <xsl:param name="newid"/>
  226. <xsl:copy>
  227. <xsl:apply-templates select="@*">
  228. <xsl:with-param name="newid" select="$newid"/>
  229. </xsl:apply-templates>
  230. <xsl:apply-templates select="*|text()|processing-instruction()">
  231. <xsl:with-param name="newid" select="$newid"/>
  232. </xsl:apply-templates>
  233. </xsl:copy>
  234. </xsl:template>
  235. <xsl:template match="@*" priority="-1">
  236. <xsl:copy-of select="."/>
  237. </xsl:template>
  238. <xsl:template match="processing-instruction()" priority="-1">
  239. <xsl:copy-of select="."/>
  240. </xsl:template>
  241. <xsl:key name="duplicate-id"
  242. match="*[not(contains(@class, ' topic/topic '))]/@id"
  243. use="concat(ancestor::*[contains(@class, ' topic/topic ')][1]/@id, '|', .)"/>
  244. <xsl:template match="@id[not(parent::*[contains(@class, ' topic/topic ')])]">
  245. <xsl:param name="newid"/>
  246. <xsl:attribute name="id">
  247. <xsl:value-of select="$newid"/>
  248. <xsl:text>_Connect_42_</xsl:text>
  249. <xsl:value-of select="."/>
  250. <xsl:variable name="current-id" select="concat(ancestor::*[contains(@class, ' topic/topic ')][1]/@id, '|', .)"/>
  251. <xsl:if test="not(generate-id(.) = generate-id(key('duplicate-id', $current-id)[1]))">
  252. <xsl:text>_</xsl:text>
  253. <xsl:value-of select="generate-id()"/>
  254. </xsl:if>
  255. </xsl:attribute>
  256. </xsl:template>
  257. <xsl:template name="isNotTopicRef" as="xs:boolean">
  258. <xsl:param name="class" select="@class"/>
  259. <xsl:sequence select="contains($class,' bookmap/abbrevlist ')
  260. or contains($class,' bookmap/amendments ')
  261. or contains($class,' bookmap/bookabstract ')
  262. or contains($class,' bookmap/booklist ')
  263. or contains($class,' bookmap/colophon ')
  264. or contains($class,' bookmap/dedication ')
  265. or contains($class,' bookmap/tablelist ')
  266. or contains($class,' bookmap/trademarklist ')
  267. or contains($class,' bookmap/figurelist ')"/>
  268. </xsl:template>
  269. <xsl:template match="*[contains(@class, ' map/reltable ')]" mode="build-tree"/>
  270. </xsl:stylesheet>