Convert date format in xslt from YYYYMMDD to MM/DD/YYYY

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 04:17:48

问题


I am Having date in xml file in format like

YYYYMMDD

Applying the xslt transformation i want to change the format to

MM/DD/YYYY

.

For example, Incoming format - 20160513 Output format - 05/13/2016


回答1:


Given:

<date>20160513</date>

the following:

<xsl:template match="date">
    <xsl:copy>
        <xsl:value-of select="substring(., 5, 2)"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="substring(., 7, 2)"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="substring(., 1, 4)"/>
    </xsl:copy>
</xsl:template>

will return:

<date>05/13/2016</date>



回答2:


XSLT 2.0 option...

<xsl:template match="date[matches(normalize-space(),'^\d{8}$')]">
  <xsl:copy>
    <xsl:value-of select="replace(normalize-space(),
      '(\d{4})(\d{2})(\d{2})',
      '$2/$3/$1')"/>
  </xsl:copy>
</xsl:template>


来源:https://stackoverflow.com/questions/40036624/convert-date-format-in-xslt-from-yyyymmdd-to-mm-dd-yyyy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!