stripns.xsl 13 KB

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