首页 > 解决方案 > 我如何将 XLST 日期从 2020-12-30 转换为 20201230

问题描述

我得到一个 XML 文件并尝试共同转换它。我做过的大部分领域。

我得到导入的代码是:

<DeliveryDate>2020-11-03</DeliveryDate>

我的 XLST 是:

  <require><xsl:value-of select="PurchaseOrderLine/DeliveryDate"/></require>

问题是格式输出不好。

输入是2020-12-31,我需要输出20201231

标签: xmlxslt

解决方案


一种快速的方法是使用替换功能

<xsl:value-of select="replace(PurchaseOrderLine/DeliveryDate,'-','')"/>

但是你需要 XSLT 2.0 才能工作 (<xsl:stylesheet version="2.0"...)

对于 XLST 1.0,您需要创建一个模板:

<xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
        <xsl:when test="$text = '' or $replace = ''or not($replace)" >
            <!-- Prevent this routine from hanging -->
            <xsl:value-of select="$text" />
        </xsl:when>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)" />
            <xsl:value-of select="$by" />
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="substring-after($text,$replace)" />
                <xsl:with-param name="replace" select="$replace" />
                <xsl:with-param name="by" select="$by" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

并这样称呼它:

<xsl:variable name="newtext">
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="$text" />
        <xsl:with-param name="replace" select="a" />
        <xsl:with-param name="by" select="b" />
    </xsl:call-template>
</xsl:variable>

或尝试翻译:

<xsl:variable name="newtext" select="translate(PurchaseOrderLine/DeliveryDate,'-','')"/>

推荐阅读