XML Tutorial/XSLT stylesheet/date function

Материал из Web эксперт
Версия от 08:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Date calculation

File: Data.xml

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:auth="http://www.authors.ru/">
  <xsl:output method="text"/>
  <xsl:variable name="yMD1" as="xs:yearMonthDuration" select="xs:yearMonthDuration("P1Y8M")"/>
  <xsl:variable name="yMD2" as="xs:yearMonthDuration" select="xs:yearMonthDuration("P2Y7M")"/>
  <xsl:variable name="dTD1" as="xs:dayTimeDuration"   select="xs:dayTimeDuration("P5DT9H23M12S")"/>
  <xsl:variable name="dTD2" as="xs:dayTimeDuration"   select="xs:dayTimeDuration("P3DT16H12M17S")"/>
  <xsl:variable name="dT" as="xs:dateTime"            select="xs:dateTime("1995-04-21T00:47:00")"/>
  <xsl:variable name="d" as="xs:date"                 select="xs:date("1995-04-21")"/>
  <xsl:variable name="t" as="xs:time"                 select="xs:time("17:03:00")"/>
  <xsl:template match="/">
    <xsl:text>More tests of addition in XPath 2.0:</xsl:text>
    <xsl:text>&#xA;&#xA;  Two xs:yearMonthDurations:&#xA;    </xsl:text>
    <xsl:value-of select="($yMD1, "+", $yMD2, "=", $yMD1 + $yMD2)"/>
    <xsl:text>&#xA;&#xA;  Two xs:dayTimeDurations:&#xA;    </xsl:text>
    <xsl:value-of select="($dTD1, "+", $dTD2, "=", $dTD1 + $dTD2)"/>
    <xsl:text>&#xA;&#xA;  An xs:yearMonthDuration and an </xsl:text>
    <xsl:text>xs:dateTime:&#xA;    </xsl:text>
    <xsl:value-of select="($dT, "+", $yMD1, "=", $dT + $yMD1)"/>
    <xsl:text>&#xA;&#xA;  An xs:dayTimeDuration and an </xsl:text>
    <xsl:text>xs:dateTime:&#xA;    </xsl:text>
    <xsl:value-of select="($dT, "+", $dTD1, "=", $dT + $dTD1)"/>
    <xsl:text>&#xA;&#xA;  An xs:yearMonthDuration and an </xsl:text>
    <xsl:text>xs:date:&#xA;    </xsl:text>
    <xsl:value-of select="($d, "+", $yMD1, "=", $d + $yMD1)"/>
    <xsl:text>&#xA;&#xA;  An xs:dayTimeDuration and an </xsl:text>
    <xsl:text>xs:date:&#xA;    </xsl:text>
    <xsl:value-of select="($d, "+", $dTD1, "=", $d + $dTD1)"/>
    <xsl:text>&#xA;&#xA;  An xs:dayTimeDuration and an </xsl:text>
    <xsl:text>xs:time:&#xA;    </xsl:text>
    <xsl:value-of select="($t, "+", $dTD1, "=", $t + $dTD1)"/>
    <xsl:text>xs:time:&#xA;    </xsl:text>
    <xsl:value-of select="/sonnet/auth:author/year-of-death - 
                          /sonnet/auth:author/year-of-birth"/>
  </xsl:template>
</xsl:stylesheet>
Output:
More tests of addition in XPath 2.0:
  Two xs:yearMonthDurations:
    P1Y8M + P2Y7M = P4Y3M
  Two xs:dayTimeDurations:
    P5DT9H23M12S + P3DT16H12M17S = P9DT1H35M29S
  An xs:yearMonthDuration and an xs:dateTime:
    1995-04-21T00:47:00 + P1Y8M = 1996-12-21T00:47:00
  An xs:dayTimeDuration and an xs:dateTime:
    1995-04-21T00:47:00 + P5DT9H23M12S = 1995-04-26T10:10:12
  An xs:yearMonthDuration and an xs:date:
    1995-04-21 + P1Y8M = 1996-12-21
  An xs:dayTimeDuration and an xs:date:
    1995-04-21 + P5DT9H23M12S = 1995-04-26
  An xs:dayTimeDuration and an xs:time:
    17:03:00 + P5DT9H23M12S = 02:26:12xs:time: