Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xslt问题中的格式化日期函数_Xslt_Xslt 2.0_Formatdatetime - Fatal编程技术网

xslt问题中的格式化日期函数

xslt问题中的格式化日期函数,xslt,xslt-2.0,formatdatetime,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

我正在做一项家庭作业,创建一个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服务器来测试我的样式表。要查看天气,它会显示正确的信息

这是我的目标:


我在谷歌上搜索了很多关于如何实现和使用format date功能的信息,但我仍然不清楚我做错了什么。

你需要选择带有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>
4月26日星期四

有一个工作样本


当然,您需要使用XSLT2或3处理器,如Saxon9。我不知道在Python的上下文中支持这一点,尽管Saxon 9 C原则上应该允许这样做。

那么,您得到了什么结果?您确定使用XSLT2处理器吗?如果要设置属性值的格式,则dateforecast/area/forecast period[@start time local]的格式应为dateforecast/area/forecast period/@start time local。当某项操作失败时,请始终告诉我们它是如何失败的。查看失败症状始终是解决问题的第一步。很难知道它是如何失败的,因为我使用的是http python服务器,编辑xsl的唯一方法是通过notepadd++我能够找到一种方法,通过命令行使用saxon9并执行我的样式表我厌倦了你的代码,收到了这个错误消息weather-1第13行第123列xsl:value of/@select中char 17处键入错误。xsl:XPTY0004:fn:format dateTime 2018-04-26T04:30:02+10:00的第一个参数不允许包含多个项目的序列,2018-04-26T05:00:00+10:00未命名模式下/产品的内置模板规则一个以上项目的序列不允许作为fn:form在日期时间2018-04-26T04:30:02+10:00的第一个参数2018-04-26T05:00:00+10:00。我给了您一个小示例,演示如何针对示例数据的一个实例使用format dateTime函数,以获得所需的输出,因为您似乎很难使用该函数。显然,如果您想要格式化一系列的值,那么您需要一种完全不同的方法,比如使用match=forecast period设置一个tempate,在这里您只需使用format-dateTime@start-当地时间,'[FNn][D1][MNn]“。或者,为了快速返回基于上一示例的格式化值序列,可以使用/product/forecast/area/forecast period/format-dateTime@start-本地时间,“[FNn][D1][MNn]”以避免该错误。但是您可能需要花一些时间阅读中有关XSLT处理模型的一章,这样您就能够更好地针对模块化的XML输入结构编写代码,并将其分解为小的可管理单元。我试图更具体地说明在何处调用开始时间局部,因此添加了[index='0']在forecast period的旁边,可以看到这是可行的,但我有一个未关闭的属性错误