html - 当我在 xslt 中迭代时,如何同时从两个不同的节点/元素中获取文本?
问题描述
所以我有 XML:
<forecast>
<area aac="NSW_FA001" description="New South Wales" type="region">
<forecast-period start-time-local="2020-04-13T09:08:07+10:00" end-time-local="2020-04-13T09:08:07+10:00" start-time-utc="2020-04-12T23:08:07Z" end-time-utc="2020-04-12T23:08:07Z">
<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>
</area>
<area aac="NSW_ME006" description="Canberra" type="metropolitan" parent-aac="NSW_FA001">
<forecast-period index="0" start-time-local="2020-04-13T00:00:00+10:00" end-time-local="2020-04-14T00:00:00+10:00" start-time-utc="2020-04-12T14:00:00Z" end-time-utc="2020-04-13T14:00:00Z">
<text type="forecast">Some high cloud, clearing by the afternoon. Light winds.</text>
<text type="uv_alert">Sun protection 9:30am to 2:20pm, UV Index predicted to reach 5 [Moderate]</text>
</forecast-period>
<forecast-period index="1" start-time-local="2020-04-14T00:00:00+10:00" end-time-local="2020-04-15T00:00:00+10:00" start-time-utc="2020-04-13T14:00:00Z" end-time-utc="2020-04-14T14:00:00Z">
<text type="forecast">Areas of fog in the morning. Sunny day. Light winds becoming northwesterly 15 to 20 km/h in the early afternoon then becoming light in the late afternoon.</text>
</forecast-period>
<forecast-period index="2" start-time-local="2020-04-15T00:00:00+10:00" end-time-local="2020-04-16T00:00:00+10:00" start-time-utc="2020-04-14T14:00:00Z" end-time-utc="2020-04-15T14:00:00Z">
<text type="forecast">Patchy morning fog. Mostly sunny day. Light winds.</text>
</forecast-period>
<forecast-period index="3" start-time-local="2020-04-16T00:00:00+10:00" end-time-local="2020-04-17T00:00:00+10:00" start-time-utc="2020-04-15T14:00:00Z" end-time-utc="2020-04-16T14:00:00Z">
<text type="forecast">Partly cloudy. Slight (20%) chance of a shower. Winds northwesterly and light increasing to 20 to 25 km/h during the morning then increasing to 25 to 35 km/h during the day.</text>
</forecast-period>
<forecast-period index="4" start-time-local="2020-04-17T00:00:00+10:00" end-time-local="2020-04-18T00:00:00+10:00" start-time-utc="2020-04-16T14:00:00Z" end-time-utc="2020-04-17T14:00:00Z">
<text type="forecast">Mostly sunny. Winds northwesterly 15 to 20 km/h.</text>
</forecast-period>
<forecast-period index="5" start-time-local="2020-04-18T00:00:00+10:00" end-time-local="2020-04-19T00:00:00+10:00" start-time-utc="2020-04-17T14:00:00Z" end-time-utc="2020-04-18T14:00:00Z">
<text type="forecast">Mostly sunny. Slight (20%) chance of a shower. Light winds.</text>
</forecast-period>
<forecast-period index="6" start-time-local="2020-04-19T00:00:00+10:00" end-time-local="2020-04-20T00:00:00+10:00" start-time-utc="2020-04-18T14:00:00Z" end-time-utc="2020-04-19T14:00:00Z">
<text type="forecast">Partly cloudy. Slight (30%) chance of a shower. Light winds.</text>
</forecast-period>
</area>
<area aac="NSW_PT027" description="Canberra" type="location" parent-aac="NSW_ME006">
<forecast-period index="0" start-time-local="2020-04-13T08:57:34+10:00" end-time-local="2020-04-14T00:00:00+10:00" start-time-utc="2020-04-12T22:57:34Z" end-time-utc="2020-04-13T14:00:00Z">
<element type="forecast_icon_code">3</element>
<element type="air_temperature_maximum" units="Celsius">19</element>
<text type="precis">Partly cloudy.</text>
<text type="probability_of_precipitation">0%</text>
</forecast-period>
<forecast-period index="1" start-time-local="2020-04-14T00:00:00+10:00" end-time-local="2020-04-15T00:00:00+10:00" start-time-utc="2020-04-13T14:00:00Z" end-time-utc="2020-04-14T14:00:00Z">
<element type="forecast_icon_code">10</element>
<element type="air_temperature_minimum" units="Celsius">4</element>
<element type="air_temperature_maximum" units="Celsius">23</element>
<text type="precis">Possible early fog then sunny.</text>
<text type="probability_of_precipitation">0%</text>
</forecast-period>
<forecast-period index="2" start-time-local="2020-04-15T00:00:00+10:00" end-time-local="2020-04-16T00:00:00+10:00" start-time-utc="2020-04-14T14:00:00Z" end-time-utc="2020-04-15T14:00:00Z">
<element type="forecast_icon_code">3</element>
<element type="air_temperature_minimum" units="Celsius">8</element>
<element type="air_temperature_maximum" units="Celsius">25</element>
<text type="precis">Mostly sunny.</text>
<text type="probability_of_precipitation">0%</text>
</forecast-period>
<forecast-period index="3" start-time-local="2020-04-16T00:00:00+10:00" end-time-local="2020-04-17T00:00:00+10:00" start-time-utc="2020-04-15T14:00:00Z" end-time-utc="2020-04-16T14:00:00Z">
<element type="forecast_icon_code">3</element>
<element type="air_temperature_minimum" units="Celsius">11</element>
<element type="air_temperature_maximum" units="Celsius">25</element>
<text type="precis">Partly cloudy.</text>
<text type="probability_of_precipitation">20%</text>
</forecast-period>
<forecast-period index="4" start-time-local="2020-04-17T00:00:00+10:00" end-time-local="2020-04-18T00:00:00+10:00" start-time-utc="2020-04-16T14:00:00Z" end-time-utc="2020-04-17T14:00:00Z">
<element type="forecast_icon_code">3</element>
<element type="air_temperature_minimum" units="Celsius">11</element>
<element type="air_temperature_maximum" units="Celsius">20</element>
<text type="precis">Mostly sunny.</text>
<text type="probability_of_precipitation">10%</text>
</forecast-period>
<forecast-period index="5" start-time-local="2020-04-18T00:00:00+10:00" end-time-local="2020-04-19T00:00:00+10:00" start-time-utc="2020-04-17T14:00:00Z" end-time-utc="2020-04-18T14:00:00Z">
<element type="forecast_icon_code">3</element>
<element type="air_temperature_minimum" units="Celsius">4</element>
<element type="air_temperature_maximum" units="Celsius">19</element>
<text type="precis">Mostly sunny.</text>
<text type="probability_of_precipitation">20%</text>
</forecast-period>
<forecast-period index="6" start-time-local="2020-04-19T00:00:00+10:00" end-time-local="2020-04-20T00:00:00+10:00" start-time-utc="2020-04-18T14:00:00Z" end-time-utc="2020-04-19T14:00:00Z">
<element type="forecast_icon_code">3</element>
<element type="precipitation_range">0 to 0.4 mm</element>
<element type="air_temperature_minimum" units="Celsius">8</element>
<element type="air_temperature_maximum" units="Celsius">19</element>
<text type="precis">Partly cloudy.</text>
<text type="probability_of_precipitation">30%</text>
</forecast-period>
</area>
<area aac="NSW_PT146" description="Tuggeranong" type="location" parent-aac="NSW_ME006">
<forecast-period index="0" start-time-local="2020-04-13T08:57:34+10:00" end-time-local="2020-04-14T00:00:00+10:00" start-time-utc="2020-04-12T22:57:34Z" end-time-utc="2020-04-13T14:00:00Z">
<element type="air_temperature_maximum" units="Celsius">19</element>
</forecast-period>
</area>
</forecast>
要获取文本:
4 月 13 日星期一剩余时间的预报 一些高云,下午将转晴。轻风。预报 4 月 14 日星期二剩余时间 上午有雾区。晴天。午后风速为 15 至 20 公里/小时,然后在下午晚些时候变小。预报 4 月 15 日星期三剩余时间 晨雾斑驳。大多是晴天。轻风。.... ETC
我用了:
<xsl:variable name ="cb" select="document('cb123.xml')"/>
<xsl:variable name = "canberra_date" select = "$cb/forecast/area[@aac='NSW_ME006']/forecast-period"/>
<b>Canberra Weather</b>
<br></br>
<xsl:for-each select='$canberra_date'>
<xsl:text>Forecast for the rest of </xsl:text><xsl:value-of select="format-dateTime(@start-time-local, '[F] [MNn] [D1]', 'en', (), ()) "/>
<br></br>
<xsl:value-of select="text[@type='forecast']"/>
<br></br>
</xsl:for-each>
Q1。我如何能够获取上面的文本并将其写入 html 以及节点内的温度:
/forecast/area[@aac='NSW_PT027']/forecast-period/element[@type='air_temperature_maximum'] and
/forecast/area[@aac='NSW_PT027']/forecast-period/element[@type='air_temperature_minimum']
Q2。我该怎么做才能跳过此循环中的第一个文本?即我不想要“4 月 13 日星期一剩余时间的预测,仅从 4 月 14 日星期二开始以及相应的温度?
期望的输出:
> Forecast for Tuesday 14 April Areas of fog in the morning. Sunny day.
> Light winds becoming northwesterly 15 to 20 km/h in the early
> afternoon then becoming light in the late afternoon. Min 4 Max 23
>
> Forecast for Wednesday 15 April Patchy morning fog. Mostly sunny day.
> Light winds. Min 8 Max 25 ....
像这样的东西..谢谢!
解决方案
这是一种可以做到的方法。根据您的需要调整布局。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Weather Forecast</title>
</head>
<body>
<b>Canberra Weather</b>
<br/>
<xsl:for-each select="forecast/area[@aac='NSW_ME006']/forecast-period[position()!=1]"> <!-- Skip first forecast-period -->
<xsl:variable name="currentF" select="."/>
<xsl:text>Forecast for the rest of </xsl:text><xsl:value-of select="format-dateTime(@start-time-local, '[F] [MNn] [D1]', 'en', (), ()) "/>
<xsl:value-of select="text[@type='forecast']"/>
<br/>
Min <xsl:value-of select="//area[@parent-aac=$currentF/../@aac]/forecast-period[@start-time-local=$currentF/@start-time-local]/element[@type='air_temperature_minimum']"/>
<br/>
Max <xsl:value-of select="//area[@parent-aac=$currentF/../@aac]/forecast-period[@start-time-local=$currentF/@start-time-local]/element[@type='air_temperature_maximum']"/>
<br/><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
看到它在这里工作:https ://xsltfiddle.liberty-development.net/gVhDDzf
推荐阅读
- c# - 如何从另一种形式更改图片框的图像?
- python - Pandas:将 CSV 数据从单列重新格式化为多个新列
- html - 如何根据它包含的图像的高度来调整 div?
- python - 用于有时间限制的用户删除的 Boto 脚本
- javascript - 如何在浏览器中使用 `Headers` 迭代器?
- php - 添加上传功能阻止了我的多次删除
- android - Android Studio:我的按钮真正的菜鸟问题
- visual-studio-code - rust-analyzer 错误“无法解析宏 `$crate::format_args`”是什么意思,我该如何解决?
- android - 无法为 com.android.build.gradle.internal.dsl.NdkOptions 类型的对象设置未知属性“debugSymbolLevel”
- elasticsearch - 如何将新节点副本添加到 Elasticsearch 正在运行,而不重新启动服务?