related-links.xsl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. This file is part of the DITA Open Toolkit project.
  4. Copyright 2010 IBM Corporation
  5. See the accompanying LICENSE file for applicable license.
  6. -->
  7. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
  8. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  9. xmlns:related-links="http://dita-ot.sourceforge.net/ns/200709/related-links"
  10. exclude-result-prefixes="related-links xs">
  11. <xsl:key name="link"
  12. match="*[contains(@class, ' topic/link ')][not(ancestor::*[contains(@class, ' topic/linklist ')])]"
  13. use="related-links:link(.)"/>
  14. <xsl:key name="hideduplicates"
  15. match="*[contains(@class, ' topic/link ')][not(ancestor::*[contains(@class, ' topic/linklist ')])]
  16. [empty(@role) or @role = ('cousin', 'external', 'friend', 'other', 'sample', 'sibling')]"
  17. use="related-links:hideduplicates(.)"/>
  18. <xsl:function name="related-links:omit-from-unordered-links" as="xs:boolean">
  19. <xsl:param name="node" as="element()"/>
  20. <xsl:sequence select="$node/@role = ('child', 'descendant', 'next', 'previous', 'parent') or
  21. $node[@importance = 'required' and (empty(@role) or @role = ('sibling', 'friend', 'cousin'))] or
  22. $node/ancestor::*[contains(@class, ' topic/linklist ')]"/>
  23. </xsl:function>
  24. <xsl:function name="related-links:hideduplicates" as="xs:string">
  25. <xsl:param name="link" as="element()"/>
  26. <xsl:value-of select="concat($link/ancestor::*[contains(@class, ' topic/related-links ')]/parent::*[contains(@class, ' topic/topic ')]/@id,
  27. ' ',
  28. $link/@href,
  29. $link/@scope,
  30. $link/@audience,
  31. $link/@platform,
  32. $link/@product,
  33. $link/@otherprops,
  34. $link/@rev,
  35. $link/@type,
  36. normalize-space(string-join($link/*, ' ')))"/>
  37. </xsl:function>
  38. <xsl:function name="related-links:link" as="xs:string">
  39. <xsl:param name="link" as="element()"/>
  40. <xsl:value-of select="concat($link/ancestor::*[contains(@class, ' topic/related-links ')]/parent::*[contains(@class, ' topic/topic ')]/@id,
  41. ' ',
  42. $link/@href,
  43. $link/@type,
  44. $link/@role,
  45. $link/@platform,
  46. $link/@audience,
  47. $link/@importance,
  48. $link/@outputclass,
  49. $link/@keyref,
  50. $link/@scope,
  51. $link/@format,
  52. $link/@otherrole,
  53. $link/@product,
  54. $link/@otherprops,
  55. $link/@rev,
  56. $link/@class,
  57. $link/../@collection-type,
  58. normalize-space(string-join($link/*, ' ')))"/>
  59. </xsl:function>
  60. <!-- Ungrouped links have a priority of zero. (Can be overridden.) -->
  61. <xsl:template match="*[contains(@class, ' topic/link ')]" mode="related-links:get-group-priority"
  62. name="related-links:group-priority." as="xs:integer">
  63. <xsl:sequence select="0"/>
  64. </xsl:template>
  65. <!-- Ungrouped links belong to the no-name group. (Can be overridden.) -->
  66. <xsl:template match="*[contains(@class, ' topic/link ')]" mode="related-links:get-group" name="related-links:group." as="xs:string">
  67. <xsl:text/>
  68. </xsl:template>
  69. <!-- Without a group, links are emitted as-is. (Can be overridden.) -->
  70. <xsl:template match="*[contains(@class, ' topic/link ')]" mode="related-links:result-group"
  71. name="related-links:group-result." as="element()?">
  72. <xsl:param name="links" as="node()*"/>
  73. <xsl:if test="exists($links)">
  74. <linklist class="- topic/linklist " outputclass="relinfo relref">
  75. <xsl:sequence select="$links"/>
  76. </linklist>
  77. </xsl:if>
  78. </xsl:template>
  79. <!-- Ungrouped links have the default-mode template applied to them. (Can be overridden.) -->
  80. <xsl:template match="*[contains(@class, ' topic/link ')]" mode="related-links:link" name="related-links:link"
  81. as="element()*">
  82. <xsl:sequence select="."/>
  83. </xsl:template>
  84. <!-- Main entry point. -->
  85. <xsl:template match="*[contains(@class, ' topic/related-links ')]" mode="related-links:group-unordered-links"
  86. as="element()*">
  87. <!-- Node set. The set of nodes to group. -->
  88. <xsl:param name="nodes" as="element()*"/>
  89. <!-- Sent back to all callback templates as a parameter.-->
  90. <!-- XXX: Seems obsolete as the value is never used anywhere -->
  91. <xsl:param name="tunnel"/>
  92. <!-- Query all links for their group and priority. -->
  93. <xsl:variable name="group-priorities" as="xs:string">
  94. <xsl:call-template name="related-links:get-priorities">
  95. <xsl:with-param name="nodes" select="$nodes"/>
  96. <xsl:with-param name="tunnel" select="$tunnel"/>
  97. </xsl:call-template>
  98. </xsl:variable>
  99. <!-- Get order of groups based on priorities. -->
  100. <xsl:variable name="group-sequence" as="xs:string">
  101. <xsl:call-template name="related-links:get-sequence-from-priorities">
  102. <xsl:with-param name="priorities" select="$group-priorities"/>
  103. <xsl:with-param name="tunnel" select="$tunnel"/>
  104. </xsl:call-template>
  105. </xsl:variable>
  106. <!-- Process the links in each group in order. -->
  107. <xsl:call-template name="related-links:walk-groups">
  108. <xsl:with-param name="nodes" select="$nodes"/>
  109. <xsl:with-param name="group-sequence" select="$group-sequence"/>
  110. <xsl:with-param name="tunnel" select="$tunnel"/>
  111. </xsl:call-template>
  112. </xsl:template>
  113. <!-- Get the priorities and groups of every link. -->
  114. <!-- Produces a string like "2 task task ;3 concept concept ;1 reference reference ;0 topic ;",
  115. where the numbers are priorities of each group, and the space-delimited words
  116. are the groups and link types (link/@type) which belong to that group. -->
  117. <xsl:template name="related-links:get-priorities" as="xs:string">
  118. <xsl:param name="nodes" as="element()*"/>
  119. <xsl:param name="tunnel"/>
  120. <xsl:param name="partial-result" select="''" as="xs:string"/>
  121. <xsl:choose>
  122. <xsl:when test="exists($nodes)">
  123. <!-- Process each node one at a time. -->
  124. <xsl:variable name="node" select="$nodes[1]" as="element()"/>
  125. <xsl:variable name="node-group" as="xs:string">
  126. <xsl:apply-templates select="$node" mode="related-links:get-group">
  127. <xsl:with-param name="tunnel" select="$tunnel"/>
  128. </xsl:apply-templates>
  129. </xsl:variable>
  130. <xsl:variable name="node-priorty" as="xs:integer">
  131. <xsl:apply-templates select="$node" mode="related-links:get-group-priority">
  132. <xsl:with-param name="tunnel" select="$tunnel"/>
  133. </xsl:apply-templates>
  134. </xsl:variable>
  135. <xsl:call-template name="related-links:get-priorities">
  136. <xsl:with-param name="nodes" select="$nodes[position() != 1]"/>
  137. <xsl:with-param name="tunnel" select="$tunnel"/>
  138. <xsl:with-param name="partial-result">
  139. <xsl:value-of>
  140. <xsl:choose>
  141. <!-- This type has already been seen. -->
  142. <xsl:when
  143. test="contains($partial-result, concat(' ', $node-group, ' '))
  144. and contains($partial-result, concat(' ', $node/@type, ' '))">
  145. <xsl:value-of select="$partial-result"/>
  146. </xsl:when>
  147. <!-- This type has not been seen, but the base group has. -->
  148. <xsl:when test="contains($partial-result, concat(' ', $node-group, ' '))">
  149. <xsl:value-of select="substring-before($partial-result, concat(' ', $node-group, ' '))"/>
  150. <xsl:text> </xsl:text>
  151. <xsl:value-of select="$node-group"/>
  152. <xsl:text> </xsl:text>
  153. <xsl:value-of select="$node/@type"/>
  154. <xsl:text> </xsl:text>
  155. <xsl:value-of select="substring-after($partial-result, concat(' ', $node-group, ' '))"/>
  156. </xsl:when>
  157. <!-- Never seen this base group before (nor the type). -->
  158. <xsl:otherwise>
  159. <xsl:value-of select="$partial-result"/>
  160. <xsl:value-of select="$node-priorty"/>
  161. <xsl:text> </xsl:text>
  162. <xsl:value-of select="$node-group"/>
  163. <xsl:if test="$node-group != $node/@type">
  164. <xsl:text> </xsl:text>
  165. <xsl:value-of select="$node/@type"/>
  166. </xsl:if>
  167. <xsl:text> </xsl:text>
  168. <xsl:text>;</xsl:text>
  169. </xsl:otherwise>
  170. </xsl:choose>
  171. </xsl:value-of>
  172. </xsl:with-param>
  173. </xsl:call-template>
  174. </xsl:when>
  175. <xsl:otherwise>
  176. <xsl:value-of select="$partial-result"/>
  177. </xsl:otherwise>
  178. </xsl:choose>
  179. </xsl:template>
  180. <!-- Sort groups according to their priorities, removing duplicates. -->
  181. <!-- Takes a string returned by related-links:get-priorities and returns
  182. the groups and link types in decreasing order of priority
  183. (e.g., "concept concept;task task;reference reference; topic;"). -->
  184. <xsl:template name="related-links:get-sequence-from-priorities" as="xs:string">
  185. <xsl:param name="priorities" as="xs:string"/>
  186. <xsl:param name="tunnel"/>
  187. <xsl:param name="partial-result" select="''" as="xs:string"/>
  188. <xsl:choose>
  189. <xsl:when test="contains($priorities, ';')">
  190. <xsl:call-template name="related-links:get-best-priority-in-sequence">
  191. <xsl:with-param name="priorities" select="$priorities"/>
  192. <xsl:with-param name="tunnel" select="$tunnel"/>
  193. <xsl:with-param name="partial-result" select="$partial-result"/>
  194. </xsl:call-template>
  195. </xsl:when>
  196. <xsl:otherwise>
  197. <xsl:value-of select="$partial-result"/>
  198. </xsl:otherwise>
  199. </xsl:choose>
  200. </xsl:template>
  201. <!-- Find the highest-priority group remaining in the list of priorities. -->
  202. <xsl:template name="related-links:get-best-priority-in-sequence" as="xs:string">
  203. <!-- semicolon separated list of space separated tuple of priority integer and group name -->
  204. <xsl:param name="priorities" as="xs:string"/>
  205. <xsl:param name="tunnel"/>
  206. <xsl:param name="partial-result" as="xs:string"/>
  207. <xsl:param name="best-group" select="'#none#'" as="xs:string"/>
  208. <xsl:param name="best-priority" select="-1" as="xs:integer"/>
  209. <!-- semicolon separated list of space separated tuple of priority integer and group name -->
  210. <xsl:param name="lesser-priorities" select="''" as="xs:string"/>
  211. <xsl:choose>
  212. <xsl:when test="contains($priorities, ';')">
  213. <xsl:choose>
  214. <!-- First group always wins. -->
  215. <xsl:when test="$best-group = '#none#'">
  216. <xsl:call-template name="related-links:get-best-priority-in-sequence">
  217. <xsl:with-param name="priorities" select="substring-after($priorities, ';')"/>
  218. <xsl:with-param name="tunnel" select="$tunnel"/>
  219. <xsl:with-param name="partial-result" select="$partial-result"/>
  220. <xsl:with-param name="best-priority" select="xs:integer(substring-before(substring-before($priorities, ';'), ' '))"/>
  221. <xsl:with-param name="best-group" select="substring-after(substring-before($priorities, ';'), ' ')"/>
  222. <xsl:with-param name="lesser-priorities" select="$lesser-priorities"/>
  223. </xsl:call-template>
  224. </xsl:when>
  225. <!-- Higher-priority group found; shunt best-so-far to lesser priorities and continue. -->
  226. <xsl:when test="xs:integer(substring-before(substring-before($priorities, ';'), ' ')) > $best-priority">
  227. <xsl:call-template name="related-links:get-best-priority-in-sequence">
  228. <xsl:with-param name="priorities" select="substring-after($priorities, ';')"/>
  229. <xsl:with-param name="tunnel" select="$tunnel"/>
  230. <xsl:with-param name="partial-result" select="$partial-result"/>
  231. <xsl:with-param name="best-priority" select="xs:integer(substring-before(substring-before($priorities, ';'), ' '))"/>
  232. <xsl:with-param name="best-group" select="substring-after(substring-before($priorities, ';'), ' ')"/>
  233. <xsl:with-param name="lesser-priorities" select="concat($lesser-priorities, $best-priority, ' ', $best-group, ';')"/>
  234. </xsl:call-template>
  235. </xsl:when>
  236. <!-- Best-so-far priority is still supreme. -->
  237. <xsl:otherwise>
  238. <xsl:call-template name="related-links:get-best-priority-in-sequence">
  239. <xsl:with-param name="priorities" select="substring-after($priorities, ';')"/>
  240. <xsl:with-param name="tunnel" select="$tunnel"/>
  241. <xsl:with-param name="partial-result" select="$partial-result"/>
  242. <xsl:with-param name="best-priority" select="$best-priority"/>
  243. <xsl:with-param name="best-group" select="$best-group"/>
  244. <xsl:with-param name="lesser-priorities" select="concat($lesser-priorities, substring-before($priorities, ';'), ';')"/>
  245. </xsl:call-template>
  246. </xsl:otherwise>
  247. </xsl:choose>
  248. </xsl:when>
  249. <!-- Best priority found. -->
  250. <xsl:otherwise>
  251. <xsl:call-template name="related-links:get-sequence-from-priorities">
  252. <xsl:with-param name="priorities" select="$lesser-priorities"/>
  253. <xsl:with-param name="tunnel" select="$tunnel"/>
  254. <xsl:with-param name="partial-result">
  255. <xsl:choose>
  256. <!-- Duplicate; just move on. (Should not happen.) -->
  257. <xsl:when test="contains(concat(';', $partial-result), concat(';', $best-group, ';'))">
  258. <xsl:value-of select="$partial-result"/>
  259. </xsl:when>
  260. <!-- Add group to list and move on. -->
  261. <xsl:otherwise>
  262. <xsl:value-of select="concat($partial-result, $best-group, ';')"/>
  263. </xsl:otherwise>
  264. </xsl:choose>
  265. </xsl:with-param>
  266. </xsl:call-template>
  267. </xsl:otherwise>
  268. </xsl:choose>
  269. </xsl:template>
  270. <!-- Process each group in turn. -->
  271. <xsl:template name="related-links:walk-groups" as="element()*">
  272. <xsl:param name="nodes" as="element()*"/>
  273. <xsl:param name="tunnel"/>
  274. <!-- semicolon separate list -->
  275. <xsl:param name="group-sequence" select="''" as="xs:string"/>
  276. <xsl:choose>
  277. <xsl:when test="contains($group-sequence, ';')">
  278. <xsl:call-template name="related-links:do-group">
  279. <xsl:with-param name="nodes" select="$nodes"/>
  280. <xsl:with-param name="tunnel" select="$tunnel"/>
  281. <xsl:with-param name="group" select="substring-before($group-sequence, ';')"/>
  282. </xsl:call-template>
  283. <xsl:call-template name="related-links:walk-groups">
  284. <xsl:with-param name="nodes" select="$nodes"/>
  285. <xsl:with-param name="tunnel" select="$tunnel"/>
  286. <xsl:with-param name="group-sequence" select="substring-after($group-sequence, ';')"/>
  287. </xsl:call-template>
  288. </xsl:when>
  289. </xsl:choose>
  290. </xsl:template>
  291. <!-- Process each group. -->
  292. <xsl:template name="related-links:do-group" as="element()?">
  293. <xsl:param name="nodes" as="element()*"/>
  294. <xsl:param name="tunnel"/>
  295. <!-- space separated list -->
  296. <xsl:param name="group" as="xs:string"/>
  297. <!-- Process the links belonging to that group. -->
  298. <xsl:variable name="group-nodes" select="$nodes[contains(concat(' ', $group), concat(' ', @type, ' '))]" as="element()*"/>
  299. <!-- Let the group wrap all its links in additional elements. -->
  300. <xsl:apply-templates select="$group-nodes[1]" mode="related-links:result-group">
  301. <xsl:with-param name="links" as="node()*">
  302. <xsl:apply-templates select="$group-nodes" mode="related-links:link">
  303. <xsl:sort
  304. select="
  305. 10 * number(@role = 'parent') +
  306. 9 * number(@role = 'ancestor') +
  307. 8 * number(@role = 'child') +
  308. 7 * number(@role = 'descendant') +
  309. 6 * number(@role = 'next') +
  310. 5 * number(@role = 'previous') +
  311. 4 * number(@role = 'sibling') +
  312. 3 * number(@role = 'cousin') +
  313. 2 * number(@role = 'friend') +
  314. 1 * number(@role = 'other')"
  315. data-type="number" order="descending"/>
  316. <!-- All @role='other' have to go together, darn. -->
  317. <xsl:sort select="@otherrole" data-type="text"/>
  318. <xsl:with-param name="tunnel" select="$tunnel"/>
  319. </xsl:apply-templates>
  320. </xsl:with-param>
  321. <xsl:with-param name="tunnel" select="$tunnel"/>
  322. </xsl:apply-templates>
  323. </xsl:template>
  324. </xsl:stylesheet>