XSLT 1.0模板和变量创建

时间:2016-12-08 12:16:17

标签: xslt xslt-1.0

我正在运行此代码,我根据需要在'@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的新手,我必须解决这个问题,因为我没有其他选择来更改或升级版本,我只能使用Pure XSLT 1.0。这是我对项目的唯一控制,我无法访问数据库服务器,使用此XLST的XML文件或项目的任何其他方面。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

<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')的日期。它是否正确?它是否适用于两位数的日子?