2017-07-04 11 views
0

内のタグに値として現在の日付を追加:は、私は以下のようなXMLを持っているISO 8601形式のXSLT

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<properties> 
<entry key="user">1234</entry> 
</properties> 

私は1つは、ISO 8601形式で現在の日付の値が含まれており、他の二つの新しいタグを追加しますISO 8601形式で現在の日付+ 10年に設定し、XSLTを使用して新しいXMLファイルに変換した日付と1は、出力XMLは、この

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<properties> 
<entry key="user">1234</entry> 
<entry key="doc:uploadDate">2017-07-04T22:18:08Z</entry> 
<entry key="doc:deleteDate">2027-07-04T22:18:08Z</entry> 
</properties> 

のように、私は以下のXSLTを使用していなければなりません。

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> 

    <xsl:variable name="currentDate"> 
     <xsl:value-of select="current-dateTime()" /> 
    </xsl:variable> 

    <xsl:template match="entry[@key='doc:uploadDate']"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*" /> 
      <xsl:value-of 
       select="replace(normalize-space($currentDate), 
     '^(\d{2})-(\d{2})-(\d{4})\s+(.*)','$3-$1-$2T$4Z')" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:transform> 

誰かがこれで私を助けてもらえ..

+0

MM-DD-YYYYの日付をYYYY-MM-DD形式に変換するように見えるコードがありますが、日付はfn:current-dateTimeから来ていますy YYYY-MM-DDフォーマット。それは実際にxs:dateTimeとして返されます。これは、xsl:value-ofを使って文字列に変換し、次に解析します。なぜこのすべての複雑さ? –

答えて

2

をあなたは現在の日付に10年の期間を追加することができます。

<xsl:variable name="curr-date" select="current-dateTime()"/> 
    <entry> 
     <xsl:value-of select="$curr-date"/> 
    </entry> 
    <entry> 
     <xsl:value-of select="$curr-date + xs:yearMonthDuration('P10Y')"/> 
    </entry> 
+0

ソリューションMartinさんありがとうございます。上記のコードを使用した後、出力日の値は** 2017-07-04T15:19:42.959 + 05:30 **になりますが、これはこのようになります** 2017-07-04T15:19:42Z **。提案してください。 –

+0

'format-dateTime' https://www.w3.org/TR/xpath-functions-31/#func-format-dateTimeを使用して希望のフォーマットを作成し、可能なものの定義を見る時間を取ることができます機能するピクチャの文字列。 –

+0

ありがとうマーティン、私は以下の関数を使用して、それはかなりうまく動作しています。 [Y01] - [M01] - [D01] T [h01]:[m01]:[s01] Z ') "/ xhtml: > ' –

0

なぜあなたは、単に行うことはできません。

<xsl:template match="/properties"> 
    <xsl:copy> 
     <xsl:copy-of select="*"/> 
     <entry key="doc:uploadDate"> 
      <xsl:value-of select="current-dateTime()"/> 
     </entry> 
     <entry key="doc:deleteDate"> 
      <xsl:value-of select="current-dateTime() + xs:yearMonthDuration('P10Y')"/> 
     </entry> 
    </xsl:copy> 
</xsl:template> 
関連する問題