我有一个XML文件中的字段,其中包含日期值,格式如下:
``` ```
我想将其转换为标准的XSD格式:
``` 2013-04-01T17:13:41.000Z ```
请问在我的XSL转换中如何实现?我可以使用1.0和2.0样式表版本。
``` ```
我想将其转换为标准的XSD格式:
``` 2013-04-01T17:13:41.000Z ```
请问在我的XSL转换中如何实现?我可以使用1.0和2.0样式表版本。
所以...我感到无聊,之前从未使用过xsl:analyze-string。这里是一个基于正则表达式的解决方案:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:template match="/item/date">
<xsl:analyze-string select="@value" regex="([0-9]+)/([0-9]+)/([0-9]+) ([0-9]+):([0-9]+):([0-9]+) (PM|AM)">
<xsl:matching-substring>
<xsl:variable name="month" select="number(regex-group(1))"/>
<xsl:variable name="day" select="number(regex-group(2))"/>
<xsl:variable name="year" select="number(regex-group(3))"/>
<xsl:variable name="hours">
<xsl:choose>
<xsl:when test="regex-group(7) = 'PM'">
<xsl:value-of select="12 + number(regex-group(4))"/>
</xsl:when>
<xsl:otherwise><xsl:value-of select="number(regex-group(4))"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="minutes" select="number(regex-group(5))"/>
<xsl:variable name="seconds" select="number(regex-group(6))"/>
<xsl:variable name="dateTime" select="xs:dateTime( concat($year, '-', format-number($month, '00'), '-', format-number($day, '00'), 'T', format-number($hours, '00'), ':', format-number($minutes, '00'), ':', format-number($seconds, '00'), 'Z') )" />
<reformattedDate>
<xsl:value-of select="$dateTime"/>
</reformattedDate>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
我将其应用于像这样的测试 XML 文件:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<item>
<date value="4/1/2013 5:13:41 PM"/>
</item>
<?xml version="1.0" encoding="UTF-8"?>
<reformattedDate xmlns:xs="http://www.w3.org/2001/XMLSchema">2013-04-01T17:13:41Z</reformattedDate>
<xsl:variable name="dateTime"><xsl:value-of select="..."/></xsl:variable>
改为 <xsl:variable name="dateTime" select="..."/>
。 - Martin Honnen这里是使用substring-before
和substring-after
的XSLT 1.0版本。
感谢adhocgeek
提供XML输入。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/item">
<xsl:copy>
<xsl:apply-templates select="date"/>
</xsl:copy>
</xsl:template>
<xsl:template match="date">
<xsl:copy>
<xsl:variable name="date" select="substring-before(@value, ' ')"/>
<xsl:variable name="M" select="substring-before($date, '/')"/>
<xsl:variable name="D-Y" select="substring-after($date, '/')"/>
<xsl:variable name="D" select="substring-before($D-Y, '/')"/>
<xsl:variable name="Y" select="substring-after($D-Y, '/')"/>
<xsl:variable name="time-ampm" select="substring-after(@value, ' ')"/>
<xsl:variable name="time" select="substring-before($time-ampm, ' ')"/>
<xsl:variable name="ampm" select="substring-after($time-ampm, ' ')"/>
<xsl:variable name="h" select="substring-before($time, ':')"/>
<xsl:variable name="m-s" select="substring-after($time, ':')"/>
<xsl:variable name="m" select="substring-before($m-s, ':')"/>
<xsl:variable name="s" select="substring-after($m-s, ':')"/>
<xsl:variable name="hh">
<xsl:choose>
<xsl:when test="$ampm = 'PM'">
<xsl:value-of select="format-number($h + 12, '00')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number($h, '00')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="concat($Y, '-', $M, '-', $D, 'T', $hh, ':', $m, ':', $s)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<item>
<date value="4/1/2013 5:13:41 PM"/>
</item>
输出
<?xml version="1.0" encoding="utf-8"?>
<item>
<date>2013-4-1T05:13:41</date>
</item>
5:13:41 PM
需要被转换为 17:13:41
。否则输出将会比输入早12小时。 - michael.hor257k请看ESXLT。这些函数已经被许多XSL实现内置支持,如果你的实现不支持,它们还提供回退选项。
如果你的实现完全符合标准,你可以这样做:
<?xml ...
xmlns.date="http://exslt.org/dates-and-times"
...
<xsl:variable name="xsdate" select="date:parse-date(Date/@value, 'M/d/yyyy h:mm:ss a')" />
<xsl:value-of select="$xsdate" />
parse-date
。如果使用Xalan-Java,则可以使用我在http://stackoverflow.com/questions/22081400/using-non-core-exslt-date-functions-with-xalan-java#22157540中使用的技术来解析和重新格式化日期。最好在XML中首先使用`xs:dateTime`日期。 - Christopher Schultz<xsl:fallback>
元素旨在处理 元素,而不是 函数。对于函数,请使用 function-available()
函数。 - michael.hor257k<xsl:fallback>
。 - Christopher Schultzxs:dateTime
值,然后如果需要格式化,可以使用format-dateTime
:http://www.w3.org/TR/xslt20/#format-date。提取年、月、日等日期组件应该很简单,但对于时间,您需要更多的工作来注意PM
或AM
12小时制。使用substring-before和substring-after的代码可以工作,但应该添加更多的检查: 12AM => 00,12PM => 12之类的:
<xsl:variable name="hh">
<xsl:choose>
<xsl:when test="$ampm = 'AM' and $h='12'">00</xsl:when>
<xsl:when test="$ampm = 'PM' and $h!='12'">
<xsl:value-of select="format-number($h + 12, '00')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number($h, '00')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
@adhockgeek的答案对我几乎完美地起到了作用。 我只需要添加一个条件,以便它不会在值当前为12时将12加到小时数。 比如,12:15 PM!= 24:15 AM。
我从他的代码中创建了一个可在转换中重复调用的模板:
<xsl:template name="FormatDate">
<xsl:param name="dt"/>
<xsl:analyze-string select="$dt" regex="([0-9]+)/([0-9]+)/([0-9]+) ([0-9]+):([0-9]+):([0-9]+) (PM|AM)">
<xsl:matching-substring>
<xsl:variable name="month" select="number(regex-group(1))"/>
<xsl:variable name="day" select="number(regex-group(2))"/>
<xsl:variable name="year" select="number(regex-group(3))"/>
<xsl:variable name="hours">
<xsl:choose>
<xsl:when test="regex-group(7) = 'PM' and regex-group(4) != '12'">
<xsl:value-of select="12 + number(regex-group(4))"/>
</xsl:when>
<xsl:otherwise><xsl:value-of select="number(regex-group(4))"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="minutes" select="number(regex-group(5))"/>
<xsl:variable name="seconds" select="number(regex-group(6))"/>
<xsl:variable name="dateTime" select="xs:dateTime(concat($year, '-', format-number($month, '00'), '-', format-number($day, '00'), 'T', format-number($hours, '00'), ':', format-number($minutes, '00'), ':', format-number($seconds, '00')))" />
<xsl:value-of select="$dateTime"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>