-1

編集:リファクタリングされた質問とコード

この質問を拡張する..。

プレゼンテーションの開始時刻と終了時刻を次の形式で表す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
  ....

この時点では、直接フォーマットについては少し気にしませんが、変数ベースのこの2つの部分からなるグループ化を可能にしたいと考えています。ありとあらゆる助けは間違いなくありがたいです。

4

1 に答える 1

1

XSLT 1.0では、変数を使用して2パス変換を実行し、次に次のような拡張関数を実行できます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>
于 2012-10-07T09:31:09.880 に答える