XSLT转换日期时间到日期格式

时间:2009-01-28 14:21:02

标签: .net xslt xsd

我正在尝试将日期时间转换为日期格式yyyy-MM-dd,因为我使用的是xsd.exe工具,xs:date数据类型会自动更改为datetime数据类型,因为没有类型完全匹配xs:date类型的.NET Framework。

但我无法让它工作

<articles>
        <article>
          <articleid>48992</articleid>
          <deliverydateasked>2009-01-29T00:00:00+01:00</deliverydateasked>
        </article>
        <article>
          <articleid>48993</articleid>
          <deliverydateasked>2009-01-30T00:00:00+01:00</deliverydateasked>
        </article>
</articles>

尝试将xml转换为

<articles>
        <article>
          <articleid>48992</articleid>
          <deliverydateasked>2009-01-29</deliverydateasked>
        </article>
        <article>
          <articleid>48993</articleid>
          <deliverydateasked>2009-01-30</deliverydateasked>
        </article>
</articles>

目前我正在使用此XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <articles>
        <xsl:apply-templates select="article">
        </xsl:apply-templates>
            </articles>
</xsl:template>

<xsl:template name="FormatDate">

    <xsl:param name="DateTime" />
    <xsl:variable name="date">
        <xsl:value-of select="substring-before($DateTime,'T')" />
    </xsl:variable>

    <xsl:if test="string-length($date) != 10">
        <xsl:value-of select="$DateTime"/>
    </xsl:if>
    <xsl:if test="string-length($date) = 10">
        <xsl:value-of select="$date"/>
    </xsl:if>
</xsl:template>

<xsl:template match="article">
        <xsl:call-template name="FormatDate">
            <xsl:with-param name="DateTime" select="deliverydateasked"/>
        </xsl:call-template>    
</xsl:template>     

有没有人知道一个好的xslt转换。

提前致谢

我的代码的输出结果是

<articles /> 

3 个答案:

答案 0 :(得分:7)

坦率地说,这对我来说是正确的 - 有时一个简单的子串就足够了。

但是,如果你在.NET领域并且你真的需要额外的功能,那么.NET有XSLT Extension Objects


编辑:oic,你有一个基本的应用模板概念问题。试试这个(注意副本和根模板匹配):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="*">
    <xsl:copy><xsl:apply-templates /></xsl:copy>
</xsl:template>

<xsl:template match="deliverydateasked">
    <xsl:copy>
        <xsl:call-template name="FormatDate">
            <xsl:with-param name="DateTime" select="."/>
        </xsl:call-template>    
    </xsl:copy>
</xsl:template>

<xsl:template name="FormatDate">

        <xsl:param name="DateTime" />
        <xsl:variable name="date">
                <xsl:value-of select="substring-before($DateTime,'T')" />
        </xsl:variable>

        <xsl:if test="string-length($date) != 10">
                <xsl:value-of select="$DateTime"/>
        </xsl:if>
        <xsl:if test="string-length($date) = 10">
                <xsl:value-of select="$date"/>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

模板是一个难以学习的概念,你可能最好从更简单的for-each开始,和/或你似乎可以使用一些XSLT教程/书籍。

答案 1 :(得分:6)

XPath 2.0中的格式化将变得更加轻松,微软目前在过去的8年中一直拒绝支持。由于格式问题实际上只对.Net中的XSLT持久存在,我喜欢使用自定义功能,这是一个更清洁的功能。更容易:

带格式化功能的XSLT:

    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="http://www.tempuri.org/User">

  <msxsl:script implements-prefix="user" language="C#">
        <![CDATA[
          public string FormatCurrency(string amount)
          {
            return decimal.Parse(amount).ToString("C0");
          }

          public string FormatDate(string dateValue)
          {
            return DateTime.Parse(dateValue).ToString("MM/dd/yyyy hh:mm");
          }
          ]]>
      </msxsl:script>

用法:

<xsl:value-of select="user:FormatDate(@transactionDate)"/>
<xsl:value-of select="user:FormatCurrency(@amount)"/>

当您在.Net中执行XSLT时,请务必告诉它它是可信的(以便运行msxsl:script块。

XslCompiledTransform.Load(reader, XsltSettings.TrustedXslt, null);

答案 2 :(得分:1)

感谢Stesoc和annakata我明白了 这是我现在使用的代码,它完美无缺

<xsl:template match="*">
    <xsl:param name="parentElm">
        <xsl:value-of select="name(..)" />
    </xsl:param>
    <xsl:choose>
        <xsl:when test="local-name() = 'deliverydateasked'">
            <xsl:element name="deliverydateasked">
                <xsl:call-template name="FormatDate">
                    <xsl:with-param name="DateTime" select="."/>
                </xsl:call-template>
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <xsl:element name="{local-name()}">
                <xsl:copy-of select="@*" />
                <xsl:apply-templates select="@* | node()" />
            </xsl:element>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="FormatDate">
    <xsl:param name="DateTime" />
    <xsl:variable name="date">
        <xsl:value-of select="substring-before($DateTime,'T')" />
    </xsl:variable>

    <xsl:if test="string-length($date) != 10">
        <xsl:value-of select="$DateTime"/>
    </xsl:if>
    <xsl:if test="string-length($date) = 10">
        <xsl:value-of select="$date"/>
    </xsl:if>
</xsl:template>