impimr2xml-finalize-conditional-styles.xsl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?xml-stylesheet type="text/xsl" ?>
  3. <!--
  4. /*****************************************************************************
  5. * Licensed Materials - Property of IBM
  6. *
  7. * IBM Cognos Products: migv4
  8. *
  9. * (C) Copyright IBM Corp. 2003, 2011.
  10. *
  11. * US Government Users Restricted Rights - Use, duplication or disclosure
  12. * restricted by GSA ADP Schedule Contract with IBM Corp.
  13. ****************************************************************************/
  14. -->
  15. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1"
  16. xmlns="http://developer.cognos.com/schemas/report/1/"
  17. xmlns:x="http://developer.cognos.com/schemas/report/1/"
  18. xmlns:str="http://www.ora.com/XSLTCookbook/namespaces/strings"
  19. extension-element-prefixes="str"
  20. exclude-result-prefixes="x str"
  21. >
  22. <xsl:import href="copy.xslt"/>
  23. <xsl:import href="str.find-last.xslt" xmlns:str="http://www.ora.com/XSLTCookbook/namespaces/strings" />
  24. <xsl:output method="xml" />
  25. <xsl:strip-space elements="*"/>
  26. <xsl:preserve-space elements="x:defaultSelection x:text"/>
  27. <!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
  28. <xsl:template name="merge-conditional-style-variable-expressions">
  29. <xsl:param name="nodes"/>
  30. <xsl:param name="pos"/>
  31. <xsl:variable name="current-variable" select="$nodes[$pos]"/>
  32. <xsl:choose>
  33. <xsl:when test="$current-variable">
  34. <xsl:variable name="expr-text" select="$current-variable/x:expression"/>
  35. <xsl:variable name="tail" select="' then ( 1 ) else ( 0 )'"/>
  36. <xsl:variable name="head">
  37. <xsl:call-template name="str:substring-before-last">
  38. <xsl:with-param name="input" select="$expr-text"/>
  39. <xsl:with-param name="substr" select="$tail"/>
  40. </xsl:call-template>
  41. </xsl:variable>
  42. <xsl:variable name="new-expr-text">
  43. <xsl:value-of select="$head"/>
  44. <xsl:text> then (</xsl:text>
  45. <xsl:text> </xsl:text>
  46. <xsl:text>'</xsl:text>
  47. <xsl:value-of select="$pos"/>
  48. <xsl:text>'</xsl:text>
  49. <xsl:text> )</xsl:text>
  50. <xsl:text> else (</xsl:text>
  51. <xsl:call-template name="merge-conditional-style-variable-expressions">
  52. <xsl:with-param name="nodes" select="$nodes"/>
  53. <xsl:with-param name="pos" select="$pos+1"/>
  54. </xsl:call-template>
  55. <xsl:text> )</xsl:text>
  56. </xsl:variable>
  57. <xsl:value-of select="$new-expr-text"/>
  58. </xsl:when>
  59. <xsl:otherwise>
  60. <xsl:text> </xsl:text>
  61. <xsl:text>'</xsl:text>
  62. <xsl:text>0</xsl:text>
  63. <xsl:text>'</xsl:text>
  64. <!-- and quit (stop recursion) -->
  65. </xsl:otherwise>
  66. </xsl:choose>
  67. </xsl:template>
  68. <!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
  69. <xsl:template match="x:variableList">
  70. <xsl:variable name="variable-list" select="."/>
  71. <xsl:variable name="conditional-style-parents" select="//*[ x:conditionalStyle ]"/>
  72. <variableList>
  73. <xsl:for-each select="$conditional-style-parents">
  74. <xsl:if test="count( x:conditionalStyle ) &gt; 1">
  75. <!-- Build the new single combined selector variable for each set of sibling conditionalStyle-s: -->
  76. <xsl:variable name="conditional-styles" select="x:conditionalStyle"/>
  77. <xsl:variable name="referenced-variables" select="$variable-list/x:variable[ @name = $conditional-styles/@refVariable ]"/>
  78. <variable>
  79. <xsl:attribute name="name">
  80. <xsl:call-template name="make-selector-name">
  81. <xsl:with-param name="parent-node-of-conditional-styles" select="."/>
  82. </xsl:call-template>
  83. </xsl:attribute>
  84. <xsl:attribute name="type">
  85. <xsl:text>string</xsl:text>
  86. </xsl:attribute>
  87. <expression>
  88. <xsl:call-template name="merge-conditional-style-variable-expressions">
  89. <xsl:with-param name="nodes" select="$referenced-variables"/>
  90. <xsl:with-param name="pos" select="1"/>
  91. </xsl:call-template>
  92. </expression>
  93. <variableValueList>
  94. <!-- Add conditional values: -->
  95. <xsl:for-each select="$referenced-variables">
  96. <variableValue >
  97. <xsl:attribute name="value">
  98. <xsl:value-of select="position()"/>
  99. </xsl:attribute>
  100. </variableValue>
  101. </xsl:for-each>
  102. <!-- Add the default value: -->
  103. <variableValue >
  104. <xsl:attribute name="value">
  105. <xsl:value-of select="0"/>
  106. </xsl:attribute>
  107. </variableValue>
  108. </variableValueList>
  109. </variable>
  110. </xsl:if>
  111. </xsl:for-each>
  112. <!-- Copy all pre-existing report variables: -->
  113. <!-- !! ToDo: The variable-s that are referred to *only* from conditionalStyle-s can be deleted from this variableList here.
  114. That is: we may want not to copy any node that is *only* in this node set:
  115. <xsl:variable name="report-variables-for-conditional-style" select="x:variable[ @name = //x:conditionalStyle/@refVariable ]"/>
  116. -->
  117. <xsl:copy-of select="x:variable"/>
  118. </variableList>
  119. </xsl:template>
  120. <!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
  121. <xsl:template name="make-selector-name">
  122. <xsl:param name="parent-node-of-conditional-styles"/>
  123. <xsl:text>ConditionalStyleSelector</xsl:text>
  124. <!-- Make sure this name is unique among report variable-s in the variableList. -->
  125. <xsl:text>_</xsl:text>
  126. <xsl:value-of select="generate-id( $parent-node-of-conditional-styles )"/>
  127. </xsl:template>
  128. <!-- = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -->
  129. <xsl:template match="x:conditionalStyle">
  130. <!-- Delete.
  131. All sibling conditionalStyle elements will be merged and "single child" conditionalStyle elements will be copied
  132. by other template in this stylesheet. -->
  133. </xsl:template>
  134. <xsl:template match="*[ x:conditionalStyle ]">
  135. <xsl:copy>
  136. <xsl:copy-of select="@*"/>
  137. <xsl:variable name="conditional-styles" select="x:conditionalStyle"/>
  138. <xsl:choose>
  139. <xsl:when test="count( $conditional-styles) &gt; 1">
  140. <!-- Copy all contents except sibling conditionalStyle-s (they are removed by other template in this stylesheet) -->
  141. <xsl:apply-templates/>
  142. <!-- Merge sibling conditionalStyle-s: -->
  143. <conditionalStyle>
  144. <xsl:attribute name="refVariable">
  145. <xsl:call-template name="make-selector-name">
  146. <xsl:with-param name="parent-node-of-conditional-styles" select="."/>
  147. </xsl:call-template>
  148. </xsl:attribute>
  149. <xsl:for-each select="$conditional-styles">
  150. <style>
  151. <xsl:attribute name="refVariableValue">
  152. <xsl:value-of select="position()"/>
  153. </xsl:attribute>
  154. <xsl:copy-of select=".//x:CSS"/>
  155. </style>
  156. </xsl:for-each>
  157. </conditionalStyle>
  158. </xsl:when>
  159. <xsl:otherwise>
  160. <!-- Copy all contents except "single child" conditionalStyle (it is removed by other template in tis stylesheet) -->
  161. <xsl:apply-templates/>
  162. <!-- Copy "single child" conditionalStyle: -->
  163. <xsl:copy-of select="$conditional-styles"/>
  164. </xsl:otherwise>
  165. </xsl:choose>
  166. </xsl:copy>
  167. </xsl:template>
  168. </xsl:stylesheet>