2012-10-06 80 views
-1

編輯:重構問題和代碼分組XML元素

擴展this question ...

我有一個代表在格式開始和結束呈現時間的XML飼料:2012-06-06 10:45 ,每個這些節點在以這種方式表示:

<session id="9c4716c71f" name="Session 1" starttime="2012-06-06 10:45" endtime="2012-06-06 12:45" location="" chair=""> 
    <article code="c1" id="TT-282" type="presentation"></article> 
    <article code="c2" id="TT-301" type="presentation"></article> 
    ... 
</session> 
<session id="9c4716c249z" name="Session 2" starttime="2012-06-06 14:45" endtime="2012-06-06 16:45" location="" chair=""> 
    <article code="c1" id="TT-214" type="presentation"></article> 
    <article code="c2" id="TT-328" type="presentation"></article> 
    ... 

在前面的問題,我只關心通過次分組,但現在我想(2012年6月6日)將所有這些組合在一起。因此,理想情況下,我將輸出按日期分組,然後按時間分組(如我將在下面的代碼中所示)。

我有應用,以顯示該部分的模板:

<!-- formatting dateTime --> 
<xsl:template name="formatDate"> 
    <xsl:param name="dateTime" /> 
    <xsl:variable name="date" select="substring-before($dateTime, ' ')" /> 
    <xsl:variable name="year" select="substring-before($date, '-')" /> 
    <xsl:variable name="month" select="number(substring-before(substring-after($date, '-'), '-'))" /> 
    <xsl:variable name="day" select="number(substring-after(substring-after($date, '-'), '-'))" /> 

    <!-- output --> 
    <xsl:choose> 
    <xsl:when test="$month = '1'">January</xsl:when> 
    <xsl:when test="$month = '2'">February</xsl:when> 
    <xsl:when test="$month = '3'">March</xsl:when> 
    <xsl:when test="$month = '4'">April</xsl:when> 
    <xsl:when test="$month = '5'">May</xsl:when> 
    <xsl:when test="$month = '6'">June</xsl:when> 
    <xsl:when test="$month = '7'">July</xsl:when> 
    <xsl:when test="$month = '8'">August</xsl:when> 
    <xsl:when test="$month = '9'">September</xsl:when> 
    <xsl:when test="$month = '10'">October</xsl:when> 
    <xsl:when test="$month = '11'">November</xsl:when> 
    <xsl:when test="$month = '12'">December</xsl:when> 
    </xsl:choose> 
    <xsl:value-of select="' '" /> 
    <xsl:value-of select="$day" /> 
    <xsl:value-of select="', '" /> 
    <xsl:value-of select="$year" /> 
</xsl:template> 

<!-- formatting dateTime --> 
<xsl:template name="formatTime"> 
    <xsl:param name="dateTime" /> 
    <xsl:value-of select="substring-after($dateTime, ' ')" /> 
</xsl:template> 

和我應用它以這種方式:

<h4> 
    <!-- output DD-MM-YYYY --> 
    <xsl:call-template name="formatDate"> 
    <xsl:with-param name="dateTime" select="@starttime" /> 
    </xsl:call-template> 
</h4> 

這是我的當前的分組的方法。可以看出,它直接在未格式化的日期時間(2012-06-06 10:45)上分組,但我想先對格式化的日期(存儲在$cdate之內),然後在$ctime之內進行分組。

<xsl:template match="session"> 

    <xsl:variable name="cdate"> 
    <xsl:call-template name="formatDate"> 
     <xsl:with-param name="dateTime" select="@starttime" /> 
    </xsl:call-template> 
    </xsl:variable> 


    <xsl:if test='not(preceding-sibling::session[@starttime = current()/@starttime])'> 
    <h2><xsl:value-of select="@starttime" /></h2> 
    <div><xsl:apply-templates /></div> 
    </xsl:if> 
</xsl:template> 

<xsl:template match="article"> 
    <b><xsl:value-of select="@name" /></b> 
</xsl:template> 

所以,目前我看到了一個顯示在頁面上的獨特@startime值列表。 ,我真的,真的喜歡看到(我仍然通過XSLT心態工作)的輸出是這樣的:

June 06, 2012 
    10:45 - 12:45 | TT-282 
       | TT-301 
    ---------------------------------- 
    14:45 - 16:45 | TT-214 
       | TT-328 

June 07, 2012 
    .... 

我關心的直接格式化少一點點在這一點上,只是希望能夠擁有這種基於變量的兩部分分組。任何和所有的幫助是絕對讚賞。

+0

我不理解downvotes ... – espais

+0

再次,如果你打算投票關閉,請至少告訴我什麼地方錯了...對我來說它明確清楚是什麼,我想 – espais

回答

1

使用XSLT 1.0,您可以使用變量和擴展函數如exsl:node-set(例如,

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:exsl="http://exslt.org/common" 
    exclude-result-prefixes="exsl" 
    version="1.0"> 

<xsl:variable name="temp-doc"> 
    <!-- now create the nodes here you want to process later on in a second pass --> 
</xsl:variable> 

<xsl:key name="k2" match="foo" use="bar"/> 

<xsl:template match="/"> 
    <!-- here we are processing the nodes in the variable obtained from the first pass of prcocesssing --> 
    <xsl:apply-templates select="exsl:node-set($temp-doc)//foo[generate-id() = generate-id(key('k2', bar)[1])]"/> 
</xsl:template> 

<xsl:template match="foo">...</xsl:template> 

</xsl:stylesheet> 
+0

是這個答案真的是針對當前的問題嗎?似乎並不如此。 –

+0

我很感謝你的回答,但我最終重構了我的代碼,並會用新代碼發佈一個更好形成的問題 – espais