related-links.xsl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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:copy-of select="ancestor-or-self::*[@xml:lang][1]/@xml:lang"/>
  76. <xsl:sequence select="$links"/>
  77. </linklist>
  78. </xsl:if>
  79. </xsl:template>
  80. <!-- Ungrouped links have the default-mode template applied to them. (Can be overridden.) -->
  81. <xsl:template match="*[contains(@class, ' topic/link ')]" mode="related-links:link" name="related-links:link"
  82. as="element()*">
  83. <xsl:sequence select="."/>
  84. </xsl:template>
  85. <!-- Main entry point. -->
  86. <xsl:template match="*[contains(@class, ' topic/related-links ')]" mode="related-links:group-unordered-links"
  87. as="element()*">
  88. <!-- Node set. The set of nodes to group. -->
  89. <xsl:param name="nodes" as="element()*"/>
  90. <!-- Sent back to all callback templates as a parameter.-->
  91. <!-- XXX: Seems obsolete as the value is never used anywhere -->
  92. <xsl:param name="tunnel"/>
  93. <!-- Query all links for their group and priority. -->
  94. <xsl:variable name="group-priorities" as="xs:string">
  95. <xsl:call-template name="related-links:get-priorities">
  96. <xsl:with-param name="nodes" select="$nodes"/>
  97. <xsl:with-param name="tunnel" select="$tunnel"/>
  98. </xsl:call-template>
  99. </xsl:variable>
  100. <!-- Get order of groups based on priorities. -->
  101. <xsl:variable name="group-sequence" as="xs:string">
  102. <xsl:call-template name="related-links:get-sequence-from-priorities">
  103. <xsl:with-param name="priorities" select="$group-priorities"/>
  104. <xsl:with-param name="tunnel" select="$tunnel"/>
  105. </xsl:call-template>
  106. </xsl:variable>
  107. <!-- Process the links in each group in order. -->
  108. <xsl:call-template name="related-links:walk-groups">
  109. <xsl:with-param name="nodes" select="$nodes"/>
  110. <xsl:with-param name="group-sequence" select="$group-sequence"/>
  111. <xsl:with-param name="tunnel" select="$tunnel"/>
  112. </xsl:call-template>
  113. </xsl:template>
  114. <!-- Get the priorities and groups of every link. -->
  115. <!-- Produces a string like "2 task task ;3 concept concept ;1 reference reference ;0 topic ;",
  116. where the numbers are priorities of each group, and the space-delimited words
  117. are the groups and link types (link/@type) which belong to that group. -->
  118. <xsl:template name="related-links:get-priorities" as="xs:string">
  119. <xsl:param name="nodes" as="element()*"/>
  120. <xsl:param name="tunnel"/>
  121. <xsl:param name="partial-result" select="''" as="xs:string"/>
  122. <xsl:choose>
  123. <xsl:when test="exists($nodes)">
  124. <!-- Process each node one at a time. -->
  125. <xsl:variable name="node" select="$nodes[1]" as="element()"/>
  126. <xsl:variable name="node-group" as="xs:string">
  127. <xsl:apply-templates select="$node" mode="related-links:get-group">
  128. <xsl:with-param name="tunnel" select="$tunnel"/>
  129. </xsl:apply-templates>
  130. </xsl:variable>
  131. <xsl:variable name="node-priorty" as="xs:integer">
  132. <xsl:apply-templates select="$node" mode="related-links:get-group-priority">
  133. <xsl:with-param name="tunnel" select="$tunnel"/>
  134. </xsl:apply-templates>
  135. </xsl:variable>
  136. <xsl:call-template name="related-links:get-priorities">
  137. <xsl:with-param name="nodes" select="$nodes[position() != 1]"/>
  138. <xsl:with-param name="tunnel" select="$tunnel"/>
  139. <xsl:with-param name="partial-result">
  140. <xsl:value-of>
  141. <xsl:choose>
  142. <!-- This type has already been seen. -->
  143. <xsl:when
  144. test="contains($partial-result, concat(' ', $node-group, ' '))
  145. and contains($partial-result, concat(' ', $node/@type, ' '))">
  146. <xsl:value-of select="$partial-result"/>
  147. </xsl:when>
  148. <!-- This type has not been seen, but the base group has. -->
  149. <xsl:when test="contains($partial-result, concat(' ', $node-group, ' '))">
  150. <xsl:value-of select="substring-before($partial-result, concat(' ', $node-group, ' '))"/>
  151. <xsl:text> </xsl:text>
  152. <xsl:value-of select="$node-group"/>
  153. <xsl:text> </xsl:text>
  154. <xsl:value-of select="$node/@type"/>
  155. <xsl:text> </xsl:text>
  156. <xsl:value-of select="substring-after($partial-result, concat(' ', $node-group, ' '))"/>
  157. </xsl:when>
  158. <!-- Never seen this base group before (nor the type). -->
  159. <xsl:otherwise>
  160. <xsl:value-of select="$partial-result"/>
  161. <xsl:value-of select="$node-priorty"/>
  162. <xsl:text> </xsl:text>
  163. <xsl:value-of select="$node-group"/>
  164. <xsl:if test="$node-group != $node/@type">
  165. <xsl:text> </xsl:text>
  166. <xsl:value-of select="$node/@type"/>
  167. </xsl:if>
  168. <xsl:text> </xsl:text>
  169. <xsl:text>;</xsl:text>
  170. </xsl:otherwise>
  171. </xsl:choose>
  172. </xsl:value-of>
  173. </xsl:with-param>
  174. </xsl:call-template>
  175. </xsl:when>
  176. <xsl:otherwise>
  177. <xsl:value-of select="$partial-result"/>
  178. </xsl:otherwise>
  179. </xsl:choose>
  180. </xsl:template>
  181. <!-- Sort groups according to their priorities, removing duplicates. -->
  182. <!-- Takes a string returned by related-links:get-priorities and returns
  183. the groups and link types in decreasing order of priority
  184. (e.g., "concept concept;task task;reference reference; topic;"). -->
  185. <xsl:template name="related-links:get-sequence-from-priorities" as="xs:string">
  186. <xsl:param name="priorities" as="xs:string"/>
  187. <xsl:param name="tunnel"/>
  188. <xsl:param name="partial-result" select="''" as="xs:string"/>
  189. <xsl:choose>
  190. <xsl:when test="contains($priorities, ';')">
  191. <xsl:call-template name="related-links:get-best-priority-in-sequence">
  192. <xsl:with-param name="priorities" select="$priorities"/>
  193. <xsl:with-param name="tunnel" select="$tunnel"/>
  194. <xsl:with-param name="partial-result" select="$partial-result"/>
  195. </xsl:call-template>
  196. </xsl:when>
  197. <xsl:otherwise>
  198. <xsl:value-of select="$partial-result"/>
  199. </xsl:otherwise>
  200. </xsl:choose>
  201. </xsl:template>
  202. <!-- Find the highest-priority group remaining in the list of priorities. -->
  203. <xsl:template name="related-links:get-best-priority-in-sequence" as="xs:string">
  204. <!-- semicolon separated list of space separated tuple of priority integer and group name -->
  205. <xsl:param name="priorities" as="xs:string"/>
  206. <xsl:param name="tunnel"/>
  207. <xsl:param name="partial-result" as="xs:string"/>
  208. <xsl:param name="best-group" select="'#none#'" as="xs:string"/>
  209. <xsl:param name="best-priority" select="-1" as="xs:integer"/>
  210. <!-- semicolon separated list of space separated tuple of priority integer and group name -->
  211. <xsl:param name="lesser-priorities" select="''" as="xs:string"/>
  212. <xsl:choose>
  213. <xsl:when test="contains($priorities, ';')">
  214. <xsl:choose>
  215. <!-- First group always wins. -->
  216. <xsl:when test="$best-group = '#none#'">
  217. <xsl:call-template name="related-links:get-best-priority-in-sequence">
  218. <xsl:with-param name="priorities" select="substring-after($priorities, ';')"/>
  219. <xsl:with-param name="tunnel" select="$tunnel"/>
  220. <xsl:with-param name="partial-result" select="$partial-result"/>
  221. <xsl:with-param name="best-priority" select="xs:integer(substring-before(substring-before($priorities, ';'), ' '))"/>
  222. <xsl:with-param name="best-group" select="substring-after(substring-before($priorities, ';'), ' ')"/>
  223. <xsl:with-param name="lesser-priorities" select="$lesser-priorities"/>
  224. </xsl:call-template>
  225. </xsl:when>
  226. <!-- Higher-priority group found; shunt best-so-far to lesser priorities and continue. -->
  227. <xsl:when test="xs:integer(substring-before(substring-before($priorities, ';'), ' ')) > $best-priority">
  228. <xsl:call-template name="related-links:get-best-priority-in-sequence">
  229. <xsl:with-param name="priorities" select="substring-after($priorities, ';')"/>
  230. <xsl:with-param name="tunnel" select="$tunnel"/>
  231. <xsl:with-param name="partial-result" select="$partial-result"/>
  232. <xsl:with-param name="best-priority" select="xs:integer(substring-before(substring-before($priorities, ';'), ' '))"/>
  233. <xsl:with-param name="best-group" select="substring-after(substring-before($priorities, ';'), ' ')"/>
  234. <xsl:with-param name="lesser-priorities" select="concat($lesser-priorities, $best-priority, ' ', $best-group, ';')"/>
  235. </xsl:call-template>
  236. </xsl:when>
  237. <!-- Best-so-far priority is still supreme. -->
  238. <xsl:otherwise>
  239. <xsl:call-template name="related-links:get-best-priority-in-sequence">
  240. <xsl:with-param name="priorities" select="substring-after($priorities, ';')"/>
  241. <xsl:with-param name="tunnel" select="$tunnel"/>
  242. <xsl:with-param name="partial-result" select="$partial-result"/>
  243. <xsl:with-param name="best-priority" select="$best-priority"/>
  244. <xsl:with-param name="best-group" select="$best-group"/>
  245. <xsl:with-param name="lesser-priorities" select="concat($lesser-priorities, substring-before($priorities, ';'), ';')"/>
  246. </xsl:call-template>
  247. </xsl:otherwise>
  248. </xsl:choose>
  249. </xsl:when>
  250. <!-- Best priority found. -->
  251. <xsl:otherwise>
  252. <xsl:call-template name="related-links:get-sequence-from-priorities">
  253. <xsl:with-param name="priorities" select="$lesser-priorities"/>
  254. <xsl:with-param name="tunnel" select="$tunnel"/>
  255. <xsl:with-param name="partial-result">
  256. <xsl:choose>
  257. <!-- Duplicate; just move on. (Should not happen.) -->
  258. <xsl:when test="contains(concat(';', $partial-result), concat(';', $best-group, ';'))">
  259. <xsl:value-of select="$partial-result"/>
  260. </xsl:when>
  261. <!-- Add group to list and move on. -->
  262. <xsl:otherwise>
  263. <xsl:value-of select="concat($partial-result, $best-group, ';')"/>
  264. </xsl:otherwise>
  265. </xsl:choose>
  266. </xsl:with-param>
  267. </xsl:call-template>
  268. </xsl:otherwise>
  269. </xsl:choose>
  270. </xsl:template>
  271. <!-- Process each group in turn. -->
  272. <xsl:template name="related-links:walk-groups" as="element()*">
  273. <xsl:param name="nodes" as="element()*"/>
  274. <xsl:param name="tunnel"/>
  275. <!-- semicolon separate list -->
  276. <xsl:param name="group-sequence" select="''" as="xs:string"/>
  277. <xsl:choose>
  278. <xsl:when test="contains($group-sequence, ';')">
  279. <xsl:call-template name="related-links:do-group">
  280. <xsl:with-param name="nodes" select="$nodes"/>
  281. <xsl:with-param name="tunnel" select="$tunnel"/>
  282. <xsl:with-param name="group" select="substring-before($group-sequence, ';')"/>
  283. </xsl:call-template>
  284. <xsl:call-template name="related-links:walk-groups">
  285. <xsl:with-param name="nodes" select="$nodes"/>
  286. <xsl:with-param name="tunnel" select="$tunnel"/>
  287. <xsl:with-param name="group-sequence" select="substring-after($group-sequence, ';')"/>
  288. </xsl:call-template>
  289. </xsl:when>
  290. </xsl:choose>
  291. </xsl:template>
  292. <!-- Process each group. -->
  293. <xsl:template name="related-links:do-group" as="element()?">
  294. <xsl:param name="nodes" as="element()*"/>
  295. <xsl:param name="tunnel"/>
  296. <!-- space separated list -->
  297. <xsl:param name="group" as="xs:string"/>
  298. <!-- Process the links belonging to that group. -->
  299. <xsl:variable name="group-nodes" select="$nodes[contains(concat(' ', $group), concat(' ', @type, ' '))]" as="element()*"/>
  300. <!-- Let the group wrap all its links in additional elements. -->
  301. <xsl:apply-templates select="$group-nodes[1]" mode="related-links:result-group">
  302. <xsl:with-param name="links" as="node()*">
  303. <xsl:apply-templates select="$group-nodes" mode="related-links:link">
  304. <xsl:sort
  305. select="
  306. 10 * number(@role = 'parent') +
  307. 9 * number(@role = 'ancestor') +
  308. 8 * number(@role = 'child') +
  309. 7 * number(@role = 'descendant') +
  310. 6 * number(@role = 'next') +
  311. 5 * number(@role = 'previous') +
  312. 4 * number(@role = 'sibling') +
  313. 3 * number(@role = 'cousin') +
  314. 2 * number(@role = 'friend') +
  315. 1 * number(@role = 'other')"
  316. data-type="number" order="descending"/>
  317. <!-- All @role='other' have to go together, darn. -->
  318. <xsl:sort select="@otherrole" data-type="text"/>
  319. <xsl:with-param name="tunnel" select="$tunnel"/>
  320. </xsl:apply-templates>
  321. </xsl:with-param>
  322. <xsl:with-param name="tunnel" select="$tunnel"/>
  323. </xsl:apply-templates>
  324. </xsl:template>
  325. </xsl:stylesheet>