2016-12-08 6 views
0

私はこのコードを実行しています。ここでは、自分のニーズに応じて '@datetime'で日時をフォーマットしています。 このコードは完全に機能します。しかしXSLT 1.0テンプレートと変数の作成

<?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> 

私は1.0をXSLTの非常に新しいですし、私はバージョンを変更またはアップグレードするには、他のオプションが用意されていないとして、私は唯一の純粋なXSLT 1.0上で動作することができ、この上で仕事をしなければなりません。これは私がプロジェクトに対して持っている唯一のコントロールです。私はDBサーバー、このXLSTを使用するXMLファイル、またはプロジェクトの他の側面にアクセスすることはできません。

ご協力いただければ幸いです。

+1

XML入力と予想される出力の例を表示してください - [mcve]を参照してください。 XMLに直接アクセスできない場合は、* identity transform *テンプレートを使用して取得してください。 –

答えて

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-パス短い手です「」。私はあなたのフォーマッタがformat-number(floor(substring(.,5,1)),'00')で一日を選択していることに注意してください:。

<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> 

のように...

無関係:

その後xsl:apply-templatesを使用するように他のテンプレートを更新これは正しいです?それは2桁の日に機能しますか?

+0

ありがとう、これは魅力のように機能します! 約2桁の日、それを指摘してくれてありがとう、それに応じて変更されます。 –

+0

このフォーマットされた文字列を変数に入れて助けてくれますか?私はこれを試しました: '' '' '' '' 'UP100 @ UP警察のチャット・トランスクリプトは' –

+0

です。期待どおりに働いていますか?変数を使うのではなく、必要ならば ''を複数回呼び出すことができます。 – teppic

関連する問題