util.xsl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!-- This file is part of the DITA Open Toolkit project hosted on
  3. Sourceforge.net. See the accompanying license.txt file for
  4. applicable licenses.-->
  5. <!-- (c) Copyright IBM Corp. 2004, 2005 All Rights Reserved. -->
  6. <xsl:stylesheet version="2.0"
  7. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  8. <xsl:param name="deflatePrefix" select="'deflate'"/>
  9. <xsl:param name="deflateShow" select="false()"/>
  10. <xsl:output
  11. method="xml"
  12. indent="yes"
  13. omit-xml-declaration="no"
  14. standalone="no"
  15. doctype-public="-//OASIS//DTD DocBook XML V4.1.2//EN"
  16. doctype-system="docbookx.dtd"/>
  17. <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  18. - TURN ELEMENTS, ATTRIBUTES, AND TEXT INTO PROCESSING INSTRUCTIONS
  19. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  20. <xsl:template name="deflateNode">
  21. <xsl:apply-templates select="." mode="deflate">
  22. <xsl:with-param name="descendentsOkay" select="true()"/>
  23. </xsl:apply-templates>
  24. </xsl:template>
  25. <xsl:template match="*" mode="deflate" name="deflate">
  26. <xsl:param name="parentID" select="''"/>
  27. <xsl:param name="descendentsOkay" select="false()"/>
  28. <xsl:param name="textOkay" select="false()"/>
  29. <xsl:variable name="id">
  30. <xsl:choose>
  31. <xsl:when test="$parentID=''">
  32. <xsl:value-of select="concat('elem', generate-id())"/>
  33. </xsl:when>
  34. <xsl:otherwise>
  35. <xsl:value-of select="concat($parentID, '.', position())"/>
  36. </xsl:otherwise>
  37. </xsl:choose>
  38. </xsl:variable>
  39. <xsl:variable name="element" select="local-name(.)"/>
  40. <xsl:if test="$deflateShow">
  41. <xsl:call-template name="deflateElementStart">
  42. <xsl:with-param name="id" select="$id"/>
  43. <xsl:with-param name="element" select="$element"/>
  44. <xsl:with-param name="parentID" select="$parentID"/>
  45. </xsl:call-template>
  46. </xsl:if>
  47. <xsl:choose>
  48. <xsl:when test="$descendentsOkay=true()">
  49. <xsl:apply-templates select="*|text()"/>
  50. </xsl:when>
  51. <xsl:when test="$textOkay=true()">
  52. <xsl:apply-templates select="*|text()" mode="deflatetext">
  53. <xsl:with-param name="parentID" select="$id"/>
  54. </xsl:apply-templates>
  55. </xsl:when>
  56. <xsl:otherwise>
  57. <xsl:apply-templates select="*|text()" mode="deflate">
  58. <xsl:with-param name="parentID" select="$id"/>
  59. </xsl:apply-templates>
  60. </xsl:otherwise>
  61. </xsl:choose>
  62. <xsl:if test="$deflateShow">
  63. <xsl:call-template name="deflateElementEnd">
  64. <xsl:with-param name="id" select="$id"/>
  65. <xsl:with-param name="element" select="$element"/>
  66. </xsl:call-template>
  67. </xsl:if>
  68. </xsl:template>
  69. <xsl:template match="text()" mode="deflatetext">
  70. <xsl:apply-templates select="."/>
  71. </xsl:template>
  72. <xsl:template match="*" mode="deflatetext">
  73. <xsl:param name="parentID" select="''"/>
  74. <xsl:call-template name="deflate">
  75. <xsl:with-param name="parentID" select="$parentID"/>
  76. <xsl:with-param name="textOkay" select="true()"/>
  77. </xsl:call-template>
  78. </xsl:template>
  79. <xsl:template name="deflateElementName">
  80. <xsl:param name="element" select="local-name(.)"/>
  81. <xsl:if test="$deflateShow">
  82. <xsl:processing-instruction name="{$deflatePrefix}-element-name">
  83. <xsl:text>name="</xsl:text>
  84. <xsl:value-of select="$element"/>
  85. <xsl:text>"</xsl:text>
  86. </xsl:processing-instruction>
  87. </xsl:if>
  88. </xsl:template>
  89. <xsl:template name="deflateElementStart">
  90. <xsl:param name="element" select="local-name(.)"/>
  91. <xsl:param name="parentID" select="''"/>
  92. <xsl:param name="id"/>
  93. <xsl:if test="$deflateShow">
  94. <xsl:processing-instruction name="{$deflatePrefix}-element-start">
  95. <xsl:text>id="</xsl:text>
  96. <xsl:value-of select="$id"/>
  97. <xsl:text>" parentID="</xsl:text>
  98. <xsl:value-of select="$parentID"/>
  99. <xsl:text>" name="</xsl:text>
  100. <xsl:value-of select="$element"/>
  101. <xsl:text>"</xsl:text>
  102. </xsl:processing-instruction>
  103. <xsl:apply-templates select="@*" mode="deflate">
  104. <xsl:with-param name="parentID" select="$id"/>
  105. </xsl:apply-templates>
  106. </xsl:if>
  107. </xsl:template>
  108. <xsl:template name="deflateElementEnd">
  109. <xsl:param name="element" select="local-name(.)"/>
  110. <xsl:param name="id"/>
  111. <xsl:if test="$deflateShow">
  112. <!-- no parent ID attribute so ignored when reconstituting -->
  113. <xsl:processing-instruction name="{$deflatePrefix}-element-end">
  114. <xsl:text>id="</xsl:text>
  115. <xsl:value-of select="$id"/>
  116. <xsl:text>" name="</xsl:text>
  117. <xsl:value-of select="$element"/>
  118. <xsl:text>"</xsl:text>
  119. </xsl:processing-instruction>
  120. </xsl:if>
  121. </xsl:template>
  122. <!-- turn an attribute into a processing instruction -->
  123. <xsl:template match="@*" mode="deflate">
  124. <xsl:param name="parentID" select="''"/>
  125. <xsl:if test="$deflateShow">
  126. <!-- Because the value attribute is last, the value can contain quotes. -->
  127. <xsl:processing-instruction name="{$deflatePrefix}-attribute">
  128. <xsl:text>parentID="</xsl:text>
  129. <xsl:value-of select="$parentID"/>
  130. <xsl:text>" name="</xsl:text>
  131. <xsl:value-of select="local-name(.)"/>
  132. <xsl:text>" value="</xsl:text>
  133. <xsl:value-of select="."/>
  134. <xsl:text>"</xsl:text>
  135. </xsl:processing-instruction>
  136. </xsl:if>
  137. </xsl:template>
  138. <!-- turn text into a processing instruction -->
  139. <xsl:template match="text()" mode="deflate">
  140. <xsl:param name="parentID" select="''"/>
  141. <xsl:if test="$deflateShow">
  142. <!-- The final quote prevents errors if the text ends with a question mark.
  143. The text can contain quotes. -->
  144. <xsl:processing-instruction name="{$deflatePrefix}-text">
  145. <xsl:text>parentID="</xsl:text>
  146. <xsl:value-of select="$parentID"/>
  147. <xsl:text>" value="</xsl:text>
  148. <xsl:value-of select="."/>
  149. <xsl:text>"</xsl:text>
  150. </xsl:processing-instruction>
  151. </xsl:if>
  152. </xsl:template>
  153. </xsl:stylesheet>