gpt4 book ai didi

xml - 使用 XSL 更改属性值

转载 作者:行者123 更新时间:2023-12-02 10:53:16 26 4
gpt4 key购买 nike

我有一个要求,需要使用 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>

最佳答案

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

<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

关于xml - 使用 XSL 更改属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47926989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com