common.xsl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. version='1.0'>
  4. <xsl:key name="column_id" match="column" use="@id|xml:id"/>
  5. <xsl:param name="prefix" select="_"/>
  6. <xsl:param name="dir" select="mm"/>
  7. <xsl:param name="db" select="_"/>
  8. <xsl:variable name="sign-prefix">unsigned </xsl:variable>
  9. <!-- Do not output text -->
  10. <xsl:template match="text()|@*"/>
  11. <xsl:template match="text()|@*" mode="drop"/>
  12. <!-- Return the name of the context element, first look for a database
  13. specific name, use the common name if no database-specific name
  14. is found.
  15. -->
  16. <xsl:template name="get-name">
  17. <xsl:param name="select" select="."/>
  18. <xsl:choose>
  19. <!-- override test -->
  20. <xsl:when test="count($select/name[@db=$db])='1'">
  21. <xsl:value-of select="normalize-space($select/name[@db=$db])"/>
  22. </xsl:when>
  23. <!-- No override, use the standard name -->
  24. <xsl:otherwise>
  25. <xsl:value-of select="normalize-space($select/name)"/>
  26. </xsl:otherwise>
  27. </xsl:choose>
  28. </xsl:template>
  29. <xsl:template name="type-error">
  30. <xsl:message terminate="yes">
  31. <xsl:text>ERROR: Table: </xsl:text>
  32. <xsl:value-of select="normalize-space(parent::table/name)"/>
  33. <xsl:text>, column: </xsl:text>
  34. <xsl:value-of select="normalize-space(name)"/>
  35. <xsl:text> - unsupported column type: </xsl:text>
  36. <xsl:value-of select="normalize-space(type)"/>
  37. <xsl:text>.</xsl:text>
  38. </xsl:message>
  39. </xsl:template>
  40. <!-- Process the root database element -->
  41. <xsl:template match="/">
  42. <!-- Process only the first database element, this is supposed to be
  43. the root database element and having multiple database elements is
  44. a bug
  45. -->
  46. <xsl:apply-templates select="database[1]"/>
  47. </xsl:template>
  48. <!-- ################ DATABASE ################# -->
  49. <xsl:template match="database">
  50. <!-- Create all tables -->
  51. <xsl:apply-templates select="table"/>
  52. <xsl:apply-templates select="user"/>
  53. </xsl:template>
  54. <xsl:template match="database" mode="data">
  55. <!-- Insert initial data -->
  56. <xsl:apply-templates select="table" mode="data"/>
  57. </xsl:template>
  58. <!-- ################ /DATABASE ################# -->
  59. <!-- ################ TABLE ################# -->
  60. <xsl:template match="table">
  61. <!-- Process all columns -->
  62. <xsl:apply-templates select="column"/>
  63. <!-- Process all indexes -->
  64. <xsl:apply-templates select="index"/>
  65. <!-- Process initial data -->
  66. <xsl:apply-templates select="row"/>
  67. </xsl:template>
  68. <!-- ################ /TABLE ################# -->
  69. <!-- ################ COLUMN ################# -->
  70. <xsl:template match="column"/>
  71. <xsl:template name="get-type-string">
  72. <xsl:param name="select" select="."/>
  73. <xsl:choose>
  74. <xsl:when test="count($select/type[@db=$db])='1'">
  75. <xsl:value-of select="translate(normalize-space($select/type[@db=$db]),
  76. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
  77. </xsl:when>
  78. <xsl:otherwise>
  79. <xsl:value-of select="translate(normalize-space($select/type),
  80. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
  81. </xsl:otherwise>
  82. </xsl:choose>
  83. </xsl:template>
  84. <xsl:template name="get-type">
  85. <xsl:param name="select" select="."/>
  86. <xsl:variable name="type">
  87. <xsl:call-template name="get-type-string">
  88. <xsl:with-param name="select" select="$select"/>
  89. </xsl:call-template>
  90. </xsl:variable>
  91. <xsl:choose>
  92. <xsl:when test="starts-with($type, $sign-prefix)">
  93. <xsl:value-of select="substring-after($type, $sign-prefix)"/>
  94. </xsl:when>
  95. <xsl:otherwise>
  96. <xsl:value-of select="$type"/>
  97. </xsl:otherwise>
  98. </xsl:choose>
  99. </xsl:template>
  100. <xsl:template name="get-sign">
  101. <xsl:param name="select" select="."/>
  102. <xsl:variable name="type">
  103. <xsl:call-template name="get-type-string">
  104. <xsl:with-param name="select" select="$select"/>
  105. </xsl:call-template>
  106. </xsl:variable>
  107. <xsl:choose>
  108. <xsl:when test="starts-with($type, $sign-prefix)">0</xsl:when>
  109. <xsl:otherwise>1</xsl:otherwise>
  110. </xsl:choose>
  111. </xsl:template>
  112. <xsl:template name="get-null">
  113. <xsl:param name="select" select="."/>
  114. <xsl:choose>
  115. <xsl:when test="count($select/null[@db=$db])='1'">1</xsl:when>
  116. <xsl:when test="count($select/null)='1'">1</xsl:when>
  117. <xsl:otherwise>0</xsl:otherwise>
  118. </xsl:choose>
  119. </xsl:template>
  120. <xsl:template name="get-size">
  121. <xsl:param name="select" select="."/>
  122. <xsl:choose>
  123. <xsl:when test="count($select/size[@db=$db])='1'">
  124. <xsl:value-of select="normalize-space($select/size[@db=$db])"/>
  125. </xsl:when>
  126. <xsl:otherwise>
  127. <xsl:value-of select="normalize-space($select/size)"/>
  128. </xsl:otherwise>
  129. </xsl:choose>
  130. </xsl:template>
  131. <!-- column ID to column name -->
  132. <xsl:template name="get-column-name">
  133. <xsl:param name="select" select="."/>
  134. <xsl:variable name="columns" select="key('column_id', $select)"/>
  135. <xsl:variable name="column" select="$columns[1]"/>
  136. <xsl:choose>
  137. <xsl:when test="count($column) = 0">
  138. <xsl:message terminate="yes">
  139. <xsl:text>ERROR: Column with id '</xsl:text>
  140. <xsl:value-of select="$select"/>
  141. <xsl:text>' does not exist.</xsl:text>
  142. </xsl:message>
  143. </xsl:when>
  144. <xsl:otherwise>
  145. <xsl:call-template name="get-name">
  146. <xsl:with-param name="select" select="$column"/>
  147. </xsl:call-template>
  148. </xsl:otherwise>
  149. </xsl:choose>
  150. </xsl:template>
  151. <!-- ################ /COLUMN ################# -->
  152. <!-- ################ INDEX ################# -->
  153. <xsl:template match="index">
  154. <xsl:apply-templates match="colref"/>
  155. </xsl:template>
  156. <!-- ################ /INDEX ################# -->
  157. <!-- ################ COLREF ################# -->
  158. <xsl:template match="colref"/>
  159. <xsl:template name="get-column">
  160. <xsl:param name="id" select="/.."/>
  161. <xsl:variable name="columns" select="key('column_id', $id)"/>
  162. <xsl:variable name="column" select="$columns[1]"/>
  163. <xsl:choose>
  164. <xsl:when test="count($column) = 0">
  165. <xsl:message terminate="yes">
  166. <xsl:text>ERROR: Column with id '</xsl:text>
  167. <xsl:value-of select="$id"/>
  168. <xsl:text>' does not exist.</xsl:text>
  169. </xsl:message>
  170. </xsl:when>
  171. <xsl:otherwise>
  172. <xsl:copy-of select="$column"/>
  173. </xsl:otherwise>
  174. </xsl:choose>
  175. </xsl:template>
  176. <!-- ################ /COLREF ################# -->
  177. <!-- ################ ROW ################# -->
  178. <xsl:template match="row">
  179. <xsl:apply-templates select="value"/>
  180. </xsl:template>
  181. <!-- ################ /ROW ################# -->
  182. <!-- ################ VALUE ################# -->
  183. <xsl:template match="value"/>
  184. <!-- ################ /VALUE ################# -->
  185. <!-- ################ USER ################# -->
  186. <xsl:template match="user"/>
  187. <!-- ################ /USER ################# -->
  188. </xsl:stylesheet>