xform_18to19.xsl 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?xml version="1.0"?>
  2. <!--
  3. Licensed Materials - Property of IBM
  4. IBM Cognos Products: fmmd
  5. (C) Copyright IBM Corp. 2003, 2013
  6. US Government Users Restricted Rights - Use, duplication or disclosure
  7. restricted by GSA ADP Schedule Contract with IBM Corp.
  8. -->
  9. <!--XSLT version 18-->
  10. <!--XSL Transformation from a schema v18 model to a valid BMT schema v19 model-->
  11. <xsl:stylesheet xmlns="http://www.developer.cognos.com/schemas/bmt/19/0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xalan="http://xml.apache.org/xslt" extension-element-prefixes="xalan">
  12. <xsl:output encoding="utf-8" method="xml" indent="yes"/>
  13. <xsl:key name="DS_KEY" match="*[local-name()='source']" use="concat(*[local-name()='database'], ':', *[local-name()='catalog'], ':', *[local-name()='schema'])"/>
  14. <!--root match-->
  15. <xsl:template match="/">
  16. <!--make sure that this transformation is performed on a schema v18 model-->
  17. <xsl:if test="not(contains(/*[local-name() = 'project']/@xsi:schemaLocation, 'http://www.developer.cognos.com/schemas/bmt/18')) ">
  18. <xsl:message terminate="yes">The input file for this transformer must be a schema v18 model.</xsl:message>
  19. </xsl:if>
  20. <xsl:apply-templates/>
  21. </xsl:template>
  22. <!-- general copy -->
  23. <xsl:template match="*">
  24. <xsl:element name="{name(.)}">
  25. <xsl:for-each select="@*">
  26. <xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
  27. </xsl:for-each>
  28. <xsl:apply-templates/>
  29. </xsl:element>
  30. </xsl:template>
  31. <!--change the schema to v19 version-->
  32. <xsl:template match="*[local-name()='project'] ">
  33. <xsl:element name="project" namespace="http://www.developer.cognos.com/schemas/bmt/19/0">
  34. <xsl:for-each select="@*">
  35. <xsl:choose>
  36. <!--change schema from v18 to v19-->
  37. <xsl:when test="name(.) = 'xsi:schemaLocation'">
  38. <xsl:attribute name="{name(.)}"><xsl:value-of select="'http://www.developer.cognos.com/schemas/bmt/19/0 BMTModelSpecification.xsd'"/></xsl:attribute>
  39. </xsl:when>
  40. <xsl:otherwise>
  41. <xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
  42. </xsl:otherwise>
  43. </xsl:choose>
  44. </xsl:for-each>
  45. <xsl:apply-templates/>
  46. </xsl:element>
  47. </xsl:template>
  48. <!-- Centralize data sources -->
  49. <xsl:variable name="dataSourceGroup">
  50. <sources>
  51. <xsl:for-each select="//*[local-name() = 'source'] ">
  52. <xsl:if test="string(*[local-name()='database'])">
  53. <xsl:variable name="srcKey" select="concat(*[local-name()='database'], ':', *[local-name()='catalog'], ':', *[local-name()='schema'] )"/>
  54. <xsl:if test="generate-id() = generate-id ( key('DS_KEY', $srcKey) [1] )">
  55. <xsl:copy-of select="."/>
  56. </xsl:if>
  57. </xsl:if>
  58. </xsl:for-each>
  59. </sources>
  60. </xsl:variable>
  61. <xsl:template match="*[local-name()='dataSources']">
  62. <dataSources>
  63. <xsl:for-each select="xalan:nodeset($dataSourceGroup)/*/*[local-name()='source']">
  64. <dataSource>
  65. <name>
  66. <xsl:value-of select="concat('DS', position())"/>
  67. </name>
  68. <cmDataSource>
  69. <xsl:value-of select="*[local-name()= 'database']"/>
  70. </cmDataSource>
  71. <xsl:if test="*[local-name()= 'catalog']">
  72. <catalog>
  73. <xsl:value-of select="*[local-name()= 'catalog']"/>
  74. </catalog>
  75. </xsl:if>
  76. <xsl:if test="*[local-name()= 'schema']">
  77. <schema>
  78. <xsl:value-of select="*[local-name()= 'schema']"/>
  79. </schema>
  80. </xsl:if>
  81. <xsl:if test="*[local-name()= 'databaseType']">
  82. <type>
  83. <xsl:value-of select="*[local-name()= 'databaseType']"/>
  84. </type>
  85. </xsl:if>
  86. </dataSource>
  87. </xsl:for-each>
  88. </dataSources>
  89. </xsl:template>
  90. <!-- Remove sources from all query subjects -->
  91. <!-- Dropped as requested by Glen Seeds -->
  92. <!-- xsl:template match="*[local-name()='sources' ]"/-->
  93. <!-- Convert a string, removing leading and trailing whitespace and double quots-->
  94. <xsl:template name="Normalize">
  95. <xsl:param name="str"/>
  96. <xsl:variable name="noSpace" select="normalize-space($str)"/>
  97. <xsl:choose>
  98. <xsl:when test="starts-with($noSpace, '&quot;') and substring($noSpace, string-length(), 1)='&quot;' ">
  99. <xsl:value-of select="substring($str, 2, string-length()-2)"/>
  100. </xsl:when>
  101. <xsl:otherwise>
  102. <xsl:value-of select="$str"/>
  103. </xsl:otherwise>
  104. </xsl:choose>
  105. </xsl:template>
  106. <!-- Generate data source reference based on database, catalog and schema -->
  107. <xsl:template name="GetDataSourceRef">
  108. <xsl:param name="db" select="*[local-name() = 'database']"/>
  109. <xsl:param name="cat" select="*[local-name() = 'catalog']"/>
  110. <xsl:param name="schema" select="*[local-name() = 'schema']"/>
  111. <xsl:variable name="srcKey">
  112. <xsl:text>[</xsl:text>
  113. <xsl:call-template name="Normalize">
  114. <xsl:with-param name="str" select="$db"/>
  115. </xsl:call-template>
  116. <xsl:text>.</xsl:text>
  117. <xsl:call-template name="Normalize">
  118. <xsl:with-param name="str" select="$cat"/>
  119. </xsl:call-template>
  120. <xsl:text>.</xsl:text>
  121. <xsl:call-template name="Normalize">
  122. <xsl:with-param name="str" select="$schema"/>
  123. </xsl:call-template>
  124. <xsl:text>]</xsl:text>
  125. </xsl:variable>
  126. <xsl:for-each select="xalan:nodeset($dataSourceGroup)/*/*[local-name()='source']">
  127. <xsl:variable name="dsKey">
  128. <xsl:text>[</xsl:text>
  129. <xsl:call-template name="Normalize">
  130. <xsl:with-param name="str" select="*[local-name()='database']"/>
  131. </xsl:call-template>
  132. <xsl:text>.</xsl:text>
  133. <xsl:call-template name="Normalize">
  134. <xsl:with-param name="str" select="*[local-name()='catalog']"/>
  135. </xsl:call-template>
  136. <xsl:text>.</xsl:text>
  137. <xsl:call-template name="Normalize">
  138. <xsl:with-param name="str" select="*[local-name()='schema']"/>
  139. </xsl:call-template>
  140. <xsl:text>]</xsl:text>
  141. </xsl:variable>
  142. <xsl:if test="$srcKey=$dsKey ">
  143. <xsl:value-of select="concat('[DS', position(), ']')"/>
  144. </xsl:if>
  145. </xsl:for-each>
  146. </xsl:template>
  147. <!-- Convert stored procedure and mdQuery to use data source reference -->
  148. <xsl:template match="*[local-name() = 'storedProcedure' or local-name() = 'mdQuery'] / *[local-name() = 'source' ]">
  149. <dataSourceRef>
  150. <xsl:text>[].[dataSources].</xsl:text>
  151. <xsl:call-template name="GetDataSourceRef"/>
  152. </dataSourceRef>
  153. <!-- put back the source as requested by Glen Seeds -->
  154. <source>
  155. <xsl:apply-templates/>
  156. </source>
  157. </xsl:template>
  158. <!--Convert dbQuery/sql to use data source reference -->
  159. <xsl:template match="* [local-name() = 'dbQuery' ]">
  160. <dbQuery>
  161. <xsl:for-each select="@*">
  162. <xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
  163. </xsl:for-each>
  164. <xsl:choose>
  165. <xsl:when test="*[local-name() = 'sql']/@type='cognos' ">
  166. <xsl:apply-templates select="*[local-name()='sources' or local-name()='genearateSQL']"/>
  167. <xsl:for-each select="*[local-name()='sql']">
  168. <sql type="{@type}">
  169. <xsl:for-each select="node()">
  170. <xsl:choose>
  171. <xsl:when test="local-name()='table' and not (contains(., '#')) ">
  172. <table>
  173. <xsl:variable name="dataSource">
  174. <xsl:call-template name="GetDataSourceRef">
  175. <xsl:with-param name="db" select="substring-before(., '.')"/>
  176. <xsl:with-param name="cat" select="substring-before(substring-after(., '.'), '.')"/>
  177. <xsl:with-param name="schema" select="substring-before(substring-after(substring-after(., '.'), '.'), '.')"/>
  178. </xsl:call-template>
  179. </xsl:variable>
  180. <xsl:choose>
  181. <xsl:when test="string($dataSource)">
  182. <xsl:value-of select="$dataSource"/>
  183. </xsl:when>
  184. <xsl:otherwise>[DS1]</xsl:otherwise>
  185. </xsl:choose>
  186. <xsl:variable name="tbl" select="substring-after(substring-after(substring-after(., '.'), '.'), '.') "/>
  187. <xsl:choose>
  188. <xsl:when test="string($tbl)">
  189. <xsl:value-of select="concat('.', $tbl )"/>
  190. </xsl:when>
  191. <xsl:otherwise>
  192. <xsl:value-of select="concat('.', .)"/>
  193. </xsl:otherwise>
  194. </xsl:choose>
  195. </table>
  196. </xsl:when>
  197. <xsl:otherwise>
  198. <xsl:apply-templates select="."/>
  199. </xsl:otherwise>
  200. </xsl:choose>
  201. </xsl:for-each>
  202. </sql>
  203. </xsl:for-each>
  204. <xsl:apply-templates select="*[local-name()!='sources' and local-name()!='generateSQL' and local-name()!= 'sql']"/>
  205. </xsl:when>
  206. <xsl:otherwise>
  207. <dataSourceRef>
  208. <xsl:for-each select="*[local-name()='sources']/*[local-name()='source'][1]">
  209. <xsl:text>[].[dataSources].</xsl:text>
  210. <xsl:call-template name="GetDataSourceRef"/>
  211. </xsl:for-each>
  212. </dataSourceRef>
  213. <xsl:apply-templates/>
  214. </xsl:otherwise>
  215. </xsl:choose>
  216. </dbQuery>
  217. </xsl:template>
  218. </xsl:stylesheet>