0

次のようなイベントに関する説明と日付を含む XML ファイルがあります。

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="timedate.xsl"?>
<catalog>
    <event>
        <name>AAA Festival</name>
        <place>New York</place>
        <country>USA</country>
        <date>19/11/2013</date>
    </event>
    <event>
        <name>BBB Festival</name>
        <place>Paris</place>
        <country>France</country>
        <date>11/10/2013</date>
    </event>
    <event>
        <name>CCC Festival</name>
        <place>London</place>
        <country>UK</country>
        <date>29/09/2013</date>
    </event>
</catalog>

および XSL ファイル:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template match="/">
<html>
  <body>
    <h2>Upcoming events</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Name</th>
        <th>Place</th>
        <th>Country</th>
        <th>Date</th> 
      </tr>
      <xsl:for-each select="catalog/event">
      <xsl:sort select="date" order="descending"/>
      <tr>
        <td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="place"/></td>
        <td><xsl:value-of select="country"/></td>
        <td><xsl:value-of select="date"/></td>
</tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

今後のイベント (今日の日付を含む) のみを並べ替えて一覧表示するページを作成したいと考えています。日付が適切にフォーマットされておらず、現在の日付を取得してそれらを比較し、将来のイベントを印刷できるため、これを行うことができません。うまくいく例で解決策を教えてください。あなたの返信と助けを前もって感謝します。よろしく!

4

1 に答える 1

0

生の日付を数値形式に変換することに関係する日付を比較/ソートするためのアイデア。たとえば、式によるyear*372+12*month*31+day(実際には正確な数値は必要ないため、372 と 31 は問題ありません)

XML の日付形式が固定されている場合 (たとえば、1 が常に であることが確実な場合01)、XPath 関数の部分文字列を使用できます。

EDITED-あなたの主張に関して、私はxslの完全なサンプルを掲載しています。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template match="/">
<html>
  <body>
    <h2>Upcoming events</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Name</th>
        <th>Place</th>
        <th>Country</th>
        <th>Date</th> 
      </tr>
      <xsl:for-each select="catalog/event">
        <xsl:sort 
         select="number(substring(date, 7, 4))*372+12*31*number(substring(date, 4, 2))+number(substring(date, 1, 2))" 
         order="descending" data-type="number"/>
        <tr>
            <td><xsl:value-of select="name"/></td>
            <td><xsl:value-of select="place"/></td>
            <td><xsl:value-of select="country"/></td>
            <td><xsl:value-of select="date"/></td>
            <!-- column just for debug-->
            <td><xsl:value-of select="number(substring(date, 7, 4))*372+12*31*number(substring(date, 4, 2))+number(substring(date, 1, 2))"/></td>
        </tr>
      </xsl:for-each>

    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
于 2013-05-24T14:03:33.117 に答える