durationProperty.xslt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed Materials - Property of IBM
  4. BI and PM: ps
  5. (C) Copyright IBM Corp. 2010, 2011
  6. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. -->
  8. <!--
  9. Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  10. Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  11. -->
  12. <xsl:stylesheet version="1.0"
  13. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  14. xmlns:out="dummy-uri"
  15. xmlns:psdp="http://developer.cognos.com/schemas/xts/logicsheet/xslt/propertiesSettings/durationProperty/"
  16. exclude-result-prefixes="xsl">
  17. <xsl:output method="xml" encoding="UTF-8" indent="no"/>
  18. <xsl:namespace-alias result-prefix="xsl" stylesheet-prefix="out"/>
  19. <xsl:template match="psdp:durationConvertors">
  20. <!--
  21. Generates the following formatted string unitNbr:unit from
  22. the formatted PnYnMnDTnHnMnS; it extracts the most significant value from
  23. month to second
  24. Example:
  25. P1Y23M will generate 23:months
  26. P34DT12H will generate 34:days
  27. -->
  28. <out:template name="parseDurationValue">
  29. <out:param name="duration"/>
  30. <!-- Extract year, month and days -->
  31. <out:variable name="yearMonthDay">
  32. <out:choose>
  33. <out:when test="contains($duration,'T')">
  34. <out:value-of select="substring-before($duration,'T')"/>
  35. </out:when>
  36. <out:otherwise>
  37. <out:value-of select="$duration"/>
  38. </out:otherwise>
  39. </out:choose>
  40. </out:variable>
  41. <!-- extract month day -->
  42. <out:variable name="monthDay">
  43. <out:choose>
  44. <out:when test="contains($yearMonthDay,'Y')">
  45. <out:value-of select="substring-after($yearMonthDay,'Y')"/>
  46. </out:when>
  47. <out:otherwise>
  48. <out:value-of select="substring-after($yearMonthDay,'P')"/>
  49. </out:otherwise>
  50. </out:choose>
  51. </out:variable>
  52. <!-- Extract the hour minute seconds -->
  53. <out:variable name="hourMinuteSecond">
  54. <out:choose>
  55. <out:when test="contains($duration,'T')">
  56. <out:value-of select="substring-after($duration,'T')"/>
  57. </out:when>
  58. <out:otherwise></out:otherwise>
  59. </out:choose>
  60. </out:variable>
  61. <out:variable name="parsedDuration">
  62. <out:choose>
  63. <out:when test="$monthDay!=''">
  64. <out:choose>
  65. <out:when test="contains($monthDay,'M')">
  66. <out:variable name="months">
  67. <out:value-of select="substring-before($monthDay,'M')"/>
  68. </out:variable>
  69. <out:if test="$months!=''">
  70. <out:value-of select="concat($months,':months')"/>
  71. </out:if>
  72. <out:if test="$months=''">
  73. <out:value-of select="'0:months'"/>
  74. </out:if>
  75. </out:when>
  76. <out:when test="contains($monthDay,'D')">
  77. <out:variable name="days">
  78. <out:value-of select="substring-before($monthDay,'D')"/>
  79. </out:variable>
  80. <out:if test="$days!=''">
  81. <out:value-of select="concat($days,':days')"/>
  82. </out:if>
  83. <out:if test="$days=''">
  84. <out:value-of select="'0:days'"/>
  85. </out:if>
  86. </out:when>
  87. <out:otherwise>0:months</out:otherwise>
  88. </out:choose>
  89. </out:when>
  90. <out:when test="$hourMinuteSecond!=''">
  91. <out:choose>
  92. <out:when test="contains($hourMinuteSecond,'H')">
  93. <out:variable name="hours">
  94. <out:value-of select="substring-before($hourMinuteSecond,'H')"/>
  95. </out:variable>
  96. <out:if test="$hours!=''">
  97. <out:value-of select="concat($hours,':hours')"/>
  98. </out:if>
  99. <out:if test="$hours=''">
  100. <out:value-of select="'0:hours'"/>
  101. </out:if>
  102. </out:when>
  103. <out:when test="contains($hourMinuteSecond,'M')">
  104. <out:variable name="minutes">
  105. <out:value-of select="substring-before($hourMinuteSecond,'M')"/>
  106. </out:variable>
  107. <out:if test="$minutes!=''">
  108. <out:value-of select="concat($minutes,':minutes')"/>
  109. </out:if>
  110. <out:if test="$minutes=''">
  111. <out:value-of select="'0:minutes'"/>
  112. </out:if>
  113. </out:when>
  114. <out:when test="contains($hourMinuteSecond,'S')">
  115. <out:variable name="seconds">
  116. <out:value-of select="substring-before($hourMinuteSecond,'S')"/>
  117. </out:variable>
  118. <out:if test="$seconds!=''">
  119. <out:value-of select="concat($seconds,':seconds')"/>
  120. </out:if>
  121. <out:if test="$seconds=''">
  122. <out:value-of select="'0:seconds'"/>
  123. </out:if>
  124. </out:when>
  125. <out:otherwise>0:hours</out:otherwise>
  126. </out:choose>
  127. </out:when>
  128. <out:otherwise>0:months</out:otherwise>
  129. </out:choose>
  130. </out:variable>
  131. <out:value-of select="$parsedDuration"/>
  132. </out:template>
  133. <!--
  134. Build the duration value for the unit number and unit; this is the opposite of
  135. parseDurationValue
  136. xxx:months is converted into PxxxM
  137. -->
  138. <out:template name="buildDurationValue">
  139. <out:param name="durationUnitNbr"/>
  140. <out:param name="durationUnit"/>
  141. <out:choose>
  142. <out:when test="$durationUnit='months'">
  143. <out:value-of select="concat('P',$durationUnitNbr,'M')"/>
  144. </out:when>
  145. <out:when test="$durationUnit='days'">
  146. <out:value-of select="concat('P',$durationUnitNbr,'D')"/>
  147. </out:when>
  148. <out:when test="$durationUnit='hours'">
  149. <out:value-of select="concat('PT',$durationUnitNbr,'H')"/>
  150. </out:when>
  151. <out:when test="$durationUnit='minutes'">
  152. <out:value-of select="concat('PT',$durationUnitNbr,'M')"/>
  153. </out:when>
  154. <out:when test="$durationUnit='seconds'">
  155. <out:value-of select="concat('PT',$durationUnitNbr,'S')"/>
  156. </out:when>
  157. <out:otherwise>P0D</out:otherwise>
  158. </out:choose>
  159. </out:template>
  160. </xsl:template>
  161. <xsl:template match="*">
  162. <xsl:copy>
  163. <xsl:copy-of select="@*"/>
  164. <xsl:apply-templates/>
  165. </xsl:copy>
  166. </xsl:template>
  167. </xsl:stylesheet>