2012-10-06 82 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