xform_17to18.xsl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 17-->
  10. <!--XSL Transformation from a schema v17 model to a valid BMT schema v18 model-->
  11. <xsl:stylesheet xmlns="http://www.developer.cognos.com/schemas/bmt/18/0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  12. <xsl:output encoding="utf-8" method="xml" indent="yes"/>
  13. <xsl:key name="FOLDER_KEY" match="*[local-name()='logicalModel' or local-name() = 'folder' or local-name() = 'queryItemFolder' ]" use="*[local-name()='name']"/>
  14. <!--root match-->
  15. <xsl:template match="/">
  16. <!--make sure that this transformation is performed on a schema v17 model-->
  17. <xsl:if test="not(contains(/*[local-name() = 'project']/@xsi:schemaLocation, 'http://www.developer.cognos.com/schemas/bmt/17')) ">
  18. <xsl:message terminate="yes">The input file for this transformer must be a schema v17 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 v18 version-->
  32. <xsl:template match="*[local-name()='project'] ">
  33. <xsl:element name="project" namespace="http://www.developer.cognos.com/schemas/bmt/18/0">
  34. <xsl:for-each select="@*">
  35. <xsl:choose>
  36. <!--change schema from v17 to v18-->
  37. <xsl:when test="name(.) = 'xsi:schemaLocation'">
  38. <xsl:attribute name="{name(.)}"><xsl:value-of select="'http://www.developer.cognos.com/schemas/bmt/18/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. <!-- change all existing folders to namespaces -->
  49. <xsl:template match="* [ local-name() = 'folder' ]">
  50. <namespace>
  51. <xsl:apply-templates/>
  52. </namespace>
  53. </xsl:template>
  54. <!-- populate shortcut/targetType -->
  55. <xsl:template match="*[local-name() = 'shortcut' ]">
  56. <shortcut>
  57. <xsl:apply-templates select="*"/>
  58. <xsl:variable name="folderRef" select="substring-before(substring-after(*[local-name()='refobj'], '['), ']')"/>
  59. <xsl:variable name="objRef" select="substring-before(substring-after(substring-after(*[local-name()='refobj'], '.'), '['), ']')"/>
  60. <targetType>
  61. <xsl:for-each select="key('FOLDER_KEY', $folderRef)">
  62. <xsl:choose>
  63. <xsl:when test="string($objRef)">
  64. <xsl:for-each select="*/*[local-name()='name' and .=$objRef]">
  65. <xsl:value-of select="local-name(..)"/>
  66. </xsl:for-each>
  67. </xsl:when>
  68. <xsl:otherwise>
  69. <xsl:choose>
  70. <xsl:when test="local-name()='folder'">namespace</xsl:when>
  71. <xsl:otherwise>
  72. <xsl:value-of select="local-name()"/>
  73. </xsl:otherwise>
  74. </xsl:choose>
  75. </xsl:otherwise>
  76. </xsl:choose>
  77. </xsl:for-each>
  78. </targetType>
  79. </shortcut>
  80. </xsl:template>
  81. <!-- fix reference to shortcuts which point to folder or logicalModel -->
  82. <xsl:template match="*[ local-name() = 'refobj' ]">
  83. <xsl:variable name="self" select="."/>
  84. <xsl:variable name="folderRef" select="substring-before(substring-after($self , '['), ']')"/>
  85. <xsl:variable name="objRef" select="substring-before(substring-after(substring-after($self , '.'), '['), ']')"/>
  86. <xsl:variable name="itemRef" select="substring-after(substring-after($self, '.'), '.')"/>
  87. <!--if it points to a shortcut and the shortcut point to a folder, change it -->
  88. <refobj>
  89. <xsl:choose>
  90. <!-- 3 parts ID -->
  91. <xsl:when test="string($itemRef)">
  92. <xsl:value-of select="$self"/>
  93. </xsl:when>
  94. <!-- 2 parts ID need to change for folderShortcut -->
  95. <xsl:when test="string($objRef)">
  96. <xsl:variable name="obj" select="key('FOLDER_KEY', $folderRef)/*[ *[local-name()='name' and .=$objRef] ]"/>
  97. <xsl:choose>
  98. <xsl:when test="local-name($obj)='shortcut' and not(contains($obj/*[local-name() = 'refobj'], '.' ) )">
  99. <xsl:value-of select="concat('[', $objRef, ']')"/>
  100. </xsl:when>
  101. <xsl:otherwise>
  102. <xsl:value-of select="$self"/>
  103. </xsl:otherwise>
  104. </xsl:choose>
  105. </xsl:when>
  106. <!-- 1 part ID, need to change for queryItemFolder if needed-->
  107. <xsl:otherwise>
  108. <xsl:variable name="folder" select="key('FOLDER_KEY', $folderRef)"/>
  109. <xsl:choose>
  110. <xsl:when test="local-name($folder) = 'queryItemFolder' ">
  111. <xsl:call-template name="UniqueQIFName">
  112. <xsl:with-param name="QIF" select="$folder"/>
  113. <xsl:with-param name="type" select=" 'REF' "/>
  114. </xsl:call-template>
  115. </xsl:when>
  116. <xsl:otherwise>
  117. <xsl:value-of select="$self"/>
  118. </xsl:otherwise>
  119. </xsl:choose>
  120. </xsl:otherwise>
  121. </xsl:choose>
  122. </refobj>
  123. </xsl:template>
  124. <!-- fix references to items in reserved collectors -->
  125. <xsl:template match="*[ local-name()= 'viewref' ]">
  126. <viewref>
  127. <xsl:value-of select="concat('[].', .)"/>
  128. </viewref>
  129. </xsl:template>
  130. <!-- Ensure quertyItemFolder name unique -->
  131. <xsl:template name="GetNewQIFName">
  132. <xsl:param name="serialNo" select=" '1' "/>
  133. <xsl:variable name="newName" select="concat(*[local-name()='name'], '~', $serialNo)"/>
  134. <xsl:choose>
  135. <xsl:when test="ancestor::*[local-name()='querySubject']//*[local-name() = 'queryItem' and *[local-name() ='name' and .=$newName]] ">
  136. <xsl:call-template name="GetNewQIFName">
  137. <xsl:with-param name="serialNo" select=" number($serialNo)+1 "/>
  138. </xsl:call-template>
  139. </xsl:when>
  140. <xsl:otherwise>
  141. <xsl:value-of select="$newName"/>
  142. </xsl:otherwise>
  143. </xsl:choose>
  144. </xsl:template>
  145. <xsl:template name="UniqueQIFName">
  146. <xsl:param name="QIF" select="."/>
  147. <xsl:param name="type" select=" 'NAME' "/>
  148. <xsl:if test="$type='REF' ">[</xsl:if>
  149. <xsl:for-each select="$QIF">
  150. <xsl:variable name="name" select="*[local-name()='name']"/>
  151. <xsl:choose>
  152. <xsl:when test="ancestor::*[local-name()='querySubject']//*[local-name() = 'queryItem' and *[local-name() ='name' and .=$name]] ">
  153. <xsl:call-template name="GetNewQIFName"/>
  154. </xsl:when>
  155. <xsl:otherwise>
  156. <xsl:value-of select="$name"/>
  157. </xsl:otherwise>
  158. </xsl:choose>
  159. </xsl:for-each>
  160. <xsl:if test="$type='REF' ">]</xsl:if>
  161. </xsl:template>
  162. <xsl:template match="* [ local-name() = 'queryItemFolder' ]">
  163. <queryItemFolder>
  164. <name locale="{*[local-name()='name']/@locale}">
  165. <xsl:call-template name="UniqueQIFName"/>
  166. </name>
  167. <xsl:apply-templates select="*[local-name() != 'name']"/>
  168. </queryItemFolder>
  169. </xsl:template>
  170. </xsl:stylesheet>