xslt问题中的format-date函数

时间:2018-05-19 17:50:53

标签: xslt xslt-2.0 formatdatetime

我正在做一个家庭作业,创建一个XSL样式表,从我老师给出的XML文件中检索数据,并在XSLT中使用format-date函数。

我正在尝试以这种格式格式化日期:

4月26日星期四

这是我的xml的样子:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="weather-1.xsl"?>
<product version="1.7" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.bom.gov.au/schema/v1.7/product.xsd">
    <forecast>
        <area aac="NSW_FA001" description="New South Wales" type="region">
            <forecast-period start-time-local="2018-04-26T04:30:02+10:00" end-time-local="2018-04-26T04:30:02+10:00" start-time-utc="2018-04-25T18:30:02Z" end-time-utc="2018-04-25T18:30:02Z">
                <text type="synoptic_situation">
                    <p>A low pressure trough off the New South Wales north coast is weakening as a high pressure system south of the Bight moves very slowly east extending a ridge along the coast. The high is expected to move over the southern Tasman by Wednesday maintaining the ridge to the north.</p>
                </text>
                <text type="warning_summary_footer">Details of warnings are available on the Bureau's website www.bom.gov.au, by telephone 1300-659-218* or through some TV and radio broadcasts.</text>
                <text type="product_footer">* Calls to 1300 numbers cost around 27.5c incl. GST, higher from mobiles or public phones.</text>
            </forecast-period>
            <forecast-period index="0" start-time-local="2018-04-26T05:00:00+10:00" end-time-local="2018-04-27T00:00:00+10:00" start-time-utc="2018-04-25T19:00:00Z" end-time-utc="2018-04-26T14:00:00Z">
                <text type="fire_danger">Very High: Northwestern fire area.</text>
                <text type="forecast">Medium chance of showers along the northern half of the coast with the risk of thunderstorms. The chance of afternoon thunderstorms in the northern inland. Early fog in the east. Partly cloudy elsewhere. Daytime temperatures close to average. Winds south to southwesterly, freshening along the coast and about the central west slopes.</text>
            </forecast-period>
            <forecast-period index="1" start-time-local="2018-04-27T00:00:00+10:00" end-time-local="2018-04-28T00:00:00+10:00" start-time-utc="2018-04-26T14:00:00Z" end-time-utc="2018-04-27T14:00:00Z">
                <text type="forecast">A shower or two in the east, more likely about the north coast. The chance of a thunderstorm in the far northeast. Early frost patches about the southern ranges. Fine and mostly sunny in the west. Daytime temperatures below average, especially in the southeast. South to southeasterly winds, freshening about the northeast and along the coast.</text>
            </forecast-period>
            <forecast-period index="2" start-time-local="2018-04-28T00:00:00+10:00" end-time-local="2018-04-29T00:00:00+10:00" start-time-utc="2018-04-27T14:00:00Z" end-time-utc="2018-04-28T14:00:00Z">
                <text type="forecast">Showers in the east, more likely about the central and northern coastal fringe. Fine and mostly sunny elsewhere. Early frost patches about the southern ranges. Daytime temperatures below average in the east and about average elsewhere. South to southeasterly winds, freshening about the north coast.</text>
            </forecast-period>
            <forecast-period index="3" start-time-local="2018-04-29T00:00:00+10:00" end-time-local="2018-04-30T00:00:00+10:00" start-time-utc="2018-04-28T14:00:00Z" end-time-utc="2018-04-29T14:00:00Z">
                <text type="forecast">Showers in the east, more likely about the central and north coast. Fine and mostly sunny elsewhere. Early frost patches about the southern ranges. Daytime temperatures below average in the east, about average elsewhere. South to southeasterly winds.</text>
            </forecast-period>
        </area>
    </forecast>
</product>

这是我目前的XSL样式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:template match="product[1]">
    <html>
        <head>
            <title>State Weather</title>
        </head>
        <body>
            <h3>NSW and ACT Weather</h3>
                <p>Forecast for the rest of <xsl:value-of select="format-date(forecast/area/forecast-period[@start-time-local],'[FNn] [D1] [MNn]')"/></p>
                <p><xsl:value-of select="forecast/area/forecast-period[2]/text[2]"/></p>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

我正在使用http python服务器来测试我的样式表。要查看天气,它会显示正确的信息。

这是我希望实现的结果:

enter image description here

我做了各种谷歌搜索如何实现和使用格式日期功能,但我仍然对我做错了感到困惑。

1 个答案:

答案 0 :(得分:1)

您需要使用dateTime值选择属性值,因为它是dateTime,您需要使用format-dateTime而不是format-date

最小的例子是

  <xsl:template match="/">
     <xsl:value-of select="format-dateTime(/product/forecast/area/forecast-period/@start-time-local, '[FNn] [D1] [MNn]')"/>
  </xsl:template>

输出Thursday 26 April

https://xsltfiddle.liberty-development.net/bFDb2C5/1有一个工作样本。

当然你需要使用像Saxon 9这样的XSLT 2或3处理器。我不知道在Python的上下文中支持它,尽管Saxon 9 C原则上应该允许这样做。