【问题标题】:My XML output with date Format using XSL我使用 XSL 的日期格式的 XML 输出
【发布时间】:2018-02-26 07:16:05
【问题描述】:

我有我的 xml 文件

   <root>
         <item>Apple</item>
         <name><![CDATA[Indhu && Mathi]]></name>
         <date>28-02-2018</date>
         <dollar>500</dollar>
   </root>

将我的 XSL 文件设置为:

      <xsl:import href="lookupValues.xsl"/>
       <xsl:template match="/">
         <html>
           <head></head>
           <body>
           <h1>Hello World</h1>
           <table border="1">
           <tr><td><xsl:value-of select="root/item"/></td> </tr>
           <tr><td><xsl:value-of select="root/name"/></td> </tr>
           <tr><td><xsl:value-of select="root/date"/></td></tr>


          <tr>
           <td> Date Format </td>
            <td><xsl:call-template name="format_date">
                <xsl:with-param name="arg" select="root/date" />
          </xsl:call-template> </td>
         </tr>       
           </table>
         </body>
         </html>
          </xsl:template>

调用函数也写成 lookupValues.xsl:

     <xsl:template name="format_date">
    <xsl:param name="arg"/>

    <xsl:variable name="vDay" select="substring-before($arg, '/')"/>        
    <xsl:variable name="vMonth" select="substring-before(substring-after($arg, '/'), '/')"/>        
    <xsl:variable name="vYear" select="substring-before(substring-after(substring-after($arg, '/'), '/'), ' ')"/>

    <xsl:if test="string-length($vDay) &lt; 3">
        <xsl:text>0</xsl:text>
    </xsl:if>
    <xsl:value-of select="$vDay"/>      
    <xsl:text>-</xsl:text>

    <xsl:if test="string-length($vMonth) &lt; 4">
        <xsl:text>0</xsl:text>
    </xsl:if>
    <xsl:value-of select="$vMonth"/>
    <xsl:text>-</xsl:text>

    <xsl:value-of select="$vYear"/>     
</xsl:template> 

我想以 DD/MMM/YYYY 格式将我的日期打印为 .需要在我的代码中进行一些更正。不清楚功能。

【问题讨论】:

  • 您使用的是哪个版本的 XSLT? 1.02.0?
  • 我使用的是 1.0 版

标签: xml xslt


【解决方案1】:

XSLT 1.0 不提供任何用于日期格式化的内置函数。有 date time extensions 可以稍微减轻处理日期的任务,但并非所有扩展都符合我们的要求。

您必须编写自定义模板来处理数字月份到月份名称的转换,即 02Feb。下面是将日期从DD-MM-YYYY 格式转换为DD/MMM/YYYY 格式的样式表。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

    <xsl:template match="root">
        <formattedDate>
            <xsl:call-template name="format-date">
                <xsl:with-param name="date" select="date" />
            </xsl:call-template>
        </formattedDate>
    </xsl:template>

    <xsl:template name="format-date">
        <xsl:param name="date" />
        <xsl:variable name="day" select="substring($date, 1, 2)" />
        <xsl:variable name="month" select="substring($date, 4, 2)" />
        <xsl:variable name="year" select="substring($date, 7, 4)" />
        <xsl:variable name="monthName">
            <xsl:call-template name="month-name">
                <xsl:with-param name="month" select="$month" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="concat($day, '/', $monthName, '/', $year)" />
    </xsl:template>

    <xsl:template name="month-name">
        <xsl:param name="month" />
        <xsl:choose>
            <xsl:when test="$month = 1">Jan</xsl:when>
            <xsl:when test="$month = 2">Feb</xsl:when>
            <xsl:when test="$month = 3">Mar</xsl:when>
            <xsl:when test="$month = 4">Apr</xsl:when>
            <xsl:when test="$month = 5">May</xsl:when>
            <xsl:when test="$month = 6">Jun</xsl:when>
            <xsl:when test="$month = 7">Jul</xsl:when>
            <xsl:when test="$month = 8">Aug</xsl:when>
            <xsl:when test="$month = 9">Sep</xsl:when>
            <xsl:when test="$month = 10">Oct</xsl:when>
            <xsl:when test="$month = 11">Nov</xsl:when>
            <xsl:when test="$month = 12">Dec</xsl:when>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

  • 而不是如果我们可以在选择此选项时选择“选择/何时”,我们可以获得两件事:1)性能会很快,2)如果会出现一些新值,那么我们可以处理否则部分。
【解决方案2】:

如果您的日期输入格式是永久性的,并且看起来像 28-02-2018 (dd-mm-yyyy),那么可以简化从参数中捕获日期:

<xsl:template name="format_date">
    <xsl:param name="arg"/>
    <xsl:variable name="day">
        <xsl:value-of select="substring($arg,1,2)"/>
    </xsl:variable>
    <xsl:variable name="month">
        <xsl:value-of select="substring($arg,4,2)"/>
    </xsl:variable>
    <xsl:variable name="year">
        <xsl:value-of select="substring($arg,7,4)"/>
    </xsl:variable>
    <xsl:variable name="monthName">
        <xsl:call-template name="month-name">
            <xsl:with-param name="month" select="$month" />
        </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="concat($day, '/', $monthName, '/', $year)" />
</xsl:template>

【讨论】:

  • 是的!我得到了它。性能方面更好。非常感谢
  • 是的。我忽略了这方面。更新了答案以包含此更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-20
  • 2021-11-29
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
相关资源
最近更新 更多