related-links.xsl 19 KB

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