topicmerge.xsl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!-- This file is part of the DITA Open Toolkit project hosted on
  3. Sourceforge.net. See the accompanying license.txt file for
  4. applicable licenses.-->
  5. <!-- (c) Copyright IBM Corp. 2004, 2005 All Rights Reserved. -->
  6. <!-- book.xsl
  7. | Merge DITA topics with "validation" of topic property
  8. *-->
  9. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  10. <!-- Include error message template -->
  11. <xsl:import href="plugin:org.dita.base:xsl/common/output-message.xsl"/>
  12. <!-- Set the prefix for error message numbers -->
  13. <!-- Deprecated since 2.3 -->
  14. <xsl:variable name="msgprefix">DOTX</xsl:variable>
  15. <xsl:variable name="xml-path"></xsl:variable>
  16. <xsl:output method="xml" encoding="utf-8" />
  17. <xsl:template match="/*">
  18. <xsl:element name="{name()}">
  19. <xsl:apply-templates select="@*" mode="copy-element"/>
  20. <xsl:apply-templates select="*"/>
  21. </xsl:element>
  22. </xsl:template>
  23. <xsl:template match="/*/*[contains(@class,' map/topicmeta ')]" priority="1">
  24. <xsl:apply-templates select="." mode="copy-element"/>
  25. </xsl:template>
  26. <xsl:template match="/*[contains(@class,' map/map ')]/*[contains(@class,' topic/title ')]">
  27. <xsl:apply-templates select="." mode="copy-element"/>
  28. </xsl:template>
  29. <xsl:template match="*[contains(@class,' map/topicmeta ')]"/>
  30. <xsl:template match="*[contains(@class,' map/navref ')]"/>
  31. <xsl:template match="*[contains(@class,' map/reltable ')]"/>
  32. <xsl:template match="*[contains(@class,' map/anchor ')]"/>
  33. <xsl:template match="*[contains(@class,' map/topicref ')][@href][not(@href='')][not(@print='no')]">
  34. <xsl:variable name="topicrefClass"><xsl:value-of select="@class"/></xsl:variable>
  35. <xsl:comment>Start of imbed for <xsl:value-of select="@href"/></xsl:comment>
  36. <xsl:choose>
  37. <xsl:when test="@format and not(@format='dita')">
  38. <!-- Topicref to non-dita files will be ingored in PDF transformation -->
  39. <xsl:call-template name="output-message">
  40. <xsl:with-param name="id" select="'DOTX049I'"/>
  41. </xsl:call-template>
  42. </xsl:when>
  43. <xsl:when test="contains(@href,'#')">
  44. <xsl:variable name="sourcefile"><xsl:value-of select="substring-before(@href,'#')"/></xsl:variable>
  45. <xsl:variable name="sourcetopic"><xsl:value-of select="substring-after(@href,'#')"/></xsl:variable>
  46. <xsl:variable name="targetName"><xsl:value-of select="name(document($sourcefile,/)//*[@id=$sourcetopic][contains(@class,' topic/topic ')][1])"/></xsl:variable>
  47. <xsl:if test="$targetName and not($targetName='')">
  48. <xsl:element name="{$targetName}">
  49. <xsl:apply-templates select="document($sourcefile,/)//*[@id=$sourcetopic][contains(@class,' topic/topic ')][1]/@*" mode="copy-element"/>
  50. <xsl:attribute name="refclass"><xsl:value-of select="$topicrefClass"/></xsl:attribute>
  51. <xsl:apply-templates select="document($sourcefile,/)//*[@id=$sourcetopic][contains(@class,' topic/topic ')][1]/*" mode="copy-element">
  52. <xsl:with-param name="src-file"><xsl:value-of select="$sourcefile"/></xsl:with-param>
  53. </xsl:apply-templates>
  54. <xsl:apply-templates/>
  55. </xsl:element>
  56. </xsl:if>
  57. </xsl:when>
  58. <!-- If the target is a topic, as opposed to a ditabase mixed file -->
  59. <xsl:when test="document(@href,/)/*[contains(@class,' topic/topic ')]">
  60. <xsl:variable name="targetName"><xsl:value-of select="name(document(@href,/)/*)"/></xsl:variable>
  61. <xsl:if test="$targetName and not($targetName='')">
  62. <xsl:element name="{$targetName}">
  63. <xsl:apply-templates select="document(@href,/)/*/@*" mode="copy-element"/>
  64. <xsl:attribute name="refclass"><xsl:value-of select="$topicrefClass"/></xsl:attribute>
  65. <!-- If the root element of the topic does not contain an id attribute, then generate one.
  66. Later, we will use these id attributes as anchors for PDF bookmarks. -->
  67. <xsl:if test="not(document(@href,/)/*/@id)">
  68. <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
  69. </xsl:if>
  70. <xsl:apply-templates select="document(@href,/)/*/*" mode="copy-element">
  71. <xsl:with-param name="src-file"><xsl:value-of select="@href"/></xsl:with-param>
  72. </xsl:apply-templates>
  73. <xsl:apply-templates/>
  74. </xsl:element>
  75. </xsl:if>
  76. </xsl:when>
  77. <!-- Otherwise: pointing to ditabase container; output each topic in the ditabase file.
  78. The refclass value is copied to each of the main topics.
  79. If this topicref has children, they will be treated as children of the <dita> wrapper.
  80. This is the same as saving them as peers of the topics in the ditabase file. -->
  81. <xsl:otherwise>
  82. <xsl:for-each select="document(@href,/)/*/*">
  83. <xsl:element name="{name()}">
  84. <xsl:apply-templates select="@*" mode="copy-element"/>
  85. <xsl:attribute name="refclass"><xsl:value-of select="$topicrefClass"/></xsl:attribute>
  86. <xsl:apply-templates select="*" mode="copy-element"/>
  87. </xsl:element>
  88. </xsl:for-each>
  89. <xsl:apply-templates/>
  90. </xsl:otherwise>
  91. </xsl:choose>
  92. </xsl:template>
  93. <xsl:template match="*[contains(@class,' map/topicref ')][not(@href)]">
  94. <xsl:element name="{name()}">
  95. <xsl:apply-templates select="@*" mode="copy-element"/>
  96. <xsl:apply-templates/>
  97. </xsl:element>
  98. </xsl:template>
  99. <xsl:template match="*|@*|comment()|processing-instruction()|text()" mode="copy-element">
  100. <xsl:param name="src-file"></xsl:param>
  101. <xsl:copy>
  102. <xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()" mode="copy-element">
  103. <xsl:with-param name="src-file"><xsl:value-of select="$src-file"/></xsl:with-param>
  104. </xsl:apply-templates>
  105. </xsl:copy>
  106. </xsl:template>
  107. <xsl:template match="@id" mode="copy-element">
  108. <xsl:attribute name="id"><xsl:value-of select="generate-id(.)"/></xsl:attribute>
  109. </xsl:template>
  110. <xsl:template match="@href" mode="copy-element" priority="1">
  111. <xsl:param name="src-file"></xsl:param>
  112. <xsl:variable name="file-path">
  113. <xsl:call-template name="get-file-path">
  114. <xsl:with-param name="src-file">
  115. <xsl:value-of select="$src-file"/>
  116. </xsl:with-param>
  117. </xsl:call-template>
  118. <xsl:value-of select="."/>
  119. </xsl:variable>
  120. <xsl:variable name="file-path-new">
  121. <xsl:call-template name="normalize-path">
  122. <xsl:with-param name="file-path">
  123. <xsl:value-of select="translate($file-path,'\','/')"/>
  124. </xsl:with-param>
  125. </xsl:call-template>
  126. </xsl:variable>
  127. <xsl:choose>
  128. <xsl:when test="contains(.,'://') or ../@scope='external' or ../@scope='peer'">
  129. <xsl:copy/>
  130. </xsl:when>
  131. <xsl:when test="(parent::*[contains(@class,' topic/xref ')] or parent::*[contains(@class,' topic/link ')]) and (not(../@format) or ../@format='dita')">
  132. <xsl:choose>
  133. <xsl:when test="starts-with(.,'#')">
  134. <xsl:variable name="refer-path" select="substring-after(.,'#')"/>
  135. <xsl:choose>
  136. <xsl:when test="contains($refer-path,'/')">
  137. <xsl:variable name="topic-id" select="substring-before($refer-path,'/')"/>
  138. <xsl:variable name="target-id" select="substring-after($refer-path,'/')"/>
  139. <xsl:variable name="href-value">
  140. <xsl:value-of select="generate-id(//*[contains(@class,' topic/topic ')][@id=$topic-id]//*[@id=$target-id]/@id)"/>
  141. </xsl:variable>
  142. <xsl:if test="not($href-value='')">
  143. <xsl:attribute name="href"><xsl:text>#</xsl:text><xsl:value-of select="$href-value"/></xsl:attribute>
  144. </xsl:if>
  145. </xsl:when>
  146. <xsl:otherwise>
  147. <xsl:variable name="href-value">
  148. <xsl:value-of select="generate-id(//*[contains(@class,' topic/topic ')][@id=$refer-path]/@id)"/>
  149. </xsl:variable>
  150. <xsl:if test="not($href-value='')">
  151. <xsl:attribute name="href"><xsl:text>#</xsl:text><xsl:value-of select="$href-value"/></xsl:attribute>
  152. </xsl:if>
  153. </xsl:otherwise>
  154. </xsl:choose>
  155. </xsl:when>
  156. <xsl:when test="contains(.,'#')">
  157. <xsl:variable name="file-name" select="substring-before(.,'#')"/>
  158. <xsl:variable name="refer-path" select="substring-after(.,'#')"/>
  159. <xsl:variable name="file-name-doc" select="document($file-name,/)"/>
  160. <xsl:if test="$file-name-doc and not($file-name-doc='')">
  161. <xsl:choose>
  162. <xsl:when test="contains($refer-path,'/')">
  163. <xsl:variable name="topic-id" select="substring-before($refer-path,'/')"/>
  164. <xsl:variable name="target-id" select="substring-after($refer-path,'/')"/>
  165. <xsl:variable name="href-value">
  166. <xsl:value-of select="generate-id($file-name-doc//*[contains(@class,' topic/topic ')][@id=$topic-id]//*[@id=$target-id]/@id)"/>
  167. </xsl:variable>
  168. <xsl:if test="not($href-value='')">
  169. <xsl:attribute name="href"><xsl:text>#</xsl:text><xsl:value-of select="$href-value"/></xsl:attribute>
  170. </xsl:if>
  171. </xsl:when>
  172. <xsl:otherwise>
  173. <xsl:variable name="href-value">
  174. <xsl:value-of select="generate-id($file-name-doc//*[contains(@class,' topic/topic ')][@id=$refer-path]/@id)"/>
  175. </xsl:variable>
  176. <xsl:if test="not($href-value='')">
  177. <xsl:attribute name="href"><xsl:text>#</xsl:text><xsl:value-of select="$href-value"/></xsl:attribute>
  178. </xsl:if>
  179. </xsl:otherwise>
  180. </xsl:choose>
  181. </xsl:if>
  182. </xsl:when>
  183. <xsl:otherwise>
  184. <xsl:variable name="current-doc" select="document(.,/)"/>
  185. <xsl:if test="$current-doc and not($current-doc='')">
  186. <xsl:choose>
  187. <xsl:when test="$current-doc//*[contains(@class,' topic/topic ')]/@id">
  188. <xsl:attribute name="href"><xsl:text>#</xsl:text><xsl:value-of select="generate-id(($current-doc//*[contains(@class,' topic/topic ')])[1]/@id)"/></xsl:attribute>
  189. </xsl:when>
  190. <xsl:otherwise><xsl:text>#</xsl:text><xsl:value-of select="generate-id(($current-doc//*[contains(@class,' topic/topic ')])[1])"/></xsl:otherwise>
  191. </xsl:choose>
  192. </xsl:if>
  193. </xsl:otherwise>
  194. </xsl:choose>
  195. </xsl:when>
  196. <xsl:otherwise>
  197. <xsl:attribute name="href">
  198. <xsl:value-of select="$file-path-new"/>
  199. </xsl:attribute>
  200. </xsl:otherwise>
  201. </xsl:choose>
  202. </xsl:template>
  203. <xsl:template name="get-file-path">
  204. <xsl:param name="src-file"/>
  205. <xsl:if test="contains($src-file,'/')">
  206. <xsl:value-of select="substring-before($src-file,'/')"/>
  207. <xsl:text>/</xsl:text>
  208. <xsl:call-template name="get-file-path">
  209. <xsl:with-param name="src-file">
  210. <xsl:value-of select="substring-after($src-file,'/')"/>
  211. </xsl:with-param>
  212. </xsl:call-template>
  213. </xsl:if>
  214. </xsl:template>
  215. <xsl:template name="normalize-path">
  216. <xsl:param name="file-path" />
  217. <xsl:choose>
  218. <xsl:when test="contains($file-path,'..')">
  219. <xsl:variable name="firstdir" select="substring-before($file-path, '/')" />
  220. <xsl:variable name="newpath" select="substring-after($file-path,'/')" />
  221. <xsl:choose>
  222. <xsl:when test="$firstdir='..'">
  223. <xsl:text>../</xsl:text>
  224. <xsl:call-template name="normalize-path">
  225. <xsl:with-param name="file-path">
  226. <xsl:value-of select="$newpath"/>
  227. </xsl:with-param>
  228. </xsl:call-template>
  229. </xsl:when>
  230. <xsl:otherwise>
  231. <xsl:variable name="beforedotdot" select="substring-before($file-path,'/..')"></xsl:variable>
  232. <xsl:variable name="beforedotdotparent" >
  233. <xsl:call-template name="parent-path">
  234. <xsl:with-param name="pathname" select="$beforedotdot" />
  235. </xsl:call-template>
  236. </xsl:variable>
  237. <xsl:variable name="afterdotdot" select="substring-after($file-path,'../')"></xsl:variable>
  238. <xsl:call-template name="normalize-path">
  239. <xsl:with-param name="file-path">
  240. <xsl:value-of select="concat($beforedotdotparent,$afterdotdot)"/>
  241. </xsl:with-param>
  242. </xsl:call-template>
  243. </xsl:otherwise>
  244. </xsl:choose>
  245. </xsl:when>
  246. <xsl:otherwise>
  247. <xsl:value-of select="$file-path"></xsl:value-of>
  248. </xsl:otherwise>
  249. </xsl:choose>
  250. </xsl:template>
  251. <xsl:template name="parent-path">
  252. <xsl:param name="pathname" />
  253. <xsl:choose>
  254. <xsl:when test="contains($pathname, '/')">
  255. <xsl:value-of select="substring-before($pathname, '/')"/>
  256. <xsl:text>/</xsl:text>
  257. <xsl:call-template name="parent-path">
  258. <xsl:with-param name="pathname" select="substring-after($pathname,'/')"/>
  259. </xsl:call-template>
  260. </xsl:when>
  261. </xsl:choose>
  262. </xsl:template>
  263. <xsl:template match="processing-instruction()">
  264. <xsl:copy></xsl:copy>
  265. </xsl:template>
  266. </xsl:stylesheet>