我正在运行此代码,其中我根据需要将日期时间格式化为"@datetime"。 此代码完美运行。
<?xml version ='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match ="transcript/call">
<xsl:variable name="datestr" select="substring-before(@datetime,' UTC')" />
<xsl:variable name="MMM" select="format-number(string-length(substring-before('JanFebMarAprMayJunJulAugSepOctNovDec',substring(substring($datestr,1,3),1,3))) div 3 + 1,'00')"/>
<xsl:variable name="D" select="format-number(floor(substring($datestr,5,1)),'00')" />
<xsl:variable name="YYYY" select="substring($datestr,8,4)" />
<xsl:variable name="hh" select="substring($datestr,13,2)" />
<xsl:variable name="mm" select="substring($datestr,16,2)" />
<xsl:variable name="ss" select="substring($datestr,19,2)" />
<!-- <xsl:variable name="DateTimeFormatted" select="concat($YYYY,'-', $MMM, '-', $D, 'T', $hh, ':', $mm, ':', $ss, 'Z')" /> -->
Your chat transcript from Univ100 @ Student dated <xsl:value-of select="concat($YYYY,'-', $MMM, '-', $D, 'T', $hh, ':', $mm, ':', $ss, 'Z')" />
-------------------------------------------------------------------------------------
</xsl:template>
<xsl:template match ="transcript/say">
<xsl:if test ="./@source ='customer'">
[<xsl:value-of select ="@datetime" />] <xsl:value-of select ="@name" /> says: <xsl:value-of select ="." disable-output-escaping="yes" /></xsl:if>
<xsl:if test ="./@source ='agent'">
[<xsl:value-of select ="@datetime" />] Student Officer says: <xsl:value-of select ="." disable-output-escaping="yes" /></xsl:if>
<xsl:if test ="./@source ='system'">
<xsl:if test ="./@display ='true'">
[<xsl:value-of select ="@datetime" />] System: <xsl:value-of select ="." disable-output-escaping="yes" />
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template match ="transcript/url">
<xsl:if test ="./@source ='customer'">
[<xsl:value-of select ="@datetime" />] <xsl:value-of select ="@name" /> sends: <xsl:value-of select ="." disable-output-escaping="yes" />
</xsl:if>
<xsl:if test ="./@source ='agent'">
[<xsl:value-of select ="@datetime" />] Student Officer sends: <xsl:value-of select ="." disable-output-escaping="yes" />
</xsl:if>
<xsl:if test ="./@source ='system'">
<xsl:if test ="./@display ='true'">
[<xsl:value-of select ="@datetime" />] System: <xsl:value-of select ="." disable-output-escaping="yes" />
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template match ="transcript/event">
</xsl:template>
<xsl:template match ="parameters">
Univ100
</xsl:template>
</xsl:stylesheet>
但是,当我进行以下更改时,以便为格式设置制作不同的模板,该模板接受参数并返回格式化的字符串,因此我可以在此聊天记录中在需要时多次使用它。
我进行了以下更改,但代码不起作用。
<?xml version ='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template name="formatter">
<xsl:param name="datestr"/>
<!-- <xsl:variable name="datestr" select="substring-before(@datetime,' UTC')" /> -->
<xsl:variable name="MMM" select="format-number(string-length(substring-before('JanFebMarAprMayJunJulAugSepOctNovDec',substring(substring($datestr,1,3),1,3))) div 3 + 1,'00')"/>
<xsl:variable name="D" select="format-number(floor(substring($datestr,5,1)),'00')" />
<xsl:variable name="YYYY" select="substring($datestr,8,4)" />
<xsl:variable name="hh" select="substring($datestr,13,2)" />
<xsl:variable name="mm" select="substring($datestr,16,2)" />
<xsl:variable name="ss" select="substring($datestr,19,2)" />
<!-- <xsl:variable name="DateTimeFormatted" select="concat($YYYY,'-', $MMM, '-', $D, 'T', $hh, ':', $mm, ':', $ss, 'Z')" /> -->
<xsl:value-of select="concat($YYYY,'-', $MMM, '-', $D, 'T', $hh, ':', $mm, ':', $ss, 'Z')" />
</xsl:template>
<xsl:template match ="transcript/call">
<xsl:variable name="returnValue">
<xsl:call-template name="formatter">
<xsl:with-param name="datestr" select="@datetime"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
Your chat transcript from Univ100 @ Student dated <xsl:value-of select="$returnValue"/>
-------------------------------------------------------------------------------------
</xsl:template>
<xsl:template match ="transcript/say">
<xsl:if test ="./@source ='customer'">
[<xsl:value-of select ="@datetime" />] <xsl:value-of select ="@name" /> says: <xsl:value-of select ="." disable-output-escaping="yes" /></xsl:if>
<xsl:if test ="./@source ='agent'">
[<xsl:value-of select ="@datetime" />] Student Officer says: <xsl:value-of select ="." disable-output-escaping="yes" /></xsl:if>
<xsl:if test ="./@source ='system'">
<xsl:if test ="./@display ='true'">
[<xsl:value-of select ="@datetime" />] System: <xsl:value-of select ="." disable-output-escaping="yes" />
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template match ="transcript/url">
<xsl:if test ="./@source ='customer'">
[<xsl:value-of select ="@datetime" />] <xsl:value-of select ="@name" /> sends: <xsl:value-of select ="." disable-output-escaping="yes" />
</xsl:if>
<xsl:if test ="./@source ='agent'">
[<xsl:value-of select ="@datetime" />] Student Officer sends: <xsl:value-of select ="." disable-output-escaping="yes" />
</xsl:if>
<xsl:if test ="./@source ='system'">
<xsl:if test ="./@display ='true'">
[<xsl:value-of select ="@datetime" />] System: <xsl:value-of select ="." disable-output-escaping="yes" />
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template match ="transcript/event">
</xsl:template>
<xsl:template match ="parameters">
Univ100
</xsl:template>
</xsl:stylesheet>
我对 XSLT 1.0非常陌生,我必须为此工作,因为我没有其他选择来更改或升级版本,我只能在纯 XSLT 1.0 上工作。这是我对项目的唯一控制权,我无法访问数据库服务器、使用此 XLST 的 XML 文件或项目的任何其他方面。
任何帮助将不胜感激。
<xsl:value-of select="@datetime"/>
不会调用您的模板。重写您的"格式化程序"模板以匹配@datetime
然后使用xsl:apply-templates
调用它:
<xsl:template match="@datetime">
<xsl:variable name="MMM" select="format-number(string-length(substring-before('JanFebMarAprMayJunJulAugSepOctNovDec',substring(substring(.,1,3),1,3))) div 3 + 1,'00')"/>
<xsl:variable name="D" select="format-number(floor(substring(.,5,2)),'00')" />
<xsl:variable name="YYYY" select="substring(.,8,4)" />
<xsl:variable name="hh" select="substring(.,13,2)" />
<xsl:variable name="mm" select="substring(.,16,2)" />
<xsl:variable name="ss" select="substring(.,19,2)" />
<xsl:value-of select="concat($YYYY,'-', $MMM, '-', $D, 'T', $hh, ':', $mm, ':', $ss, 'Z')" />
</xsl:template>
请注意,此模板引用与"."匹配的@datetime
属性,这是current()
的 x 路径简写。
然后更新其他模板以使用xsl:apply-templates
:
<xsl:template match ="transcript/call">
Your chat transcript from Univ100 @ Student dated <xsl:apply-templates select="@datetime"/>
-------------------------------------------------------------------------------------
</xsl:template>
<xsl:template match ="transcript/say">
<xsl:if test ="./@source ='customer'">
[<xsl:apply-templates select ="@datetime" />] <xsl:value-of select ="@name" /> says: <xsl:value-of select ="." />
</xsl:if>
<xsl:if test ="./@source ='agent'">
[<xsl:apply-templates select ="@datetime" />] Student Officer says: <xsl:value-of select ="." />
</xsl:if>
<xsl:if test ="./@source ='system'">
<xsl:if test ="./@display ='true'">
[<xsl:apply-templates select ="@datetime" />] System: <xsl:value-of select ="." />
</xsl:if>
</xsl:if>
</xsl:template>
等等...
无关:我注意到您的格式化程序选择带有format-number(floor(substring(.,5,1)),'00')
的日期。这是对的吗?它能用两位数的日子吗?