【问题标题】:Change an attribute value using XSL使用 XSL 更改属性值
【发布时间】:2017-12-21 14:26:38
【问题描述】:

我有一个要求,需要使用 XSL 将日期格式从 yyyy-mm-dd 更改为 dd-mmm-yyyy

我已经设法获取日期的值并编写了更改它的逻辑。但不知何故,价值没有改变。

请求和 XSL 也可以在这里找到:Change the Date format

XML 输入

<params>
<param name ="query" >
    <queryData>
        <parameter index ="0" value ="2017-12-06" dataType="java.lang.String"/>
        <parameter index ="1" value ="2017-12-03" dataType="java.lang.String"/>    
     </queryData>
</param>
</params>

XSLT 1.0

<xsl:stylesheet version="1.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*" />
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="params/param/queryData/parameter[@value='*'][normalize-space()]">
        <xsl:copy>
            <xsl:call-template name="reformat-date">
                <xsl:with-param name="date" select="." />
            </xsl:call-template>
        </xsl:copy>
    </xsl:template>

    <xsl:template name="reformat-date">
        <xsl:param name="date" />
        <xsl:variable name="dd" select="substring-after(substring-after($date, '-'), '-')" />
        <xsl:variable name="mmm" select="substring-before(substring-after($date, '-'),     '-')" />
        <xsl:variable name="yyyy" select="substring-before($date, '-')" />
        <xsl:value-of select="$dd" />
        <xsl:text>-</xsl:text>
        <xsl:choose>
            <xsl:when test="$mmm = '01'">JAN</xsl:when>
            <xsl:when test="$mmm = '02'">FEB</xsl:when>
            <xsl:when test="$mmm = '03'">MAR</xsl:when>
            <xsl:when test="$mmm = '04'">APR</xsl:when>
            <xsl:when test="$mmm = '05'">MAY</xsl:when>
            <xsl:when test="$mmm = '06'">JUN</xsl:when>
            <xsl:when test="$mmm = '07'">JUL</xsl:when>
            <xsl:when test="$mmm = '08'">AUG</xsl:when>
            <xsl:when test="$mmm = '09'">SEP</xsl:when>
            <xsl:when test="$mmm = '10'">OCT</xsl:when>
            <xsl:when test="$mmm = '11'">NOV</xsl:when>
            <xsl:when test="$mmm = '12'">DEC</xsl:when>
        </xsl:choose>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="$yyyy" />
    </xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • 请发布代表您的问题的代码(XML 和 XSLT),不要提供指向外部网站的链接。

标签: xml xslt xpath xslt-1.0


【解决方案1】:

如果你想改变属性,那么你需要调整匹配模式来匹配属性,当然你需要创建一个属性,所以你需要

<xsl:template match="params/param/queryData/parameter/@value">
    <xsl:attribute name="{name()}">
        <xsl:call-template name="reformat-date">
            <xsl:with-param name="date" select="." />
        </xsl:call-template>
    </xsl:attribute>
</xsl:template>

而不是

<xsl:template match="params/param/queryData/parameter[@value='*'][normalize-space()]">
    <xsl:copy>
        <xsl:call-template name="reformat-date">
            <xsl:with-param name="date" select="." />
        </xsl:call-template>
    </xsl:copy>
</xsl:template>

http://xsltransform.net/bEJaofn/1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    相关资源
    最近更新 更多