2017-07-25 75 views
0

XML的我有3級XSLT轉換日期,分離節點

<IncorporationDate> 
    <CCYY>2016</CCYY> 
    <MM>04</MM> 
    <DD>21</DD> 
</IncorporationDate> 

現在我需要爲21 2016

所以我嘗試CONCAT這顯示今年四月和使用格式的日期,但得到錯誤的嘗試格式化字符串。

<xsl:variable name="incorpDate"> 
     <xsl:value-of select="concat(//a:Identification/b:IncorporationDate/c:DD,'/',//a:Identification/b:IncorporationDate/c:MM,'/',//a:Identification/b:IncorporationDate/c:CCYY)"/> 
</xsl:variable> 

然後

<xsl:value-of select="format-date($incorpDate, '[D] [MNn] [Y0001]')" /> 

我嘗試整個節點上的格式,日期

<xsl:value-of select="format-date(//a:Identification/b:IncorporationDate, '[D] [MNn] [Y0001]')" /> 

我知道這可能是簡單的,但XML/XSLT它不是我所知道的,我學這需要改變很多樣式表。

+0

你能說出你實際得到的錯誤嗎?另外,您是否可以確認您使用的是XSLT 2.0,因爲'format-date'在XSLT 1.0中不可用?謝謝! –

+0

當可變第一選擇,我得到錯誤: 無效的日期「21/04/2016」(非數字年份組成 時,第二個選項直接 無效的日期「20160421」(太短) 而我使用XSLT 2.0 – maw2be

回答

3

創建xs:date,然後格式化:

<xsl:template match="IncorporationDate"> 
    <xsl:value-of select="format-date(xs:date(concat(CCYY, '-', MM, '-', DD)), '[D] [MNn] [Y0001]')"/> 
</xsl:template> 

完整的示例

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

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

    <xsl:template match="IncorporationDate"> 
     <xsl:value-of select="format-date(xs:date(concat(CCYY, '-', MM, '-', DD)), '[D] [MNn] [Y0001]')"/> 
    </xsl:template> 

</xsl:transform> 

http://xsltransform.net/naZXpXm/1

0

我知道我做錯了。

關於concat我應該使格式YYYY-MM-DD格式日期功能將工作。

我改變這個,它的工作。