olink.xsl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. version="1.0">
  4. <xsl:param name="website.database.document"
  5. select="'website.database.xml'"/>
  6. <xsl:template match="olink">
  7. <xsl:choose>
  8. <xsl:when test="@targetdoc != '' or @targetptr != ''">
  9. <xsl:apply-imports/>
  10. </xsl:when>
  11. <xsl:otherwise>
  12. <xsl:call-template name="olink-entity"/>
  13. </xsl:otherwise>
  14. </xsl:choose>
  15. </xsl:template>
  16. <xsl:template name="olink-entity">
  17. <xsl:variable name="xmlfile"
  18. select="document(unparsed-entity-uri(@targetdocent),$autolayout)"/>
  19. <xsl:variable name="webpage"
  20. select="$xmlfile/webpage"/>
  21. <xsl:variable name="tocentry"
  22. select="$autolayout//*[$webpage/@id=@id]"/>
  23. <xsl:variable name="dir">
  24. <xsl:choose>
  25. <xsl:when test="starts-with($tocentry/@dir, '/')">
  26. <xsl:value-of select="substring($tocentry/@dir, 2)"/>
  27. </xsl:when>
  28. <xsl:otherwise>
  29. <xsl:value-of select="$tocentry/@dir"/>
  30. </xsl:otherwise>
  31. </xsl:choose>
  32. </xsl:variable>
  33. <!-- debug
  34. <xsl:message>Olink for <xsl:value-of select="unparsed-entity-uri(@targetdocent)"/></xsl:message>
  35. <xsl:message>Page id <xsl:value-of select="$webpage/@id"/></xsl:message>
  36. -->
  37. <xsl:choose>
  38. <xsl:when test="@type = 'embed'">
  39. <xsl:apply-templates select="$xmlfile"/>
  40. </xsl:when>
  41. <xsl:otherwise>
  42. <!-- @type = 'replace' or @type = 'new' -->
  43. <a>
  44. <xsl:if test="@id">
  45. <xsl:attribute name="name">
  46. <xsl:value-of select="@id"/>
  47. </xsl:attribute>
  48. </xsl:if>
  49. <!-- debug
  50. <xsl:message>
  51. <xsl:text>href: </xsl:text>
  52. <xsl:call-template name="root-rel-path"/>
  53. <xsl:text>::</xsl:text>
  54. <xsl:value-of select="$dir"/>
  55. <xsl:text>::</xsl:text>
  56. <xsl:value-of select="$filename-prefix"/>
  57. <xsl:text>::</xsl:text>
  58. <xsl:value-of select="$tocentry/@filename"/>
  59. <xsl:text>::</xsl:text>
  60. <xsl:if test="@localinfo">
  61. <xsl:text>#</xsl:text>
  62. <xsl:value-of select="@localinfo"/>
  63. </xsl:if>
  64. </xsl:message>
  65. -->
  66. <xsl:attribute name="href">
  67. <xsl:call-template name="root-rel-path"/>
  68. <xsl:value-of select="$dir"/>
  69. <xsl:value-of select="$filename-prefix"/>
  70. <xsl:value-of select="$tocentry/@filename"/>
  71. <xsl:if test="@localinfo">
  72. <xsl:text>#</xsl:text>
  73. <xsl:value-of select="@localinfo"/>
  74. </xsl:if>
  75. </xsl:attribute>
  76. <xsl:if test="@type = 'new'">
  77. <xsl:attribute name="target">_blank</xsl:attribute>
  78. </xsl:if>
  79. <xsl:choose>
  80. <xsl:when test="count(node()) = 0">
  81. <xsl:apply-templates select="$webpage/head/title"/>
  82. </xsl:when>
  83. <xsl:otherwise>
  84. <xsl:apply-templates/>
  85. </xsl:otherwise>
  86. </xsl:choose>
  87. </a>
  88. </xsl:otherwise>
  89. </xsl:choose>
  90. </xsl:template>
  91. <!-- Customize the selection to use both website and offsite databases -->
  92. <xsl:template name="select.target.database">
  93. <xsl:param name="targetdoc.att" select="''"/>
  94. <xsl:param name="targetptr.att" select="''"/>
  95. <xsl:param name="olink.lang" select="''"/>
  96. <!-- Is the target in the website database? -->
  97. <xsl:variable name="website.olink.key">
  98. <xsl:if test="$website.database.document != ''">
  99. <xsl:call-template name="select.olink.key">
  100. <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
  101. <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
  102. <xsl:with-param name="olink.lang" select="$olink.lang"/>
  103. <xsl:with-param name="target.database"
  104. select="document($website.database.document,/)"/>
  105. </xsl:call-template>
  106. </xsl:if>
  107. </xsl:variable>
  108. <!-- Is the target in the offsite database? -->
  109. <xsl:variable name="offsite.olink.key">
  110. <xsl:if test="$target.database.document != ''">
  111. <xsl:call-template name="select.olink.key">
  112. <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
  113. <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
  114. <xsl:with-param name="olink.lang" select="$olink.lang"/>
  115. <xsl:with-param name="target.database"
  116. select="document($target.database.document,/)"/>
  117. </xsl:call-template>
  118. </xsl:if>
  119. </xsl:variable>
  120. <xsl:choose>
  121. <xsl:when test="$website.olink.key != ''">
  122. <xsl:value-of select="$website.database.document"/>
  123. </xsl:when>
  124. <xsl:when test="$offsite.olink.key != ''">
  125. <xsl:value-of select="$target.database.document"/>
  126. </xsl:when>
  127. <xsl:otherwise>
  128. <!-- Return a bogus string and let the olink template deal with it
  129. <xsl:text>NOMATCHINANYDATABASEDOCUMENT</xsl:text>
  130. -->
  131. </xsl:otherwise>
  132. </xsl:choose>
  133. </xsl:template>
  134. <!-- Customize this template to use Website root-relative -->
  135. <xsl:template name="make.olink.href">
  136. <xsl:param name="olink.key" select="''"/>
  137. <xsl:param name="target.database"/>
  138. <xsl:if test="$olink.key != ''">
  139. <xsl:variable name="targetdoc">
  140. <xsl:value-of select="substring-before($olink.key, '/')"/>
  141. </xsl:variable>
  142. <xsl:variable name="targetptr">
  143. <xsl:value-of select="substring-before(substring-after($olink.key, '/'),'/')"/>
  144. </xsl:variable>
  145. <xsl:variable name="target.href" >
  146. <xsl:for-each select="$target.database" >
  147. <xsl:value-of select="key('targetptr-key', $olink.key)/@href" />
  148. </xsl:for-each>
  149. </xsl:variable>
  150. <xsl:variable name="target.dir" >
  151. <xsl:for-each select="$target.database" >
  152. <xsl:value-of select="key('targetdoc-key', $targetdoc)/@dir" />
  153. </xsl:for-each>
  154. </xsl:variable>
  155. <xsl:variable name="target.element" >
  156. <xsl:for-each select="$target.database" >
  157. <xsl:value-of select="key('targetptr-key', $olink.key)/@element" />
  158. </xsl:for-each>
  159. </xsl:variable>
  160. <!-- Does the target database use a sitemap? -->
  161. <xsl:variable name="use.sitemap">
  162. <xsl:choose>
  163. <xsl:when test="$target.database//sitemap">1</xsl:when>
  164. <xsl:otherwise>0</xsl:otherwise>
  165. </xsl:choose>
  166. </xsl:variable>
  167. <!-- Get the baseuri for this targetptr -->
  168. <xsl:variable name="baseuri" >
  169. <xsl:choose>
  170. <!-- Does the database use a sitemap? -->
  171. <xsl:when test="$use.sitemap != 0" >
  172. <xsl:choose>
  173. <!-- Was current.docid parameter set? -->
  174. <xsl:when test="$current.docid != ''">
  175. <!-- Was it found in the database? -->
  176. <xsl:variable name="currentdoc.key" >
  177. <xsl:for-each select="$target.database" >
  178. <xsl:value-of select="key('targetdoc-key',
  179. $current.docid)/@targetdoc" />
  180. </xsl:for-each>
  181. </xsl:variable>
  182. <xsl:choose>
  183. <xsl:when test="$currentdoc.key != ''">
  184. <xsl:for-each select="$target.database" >
  185. <xsl:call-template name="targetpath" >
  186. <xsl:with-param name="dirnode"
  187. select="key('targetdoc-key', $current.docid)/parent::dir"/>
  188. <xsl:with-param name="targetdoc" select="$targetdoc"/>
  189. </xsl:call-template>
  190. </xsl:for-each >
  191. </xsl:when>
  192. <xsl:otherwise>
  193. <xsl:message>
  194. <xsl:text>Olink error: cannot compute relative </xsl:text>
  195. <xsl:text>sitemap path because $current.docid '</xsl:text>
  196. <xsl:value-of select="$current.docid"/>
  197. <xsl:text>' not found in target database.</xsl:text>
  198. </xsl:message>
  199. </xsl:otherwise>
  200. </xsl:choose>
  201. </xsl:when>
  202. <xsl:otherwise>
  203. <xsl:message>
  204. <xsl:text>Olink warning: cannot compute relative </xsl:text>
  205. <xsl:text>sitemap path without $current.docid parameter</xsl:text>
  206. </xsl:message>
  207. </xsl:otherwise>
  208. </xsl:choose>
  209. <!-- In either case, add baseuri from its document entry-->
  210. <xsl:variable name="docbaseuri">
  211. <xsl:for-each select="$target.database" >
  212. <xsl:value-of select="key('targetdoc-key', $targetdoc)/@baseuri" />
  213. </xsl:for-each>
  214. </xsl:variable>
  215. <xsl:if test="$docbaseuri != ''" >
  216. <xsl:value-of select="$docbaseuri"/>
  217. </xsl:if>
  218. </xsl:when>
  219. <!-- No database sitemap in use -->
  220. <xsl:otherwise>
  221. <!-- compute a root-relative path if current page has a @dir -->
  222. <xsl:variable name="root-rel">
  223. <xsl:call-template name="root-rel-path"/>
  224. </xsl:variable>
  225. <xsl:if test="$root-rel != ''">
  226. <xsl:value-of select="$root-rel"/>
  227. </xsl:if>
  228. <!-- Add the target's @dir to the path -->
  229. <xsl:if test="$target.dir != ''">
  230. <xsl:value-of select="$target.dir"/>
  231. </xsl:if>
  232. <!-- Just use any baseuri from its document entry -->
  233. <xsl:variable name="docbaseuri">
  234. <xsl:for-each select="$target.database" >
  235. <xsl:value-of select="key('targetdoc-key',
  236. $targetdoc)/@baseuri" />
  237. </xsl:for-each>
  238. </xsl:variable>
  239. <xsl:if test="$docbaseuri != ''" >
  240. <xsl:value-of select="$docbaseuri"/>
  241. </xsl:if>
  242. </xsl:otherwise>
  243. </xsl:choose>
  244. </xsl:variable>
  245. <!-- Form the href information -->
  246. <xsl:if test="$baseuri != ''">
  247. <xsl:value-of select="$baseuri"/>
  248. <xsl:if test="substring($target.href,1,1) != '#'">
  249. <!--xsl:text>/</xsl:text-->
  250. </xsl:if>
  251. </xsl:if>
  252. <xsl:choose>
  253. <xsl:when test="$target.element = 'webpage' and
  254. $targetdoc = $targetptr">
  255. <!-- Don't output #id because not needed -->
  256. </xsl:when>
  257. <!-- optionally turn off frag for PDF references -->
  258. <xsl:when test="not($insert.olink.pdf.frag = 0 and
  259. translate(substring($baseuri, string-length($baseuri) - 3),
  260. 'PDF', 'pdf') = '.pdf'
  261. and starts-with($target.href, '#') )">
  262. <xsl:value-of select="$target.href"/>
  263. </xsl:when>
  264. <xsl:otherwise>
  265. <xsl:value-of select="$target.href"/>
  266. </xsl:otherwise>
  267. </xsl:choose>
  268. </xsl:if>
  269. </xsl:template>
  270. </xsl:stylesheet>