如何将XLST日期从2020-12-30转换为20201230

时间:2020-11-05 13:43:49

标签: xml xslt

我得到一个XML文件,然后尝试将其转换。我完成的大多数领域。

我导入的代码是:

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

我的XLST是:

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

问题是格式输出不好。

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

1 个答案:

答案 0 :(得分:0)

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

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

但是您需要XSLT 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,'-','')"/>