我正在尝试将日期时间转换为日期格式yyyy-MM-dd,因为我使用的是xsd.exe工具,xs:date数据类型会自动更改为datetime数据类型,因为.NET中没有类型完全匹配类型xs:date的框架.
但我无法让它发挥作用
<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>
Run Code Online (Sandbox Code Playgroud)
试图将xml转换为
<articles>
<article>
<articleid>48992</articleid>
<deliverydateasked>2009-01-29</deliverydateasked>
</article>
<article>
<articleid>48993</articleid>
<deliverydateasked>2009-01-30</deliverydateasked>
</article>
</articles>
Run Code Online (Sandbox Code Playgroud)
目前我正在使用这个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>
Run Code Online (Sandbox Code Playgroud)
有谁知道一个很好的xslt转换.
提前致谢
我的代码的输出结果是
<articles />
Run Code Online (Sandbox Code Playgroud)
坦率地说,这对我来说是正确的 - 有时一个简单的子串就足够了.
但是,如果你在.NET领域并且你真的需要额外的功能,那么.NET有XSLT扩展对象
编辑: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>
Run Code Online (Sandbox Code Playgroud)
模板是一个难以学习的概念,你可能最好从更简单的开始for-each
,和/或你似乎可以用一些XSLT教程/书籍.
在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>
Run Code Online (Sandbox Code Playgroud)
用法:
<xsl:value-of select="user:FormatDate(@transactionDate)"/>
<xsl:value-of select="user:FormatCurrency(@amount)"/>
Run Code Online (Sandbox Code Playgroud)
当您在.Net中执行XSLT时,请务必告诉它它是可信的(以便运行msxsl:script块).
XslCompiledTransform.Load(reader, XsltSettings.TrustedXslt, null);
Run Code Online (Sandbox Code Playgroud)
感谢 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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28917 次 |
最近记录: |