insertfile.xsl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:xi="http://www.w3.org/2001/XInclude"
  4. version='1.0'>
  5. <!-- ********************************************************************
  6. $Id: insertfile.xsl 5262 2005-10-12 14:58:42Z xmldoc $
  7. ********************************************************************
  8. This file is part of the XSL DocBook Stylesheet distribution.
  9. See ../README or http://docbook.sf.net/release/xsl/current/ for
  10. copyright and other information.
  11. ******************************************************************** -->
  12. <xsl:param name="textdata.default.encoding"></xsl:param>
  13. <!-- * This stylesheet makes a copy of a source tree, replacing all -->
  14. <!-- * instances of the following with corresponding Xinclude instances -->
  15. <!-- * in the result tree. -->
  16. <!-- * -->
  17. <!-- * <textobject><textdata fileref="foo.txt"> -->
  18. <!-- * <imagedata format="linespecific" fileref="foo.txt"> -->
  19. <!-- * <inlinegraphic format="linespecific" fileref="foo.txt"> -->
  20. <!-- * -->
  21. <!-- * Those become: -->
  22. <!-- * -->
  23. <!-- * <xi:include href="foo.txt" parse="text"/> -->
  24. <!-- * -->
  25. <!-- * It also works as expected with entityref in place of fileref, -->
  26. <!-- * and copies over the value of the <textdata>“encoding” atrribute (if -->
  27. <!-- * found). It is basically intended as an alternative to using the -->
  28. <!-- * DocBook XSLT Java insertfile() extension. -->
  29. <!-- ==================================================================== -->
  30. <xsl:template name="get.external.filename">
  31. <xsl:choose>
  32. <xsl:when test="@entityref">
  33. <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
  34. </xsl:when>
  35. <xsl:otherwise>
  36. <xsl:value-of select="@fileref"/>
  37. </xsl:otherwise>
  38. </xsl:choose>
  39. </xsl:template>
  40. <!-- ==================================================================== -->
  41. <xsl:template match="textobject[child::textdata[@entityref|@fileref]]">
  42. <xsl:apply-templates select="textdata"/>
  43. </xsl:template>
  44. <xsl:template match="textdata[@entityref|@fileref]">
  45. <xsl:variable name="filename">
  46. <xsl:call-template name="get.external.filename"/>
  47. </xsl:variable>
  48. <xsl:variable name="encoding">
  49. <xsl:choose>
  50. <xsl:when test="@encoding">
  51. <xsl:value-of select="@encoding"/>
  52. </xsl:when>
  53. <xsl:otherwise>
  54. <xsl:value-of select="$textdata.default.encoding"/>
  55. </xsl:otherwise>
  56. </xsl:choose>
  57. </xsl:variable>
  58. <xi:include href="{$filename}" parse="text" encoding="{$encoding}"/>
  59. </xsl:template>
  60. <!-- ==================================================================== -->
  61. <xsl:template
  62. match="inlinemediaobject
  63. [child::imageobject
  64. [child::imagedata
  65. [@format = 'linespecific' and
  66. (@entityref|@fileref)]]]">
  67. <xsl:apply-templates select="imageobject/imagedata"/>
  68. </xsl:template>
  69. <xsl:template match="imagedata
  70. [@format = 'linespecific' and
  71. (@entityref|@fileref)]">
  72. <xsl:variable name="filename">
  73. <xsl:call-template name="get.external.filename"/>
  74. </xsl:variable>
  75. <xi:include href="{$filename}" parse="text" encoding="{$textdata.default.encoding}"/>
  76. </xsl:template>
  77. <!-- ==================================================================== -->
  78. <xsl:template match="inlinegraphic
  79. [@format = 'linespecific' and
  80. (@entityref|@fileref)]">
  81. <xsl:variable name="filename">
  82. <xsl:call-template name="get.external.filename"/>
  83. </xsl:variable>
  84. <xi:include href="{$filename}" parse="text" encoding="{$textdata.default.encoding}"/>
  85. </xsl:template>
  86. <!-- ==================================================================== -->
  87. <!-- * copy everything else into result tree as-is -->
  88. <xsl:template match="node() | @*">
  89. <xsl:copy>
  90. <xsl:apply-templates select="@* | node()"/>
  91. </xsl:copy>
  92. </xsl:template>
  93. </xsl:stylesheet>