【问题标题】:XSLT convert date formatXSLT 转换日期格式
【发布时间】:2012-04-30 14:41:32
【问题描述】:

我在获取 XML 文件时遇到问题,从样本中我得到的日期格式是 mm/dd/yyyy,有时是 m/d/yyyy。我的任务是将其转换为另一个 XML 文件,其中架构只接受 yyyy-mm-dd。我仅限于使用 XSLT 1.0/XPATH 1.0。我该怎么做?

【问题讨论】:

  • 那么您如何解释 '11/12/2012' ?您需要通过关于两种格式中的哪一种适用于哪种情况的规则来澄清您的问题。
  • 源架构为 2012 年 11 月 12 日,目标架构需要将其设为 2012 年 11 月 12 日(解释为 2012 年 11 月 12 日)。

标签: xml xslt xpath biztalk xslt-1.0


【解决方案1】:

你的问题有点含糊。它需要一些样本输入和预期输出。但无论如何,这里有一个答案,可以最好地猜测你想要什么。

鉴于此输入:

<?xml version="1.0"?>
<dates>
  <date>11/12/2012</date>
  <date>3/4/2011</date>
</dates>

...由这个样式表转换...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

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

      <xsl:template match="date">
        <xsl:copy>
        <xsl:value-of select=" 
           concat(
           substring-after(substring-after(.,'/'),'/') , '-',
           format-number( number( substring-before(.,'/')), '00') , '-',
           format-number( substring-before(substring-after(.,'/'),'/'), '00') 
           )
          " />
        </xsl:copy>
      </xsl:template>

</xsl:stylesheet>

... 将产生所需的输出...

<dates>
 <date>2012-11-12</date>
 <date>2011-03-04</date>
</dates>

如果正确,请勾选答案。我在http://www.purplegene.com/static/transform.html验证了这个解决方案

【讨论】:

  • Dogbane 的解决方案与我的非常相似,但他击败了我。不过,我认为我的解决方案更简洁,读起来更好。 McLaren 的解决方案不适用于字符串长度为 9 的日期,例如 2012 年 1 月 12 日。
【解决方案2】:

下面显示的我的 XSLT 应该可以工作。它不使用硬编码的长度或索引,而是将日期字符串拆分为 '/' 以计算出日、月和年组件的位置。

XSLT:

<?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" />

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

        <!-- match the date and invoke formatDate to format it -->
    <xsl:template match="date">
        <xsl:element name="date">
            <xsl:call-template name="formatDate">
                <xsl:with-param name="dateParam" select="." />
            </xsl:call-template>
        </xsl:element>
    </xsl:template>

    <xsl:template name="formatDate">
        <xsl:param name="dateParam" />
        <!-- input format mm/dd/yyyy or m/d/yyyy -->
        <!-- output format yyyy-mm-dd -->


        <!-- parse out the day, month and year -->
        <xsl:variable name="month" select="substring-before($dateParam,'/')" />
        <xsl:variable name="day" select="substring-before(substring-after($dateParam,'/'),'/')" />
        <xsl:variable name="year" select="substring-after(substring-after($dateParam,'/'),'/')" />

        <!-- now print them out. Pad with 0 where necessary. -->
        <xsl:value-of select="$year" />
        <xsl:value-of select="'-'" />
        <xsl:if test="string-length($month) = 1">
            <xsl:value-of select="'0'" />
        </xsl:if>
        <xsl:value-of select="$month" />
        <xsl:value-of select="'-'" />
        <xsl:if test="string-length($day) = 1">
            <xsl:value-of select="'0'" />
        </xsl:if>
        <xsl:value-of select="$day" />
    </xsl:template>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

输入:

<input>
    <date>04/30/2012</date>
    <date>4/1/2012</date>
</input>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<input>
    <date>2012-04-30</date>
    <date>2012-04-01</date>
</input>

演示http://www.xsltcake.com/slices/kBveVQ

【讨论】:

  • 当你可以使用&lt;xsl:variable select="xxx"/&gt;时,请不要使用&lt;xsl:variable&gt;&lt;xsl:value-of select="xxx"/&gt;&lt;/xsl:variable&gt;。既冗长又低效!
  • 您好团队!见下文,暴露格式日期时间:stackoverflow.com/a/16892467/2816548
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 2017-12-23
相关资源
最近更新 更多