code_junkie

XSLT transformation datetime to date format

Nov 13th, 2011
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. <articles>
  2. <article>
  3. <articleid>48992</articleid>
  4. <deliverydateasked>2009-01-29T00:00:00+01:00</deliverydateasked>
  5. </article>
  6. <article>
  7. <articleid>48993</articleid>
  8. <deliverydateasked>2009-01-30T00:00:00+01:00</deliverydateasked>
  9. </article>
  10. </articles>
  11.  
  12. <articles>
  13. <article>
  14. <articleid>48992</articleid>
  15. <deliverydateasked>2009-01-29</deliverydateasked>
  16. </article>
  17. <article>
  18. <articleid>48993</articleid>
  19. <deliverydateasked>2009-01-30</deliverydateasked>
  20. </article>
  21. </articles>
  22.  
  23. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  24. <xsl:template match="/">
  25. <articles>
  26. <xsl:apply-templates select="article">
  27. </xsl:apply-templates>
  28. </articles>
  29. </xsl:template>
  30.  
  31. <xsl:template name="FormatDate">
  32.  
  33. <xsl:param name="DateTime" />
  34. <xsl:variable name="date">
  35. <xsl:value-of select="substring-before($DateTime,'T')" />
  36. </xsl:variable>
  37.  
  38. <xsl:if test="string-length($date) != 10">
  39. <xsl:value-of select="$DateTime"/>
  40. </xsl:if>
  41. <xsl:if test="string-length($date) = 10">
  42. <xsl:value-of select="$date"/>
  43. </xsl:if>
  44. </xsl:template>
  45.  
  46. <xsl:template match="article">
  47. <xsl:call-template name="FormatDate">
  48. <xsl:with-param name="DateTime" select="deliverydateasked"/>
  49. </xsl:call-template>
  50. </xsl:template>
  51.  
  52. <articles />
  53.  
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  56.  
  57. <xsl:template match="*">
  58. <xsl:copy><xsl:apply-templates /></xsl:copy>
  59. </xsl:template>
  60.  
  61. <xsl:template match="deliverydateasked">
  62. <xsl:copy>
  63. <xsl:call-template name="FormatDate">
  64. <xsl:with-param name="DateTime" select="."/>
  65. </xsl:call-template>
  66. </xsl:copy>
  67. </xsl:template>
  68.  
  69. <xsl:template name="FormatDate">
  70.  
  71. <xsl:param name="DateTime" />
  72. <xsl:variable name="date">
  73. <xsl:value-of select="substring-before($DateTime,'T')" />
  74. </xsl:variable>
  75.  
  76. <xsl:if test="string-length($date) != 10">
  77. <xsl:value-of select="$DateTime"/>
  78. </xsl:if>
  79. <xsl:if test="string-length($date) = 10">
  80. <xsl:value-of select="$date"/>
  81. </xsl:if>
  82. </xsl:template>
  83.  
  84. </xsl:stylesheet>
  85.  
  86. xmlns="http://www.w3.org/1999/xhtml"
  87. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  88. xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  89. xmlns:user="http://www.tempuri.org/User">
  90.  
  91. <msxsl:script implements-prefix="user" language="C#">
  92. <![CDATA[
  93. public string FormatCurrency(string amount)
  94. {
  95. return decimal.Parse(amount).ToString("C0");
  96. }
  97.  
  98. public string FormatDate(string dateValue)
  99. {
  100. return DateTime.Parse(dateValue).ToString("MM/dd/yyyy hh:mm");
  101. }
  102. ]]>
  103. </msxsl:script>
  104.  
  105. <xsl:value-of select="user:FormatDate(@transactionDate)"/>
  106. <xsl:value-of select="user:FormatCurrency(@amount)"/>
  107.  
  108. XslCompiledTransform.Load(reader, XsltSettings.TrustedXslt, null);
  109.  
  110. <xsl:template match="*">
  111. <xsl:param name="parentElm">
  112. <xsl:value-of select="name(..)" />
  113. </xsl:param>
  114. <xsl:choose>
  115. <xsl:when test="local-name() = 'deliverydateasked'">
  116. <xsl:element name="deliverydateasked">
  117. <xsl:call-template name="FormatDate">
  118. <xsl:with-param name="DateTime" select="."/>
  119. </xsl:call-template>
  120. </xsl:element>
  121. </xsl:when>
  122. <xsl:otherwise>
  123. <xsl:element name="{local-name()}">
  124. <xsl:copy-of select="@*" />
  125. <xsl:apply-templates select="@* | node()" />
  126. </xsl:element>
  127. </xsl:otherwise>
  128. </xsl:choose>
  129. </xsl:template>
  130.  
  131. <xsl:template name="FormatDate">
  132. <xsl:param name="DateTime" />
  133. <xsl:variable name="date">
  134. <xsl:value-of select="substring-before($DateTime,'T')" />
  135. </xsl:variable>
  136.  
  137. <xsl:if test="string-length($date) != 10">
  138. <xsl:value-of select="$DateTime"/>
  139. </xsl:if>
  140. <xsl:if test="string-length($date) = 10">
  141. <xsl:value-of select="$date"/>
  142. </xsl:if>
  143. </xsl:template>
Add Comment
Please, Sign In to add comment