stripns.xsl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:ng="http://docbook.org/docbook-ng"
  4. xmlns:db="http://docbook.org/ns/docbook"
  5. xmlns:saxon="http://icl.com/saxon"
  6. xmlns:NodeInfo="http://org.apache.xalan.lib.NodeInfo"
  7. xmlns:exsl="http://exslt.org/common"
  8. exclude-result-prefixes="db ng exsl saxon NodeInfo"
  9. version='1.0'>
  10. <!-- ********************************************************************
  11. $Id$
  12. ********************************************************************
  13. This file is part of the XSL DocBook Stylesheet distribution.
  14. See ../README or http://docbook.sf.net/release/xsl/current/ for
  15. copyright and other information.
  16. This file contains general templates common to both the HTML and FO
  17. versions of the DocBook stylesheets.
  18. ******************************************************************** -->
  19. <xsl:import href="utility.xsl"/>
  20. <!-- * Get a title for current doc so that we let the user -->
  21. <!-- * know what document we are processing at this point. -->
  22. <xsl:variable name="doc.title">
  23. <xsl:call-template name="get.doc.title"/>
  24. </xsl:variable>
  25. <!-- put an xml:base attribute on the root element -->
  26. <xsl:template match="/*" mode="stripNS">
  27. <xsl:choose>
  28. <xsl:when test="self::ng:* or self::db:*">
  29. <xsl:element name="{local-name(.)}">
  30. <xsl:copy-of select="@*[not(name(.) = 'xml:id')
  31. and not(name(.) = 'version')]"/>
  32. <xsl:if test="@xml:id">
  33. <xsl:attribute name="id">
  34. <xsl:value-of select="@xml:id"/>
  35. </xsl:attribute>
  36. </xsl:if>
  37. <xsl:call-template name="add-xml-base"/>
  38. <xsl:apply-templates mode="stripNS"/>
  39. </xsl:element>
  40. </xsl:when>
  41. <xsl:otherwise>
  42. <xsl:copy>
  43. <xsl:copy-of select="@*[not(name(.) = 'xml:id')
  44. and not(name(.) = 'version')]"/>
  45. <xsl:if test="@xml:id">
  46. <xsl:attribute name="id">
  47. <xsl:value-of select="@xml:id"/>
  48. </xsl:attribute>
  49. </xsl:if>
  50. <xsl:call-template name="add-xml-base"/>
  51. <xsl:apply-templates mode="stripNS"/>
  52. </xsl:copy>
  53. </xsl:otherwise>
  54. </xsl:choose>
  55. </xsl:template>
  56. <xsl:template match="*" mode="stripNS">
  57. <xsl:choose>
  58. <xsl:when test="self::ng:* or self::db:*">
  59. <xsl:element name="{local-name(.)}">
  60. <xsl:copy-of select="@*[not(name(.) = 'xml:id')
  61. and not(name(.) = 'version')]"/>
  62. <xsl:if test="@xml:id">
  63. <xsl:attribute name="id">
  64. <xsl:value-of select="@xml:id"/>
  65. </xsl:attribute>
  66. </xsl:if>
  67. <xsl:apply-templates mode="stripNS"/>
  68. </xsl:element>
  69. </xsl:when>
  70. <xsl:otherwise>
  71. <xsl:copy>
  72. <xsl:copy-of select="@*[not(name(.) = 'xml:id')
  73. and not(name(.) = 'version')]"/>
  74. <xsl:if test="@xml:id">
  75. <xsl:attribute name="id">
  76. <xsl:value-of select="@xml:id"/>
  77. </xsl:attribute>
  78. </xsl:if>
  79. <xsl:apply-templates mode="stripNS"/>
  80. </xsl:copy>
  81. </xsl:otherwise>
  82. </xsl:choose>
  83. </xsl:template>
  84. <xsl:template match="db:info" mode="stripNS">
  85. <xsl:variable name="info">
  86. <xsl:choose>
  87. <xsl:when test="parent::db:article
  88. |parent::db:appendix
  89. |parent::db:bibliography
  90. |parent::db:book
  91. |parent::db:chapter
  92. |parent::db:glossary
  93. |parent::db:index
  94. |parent::db:part
  95. |parent::db:preface
  96. |parent::db:refentry
  97. |parent::db:reference
  98. |parent::db:refsect1
  99. |parent::db:refsect2
  100. |parent::db:refsect3
  101. |parent::db:refsection
  102. |parent::db:refsynopsisdiv
  103. |parent::db:sect1
  104. |parent::db:sect2
  105. |parent::db:sect3
  106. |parent::db:sect4
  107. |parent::db:sect5
  108. |parent::db:section
  109. |parent::db:setindex
  110. |parent::db:set
  111. |parent::db:slides
  112. |parent::db:sidebar">
  113. <xsl:value-of select="local-name(parent::*)"/>
  114. <xsl:text>info</xsl:text>
  115. </xsl:when>
  116. <xsl:when test="parent::db:audioobject
  117. |parent::db:imageobject
  118. |parent::db:inlinemediaobject
  119. |parent::db:mediaobject
  120. |parent::db:mediaobjectco
  121. |parent::db:textobject
  122. |parent::db:videoobject">
  123. <xsl:text>objectinfo</xsl:text>
  124. </xsl:when>
  125. <xsl:otherwise>blockinfo</xsl:otherwise>
  126. </xsl:choose>
  127. </xsl:variable>
  128. <xsl:element name="{$info}">
  129. <xsl:copy-of select="@*[not(name(.) = 'xml:id')
  130. and not(name(.) = 'version')]"/>
  131. <xsl:if test="@xml:id">
  132. <xsl:attribute name="id">
  133. <xsl:value-of select="@xml:id"/>
  134. </xsl:attribute>
  135. </xsl:if>
  136. <xsl:apply-templates mode="stripNS"/>
  137. </xsl:element>
  138. <xsl:if test="(not(../db:title) and not(../ng:title))
  139. and ($info = 'prefaceinfo'
  140. or $info = 'chapterinfo'
  141. or $info = 'sectioninfo'
  142. or $info = 'sect1info'
  143. or $info = 'sect2info'
  144. or $info = 'sect3info'
  145. or $info = 'sect4info'
  146. or $info = 'sect5info'
  147. or $info = 'refsectioninfo'
  148. or $info = 'refsect1info'
  149. or $info = 'refsect2info'
  150. or $info = 'refsect3info'
  151. or $info = 'blockinfo'
  152. or $info = 'appendixinfo')">
  153. <xsl:apply-templates select="db:title|ng:title" mode="stripNS"/>
  154. </xsl:if>
  155. </xsl:template>
  156. <xsl:template match="ng:link|db:link" mode="stripNS">
  157. <xsl:variable xmlns:xlink="http://www.w3.org/1999/xlink"
  158. name="href" select="@xlink:href|@href"/>
  159. <xsl:choose>
  160. <xsl:when test="$href != '' and not(starts-with($href,'#'))">
  161. <ulink url="{$href}">
  162. <xsl:for-each select="@*">
  163. <xsl:if test="local-name(.) != 'href'
  164. and name(.) != 'version'
  165. and name(.) != 'xml:id'">
  166. <xsl:copy/>
  167. </xsl:if>
  168. </xsl:for-each>
  169. <xsl:if test="@xml:id">
  170. <xsl:attribute name="id">
  171. <xsl:value-of select="@xml:id"/>
  172. </xsl:attribute>
  173. </xsl:if>
  174. <xsl:apply-templates mode="stripNS"/>
  175. </ulink>
  176. </xsl:when>
  177. <xsl:when test="$href != '' and starts-with($href,'#')">
  178. <link linkend="{substring-after($href,'#')}">
  179. <xsl:for-each select="@*">
  180. <xsl:if test="local-name(.) != 'href'
  181. and name(.) != 'version'
  182. and name(.) != 'xml:id'">
  183. <xsl:copy/>
  184. </xsl:if>
  185. </xsl:for-each>
  186. <xsl:if test="@xml:id">
  187. <xsl:attribute name="id">
  188. <xsl:value-of select="@xml:id"/>
  189. </xsl:attribute>
  190. </xsl:if>
  191. <xsl:apply-templates mode="stripNS"/>
  192. </link>
  193. </xsl:when>
  194. <xsl:otherwise>
  195. <link>
  196. <xsl:copy-of select="@*[not(name(.) = 'xml:id')
  197. and not(name(.) = 'version')]"/>
  198. <xsl:if test="@xml:id">
  199. <xsl:attribute name="id">
  200. <xsl:value-of select="@xml:id"/>
  201. </xsl:attribute>
  202. </xsl:if>
  203. <xsl:apply-templates mode="stripNS"/>
  204. </link>
  205. </xsl:otherwise>
  206. </xsl:choose>
  207. </xsl:template>
  208. <xsl:template match="ng:tag|db:tag" mode="stripNS">
  209. <sgmltag>
  210. <xsl:copy-of select="@*[not(name(.) = 'xml:id')
  211. and not(name(.) = 'version')]"/>
  212. <xsl:apply-templates mode="stripNS"/>
  213. </sgmltag>
  214. </xsl:template>
  215. <xsl:template match="ng:textdata|db:textdata
  216. |ng:imagedata|db:imagedata
  217. |ng:videodata|db:videodata
  218. |ng:audiodata|db:audiodata" mode="stripNS">
  219. <xsl:element name="{local-name(.)}">
  220. <xsl:copy-of select="@*[not(name(.) = 'xml:id')
  221. and not(name(.) = 'version')
  222. and not(name(.) = 'entityref')]"/>
  223. <xsl:if test="@xml:id">
  224. <xsl:attribute name="id">
  225. <xsl:value-of select="@xml:id"/>
  226. </xsl:attribute>
  227. </xsl:if>
  228. <xsl:choose>
  229. <xsl:when test="@entityref">
  230. <xsl:attribute name="fileref">
  231. <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
  232. </xsl:attribute>
  233. </xsl:when>
  234. </xsl:choose>
  235. <xsl:apply-templates mode="stripNS"/>
  236. </xsl:element>
  237. </xsl:template>
  238. <xsl:template name="add-xml-base">
  239. <xsl:if test="not(@xml:base)">
  240. <xsl:variable name="base">
  241. <xsl:choose>
  242. <xsl:when test="function-available('saxon:systemId')">
  243. <xsl:value-of select="saxon:systemId()"/>
  244. </xsl:when>
  245. <xsl:when test="function-available('NodeInfo:systemId')">
  246. <xsl:value-of select="NodeInfo:systemId()"/>
  247. </xsl:when>
  248. <xsl:otherwise>
  249. <xsl:call-template name="log.message">
  250. <xsl:with-param name="level">Warn</xsl:with-param>
  251. <xsl:with-param name="source" select="$doc.title"/>
  252. <xsl:with-param name="context-desc">
  253. <xsl:text>no @xml:base</xsl:text>
  254. </xsl:with-param>
  255. <xsl:with-param name="message">
  256. <xsl:text>cannot add @xml:base to node-set root element</xsl:text>
  257. </xsl:with-param>
  258. </xsl:call-template>
  259. <xsl:call-template name="log.message">
  260. <xsl:with-param name="level">Warn</xsl:with-param>
  261. <xsl:with-param name="source" select="$doc.title"/>
  262. <xsl:with-param name="context-desc">
  263. <xsl:text>no @xml:base</xsl:text>
  264. </xsl:with-param>
  265. <xsl:with-param name="message">
  266. <xsl:text>relative paths may not work</xsl:text>
  267. </xsl:with-param>
  268. </xsl:call-template>
  269. </xsl:otherwise>
  270. </xsl:choose>
  271. </xsl:variable>
  272. <!-- debug
  273. <xsl:message>base is <xsl:value-of select="$base"/></xsl:message>
  274. -->
  275. <xsl:if test="$base != ''">
  276. <xsl:attribute name="xml:base">
  277. <xsl:call-template name="systemIdToBaseURI">
  278. <xsl:with-param name="systemId">
  279. <!-- file: seems to confuse some processors. -->
  280. <xsl:choose>
  281. <!-- however, windows paths must use file:///c:/path -->
  282. <xsl:when test="starts-with($base, 'file:///') and
  283. substring($base, 10, 1) = ':'">
  284. <xsl:value-of select="$base"/>
  285. </xsl:when>
  286. <xsl:when test="starts-with($base, 'file:/')
  287. and substring($base, 8, 1) = ':'">
  288. <xsl:value-of select="concat('file:///',
  289. substring-after($base,'file:/'))"/>
  290. </xsl:when>
  291. <xsl:when test="starts-with($base, 'file:///')">
  292. <xsl:value-of select="substring-after($base,'file://')"/>
  293. </xsl:when>
  294. <xsl:when test="starts-with($base, 'file://')">
  295. <xsl:value-of select="substring-after($base,'file:/')"/>
  296. </xsl:when>
  297. <xsl:when test="starts-with($base, 'file:/')">
  298. <xsl:value-of select="substring-after($base,'file:')"/>
  299. </xsl:when>
  300. <xsl:otherwise>
  301. <xsl:value-of select="$base"/>
  302. </xsl:otherwise>
  303. </xsl:choose>
  304. </xsl:with-param>
  305. </xsl:call-template>
  306. </xsl:attribute>
  307. </xsl:if>
  308. </xsl:if>
  309. </xsl:template>
  310. <xsl:template name="systemIdToBaseURI">
  311. <xsl:param name="systemId" select="''"/>
  312. <xsl:if test="contains($systemId,'/')">
  313. <xsl:value-of select="substring-before($systemId,'/')"/>
  314. <xsl:text>/</xsl:text>
  315. <xsl:call-template name="systemIdToBaseURI">
  316. <xsl:with-param name="systemId"
  317. select="substring-after($systemId,'/')"/>
  318. </xsl:call-template>
  319. </xsl:if>
  320. </xsl:template>
  321. <xsl:template match="comment()|processing-instruction()|text()" mode="stripNS">
  322. <xsl:copy/>
  323. </xsl:template>
  324. <xsl:template match="/" priority="-1">
  325. <xsl:choose>
  326. <xsl:when test="(function-available('exsl:node-set') or
  327. contains(system-property('xsl:vendor'),
  328. 'Apache Software Foundation'))
  329. and (*/self::ng:* or */self::db:*)">
  330. <xsl:message>Stripping namespace from DocBook 5 document.</xsl:message>
  331. <xsl:variable name="nons">
  332. <xsl:apply-templates mode="stripNS"/>
  333. </xsl:variable>
  334. <xsl:message>Processing stripped document.</xsl:message>
  335. <xsl:apply-templates select="exsl:node-set($nons)"/>
  336. </xsl:when>
  337. <xsl:otherwise>
  338. <xsl:copy-of select="@* | node()"/>
  339. </xsl:otherwise>
  340. </xsl:choose>
  341. </xsl:template>
  342. </xsl:stylesheet>