stripns.xsl 12 KB

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