stripns.xsl 12 KB

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