Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何在xslt中将2009-09-18转换为9月18日_Xml_Xslt - Fatal编程技术网

Xml 如何在xslt中将2009-09-18转换为9月18日

Xml 如何在xslt中将2009-09-18转换为9月18日,xml,xslt,Xml,Xslt,想知道如何在xslt中将2009-09-18转换为9月18日吗? 谢谢。您的后端使用什么语言 如果是PHP,则可以使用date函数轻松格式化DATETIME字符串: 您可以在将日期转换为XML格式之前对其进行格式化。在这里使用带有大字符的命名模板听起来是一个合适的解决方案。棘手的部分是月名,其余的都很琐碎,对吧?当然,您不必将其与命名模板分离。EXSLT有一些用于转换日期字符串的扩展函数,请参阅: 幸运的是,您的XSLT处理器本机支持这些功能,否则大多数日期函数都有一个简单的XSLT 1.0实

想知道如何在xslt中将2009-09-18转换为9月18日吗?
谢谢。

您的后端使用什么语言

如果是PHP,则可以使用date函数轻松格式化DATETIME字符串:


您可以在将日期转换为XML格式之前对其进行格式化。

在这里使用带有大字符的命名模板听起来是一个合适的解决方案。棘手的部分是月名,其余的都很琐碎,对吧?当然,您不必将其与命名模板分离。

EXSLT有一些用于转换日期字符串的扩展函数,请参阅:


幸运的是,您的XSLT处理器本机支持这些功能,否则大多数日期函数都有一个简单的XSLT 1.0实现,您可以包括一个纯XSLT 1.0解决方案,它假定输入有效:

<xsl:template match="/">
    <newdate>
        <xsl:call-template name="convertdate">
            <xsl:with-param name="date" select="date"/>
        </xsl:call-template>
    </newdate>
</xsl:template>

<xsl:template name="convertdate">   
    <xsl:param name="date"/>

    <xsl:variable name="day">
        <xsl:value-of select="number(substring-after(substring-after($date,'-'), '-'))"/>
    </xsl:variable>

    <xsl:variable name="suffix">
        <xsl:choose>
            <xsl:when test="$day = '1'">st</xsl:when>
            <xsl:when test="substring($day, string-length($day), 1) = '1' and not(starts-with($day, '1'))">st</xsl:when>
            <xsl:when test="substring($day, string-length($day), 1) = '2' and not(starts-with($day, '1'))">nd</xsl:when>
            <xsl:when test="substring($day, string-length($day), 1) = '3' and not(starts-with($day, '1'))">rd</xsl:when>
            <xsl:otherwise>th</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="mo">
        <xsl:value-of select="number(substring-before(substring-after($date,'-'), '-'))"/>
    </xsl:variable>     

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

    <xsl:value-of select="$day"/><xsl:value-of select="$suffix"/>&#160;<xsl:value-of select="$month"/>

</xsl:template>
大致如下:

<xsl:template name="friendly-date">
  <xsl:param name="datestring" select="'yyyy-mm-dd'" />

  <xsl:variable name="y" select="number(substring-before($datestring, '-'))" />
  <xsl:variable name="r" select="number(substring-after($datestring, '-'))" />
  <xsl:variable name="m" select="number(substring-before($r, '-'))" />
  <xsl:variable name="d" select="number(substring-after($r, '-'))" />

  <xsl:choose>
    <xsl:when test="
      $y &gt;= 1970 and $y &lt;= 9999
      and
      $m &gt;= 1 and $m &lt;= 12
      and
      $d &gt;= 1 and $m &lt;= 31
    ">
      <xsl:value-of select="$d" />
      <xsl:text> </xsl:text>
      <xsl:choose>
        <xsl:when test="$m = 1">
          <xsl:value-of select="'Jan'" />
        </xsl:when>
        <!-- ... insert missing ... -->
        <xsl:when test="$m = 12">
          <xsl:value-of select="'Dec'" />
        </xsl:when>
      </xsl:choose>
      <xsl:text> </xsl:text>
      <xsl:value-of select="$y" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$datestring" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
注意,这不会检查不可能的日期。这取决于你的投入是否会成为问题


除此之外,它不提供国际日期格式。对于不太特别的解决方案,我建议像其他解决方案一样查看EXSLT。

是的,这是正确的,也是最简单/最好的解决方案。但是如果我必须通过xsltNice,+1来处理它呢:使用不间断的空格实际上也是一个好主意。