在 XSLT/XML 中将日期显示为 DD-MM-YYYY
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19759036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying Date as DD-MM-YYYY within XSLT/XML
提问by magi4000
Been trying to format the text to display as DD-MM-YYYY when pulled from XML to XSLT 1.0, since I know it has to be laid out as YYYY-MM-DD within XSD/XML when using xs:date which what I have used.
当从 XML 拉到 XSLT 1.0 时,一直试图格式化文本以显示为 DD-MM-YYYY,因为我知道在使用 xs:date 时,它必须在 XSD/XML 中布局为 YYYY-MM-DD用过的。
Here is the code I am working working on, Any advice on how I should display this?
这是我正在处理的代码,有关如何显示此代码的任何建议?
XML
XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<events xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="events.xsd">
<venue id="01"
vtitle="ExCeL Exhibition Centre"
location="London"
telephone="0844 448 7600">
<event name="Doctor Who 50th Celebration" date="2013-10-22">
<image>images/doctor50.jpg</image><attribute>Doctor Who Event</attribute>
<description>A 50th Anniversary musical bonanza for Doctor Who.</description>
<ticket_price type="adult" status="available">£25.00</ticket_price>
<ticket_price type="child" status="none">£11.00</ticket_price>
<ticket_price type="junior" status="available">£0.00</ticket_price>
<email>[email protected]</email>
</event>
</venue>
XSD
XSD
<xs:attribute name="date" type="xs:date"/>
XSLT
XSLT
<xsl:element name="date"><xsl:value-of select="@date"/></xsl:element>
HTML
HTML
<date>2013-10-22</date>
回答by C. M. Sperberg-McQueen
Another simple solution for producing dates in dd/mm/yyyy form
以 dd/mm/yyyy 形式生成日期的另一种简单解决方案
<xsl:value-of select="
concat(substring(@date, 9, 2),
'/',
substring(@date, 6, 2),
'/',
substring(@date, 1, 4))"/>
回答by Michael Kay
I would use different solutions from those provided by @Stormtroopr for both 1.0 and 2.0.
我将使用与@Stormtroopr 为 1.0 和 2.0 提供的解决方案不同的解决方案。
In 2.0 use
在 2.0 中使用
format-date(xs:date($date), '[D01]-[M01]-[Y0001]')
In 1.0 use
在 1.0 中使用
concat(substring(., 9, 2),
'-',
substring(., 6, 2),
'-',
substring(., 1, 4))
For future reference, please tell us which version of XSLT you are using. Both 1.0 and 2.0 are in common use, and the solutions are often different.
为了将来参考,请告诉我们您使用的是哪个版本的 XSLT。1.0 和 2.0 都是通用的,解决方法也经常不同。
回答by user3567457
I am using it for SharePoint and I solved it like this
我将它用于 SharePoint 并像这样解决了它
<xsl:value-of select=" ddwrt:FormatDate(/dsQueryResponse/Rows/Row/@Date,3,1)"/>
回答by user3567457
In XSLT 2.0 you can use tokenize to split the string and then just using xsl:for-eachand xsl:sortto reverse it. (I don't have an XSLT2.0 engine right now, but this is pretty close to what you'll need).
在 XSLT 2.0 中,您可以使用 tokenize 来拆分字符串,然后只使用xsl:for-each和xsl:sort反转它。(我现在没有 XSLT2.0 引擎,但这与您需要的非常接近)。
<xsl:for-each select="tokenize(@date,'-'">
<xsl:sort select="position()" data-type="number" order="descending"/>
<xsl:value-of select="."/>
</xsl:for-each>
In XSLT 1.0, you do it using... recursion!
在 XSLT 1.0 中,您使用...递归!
Here is the crux of it, this takes a date, and then searches for the string before and after the first hyphen (-). Normally, you'd have the substring-beforecome after the processing of the subtring-afterto preserve the order, but here we switch them to ultimately reverse the ouput.
这是它的关键,这需要一个日期,然后搜索第一个连字符 ( -)前后的字符串。通常,您会substring-before在处理 之后使用comesubtring-after来保留顺序,但在这里我们切换它们以最终反转输出。
<xsl:template name="reverse-date">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="contains($text, '-')">
<xsl:call-template name="reverse-date">
<xsl:with-param name="text" select="substring-after($text, '-')"/>
</xsl:call-template>
<xsl:text>-</xsl:text>
<xsl:value-of select="substring-before($text, '-')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Below is the complete template that will reverse the date for you based on your above XML document.
下面是完整的模板,它将根据您的上述 XML 文档为您反转日期。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="//event"/>
</xsl:template>
<xsl:template match="event">
<xsl:element name="date">
<!--
** Call this template when you want to reverse the date **
-->
<xsl:call-template name="reverse-date">
<xsl:with-param name="text" select="@date" />
</xsl:call-template>
</xsl:element>
</xsl:template>
<xsl:template name="reverse-date">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="contains($text, '-')">
<xsl:value-of select="substring-before($text, '-')" />
<xsl:text>-</xsl:text>
<xsl:call-template name="reverse-date">
<xsl:with-param name="text" select="substring-after($text, '-')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space($text)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
In your code
在你的代码中
You'll need to change this:
你需要改变这个:
<xsl:element name="date"><xsl:value-of select="@date"/></xsl:element>
To this:
对此:
<xsl:element name="date">
<xsl:call-template name="reverse-date">
<xsl:with-param name="text" select="@date" />
</xsl:call-template>
</xsl:element>

