Как мне получить текст из двух разных узлов / элементов одновременно, когда я повторяю в xslt? - PullRequest
1 голос
/ 26 мая 2020

Итак, у меня есть 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>

Чтобы получить текст:

Прогноз на оставшуюся часть понедельника, 13 апреля. Высокая облачность, очищается к полудню. . Легкие ветры. Прогноз на оставшуюся часть вторника, 14 апреля. Утром туман. Солнечный день. Слабый ветер, переходящий с северо-запада на 15-20 км / ч в полдень, становится слабым ближе к вечеру. Прогноз на оставшуюся часть среды, 15 апреля. Утренний туман. Преимущественно солнечный день. Легкие ветры. .... et c

Я использовал:

<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. Как мне сделать так, чтобы он пропускал первый текст в этом l oop? т.е. мне не нужен прогноз на оставшуюся часть понедельника 13 апреля, только начиная со вторника 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 ....

Что-то вроде это .. спасибо!

1 Ответ

1 голос
/ 26 мая 2020

Вот как это можно сделать. Адаптируйте макет к тому, что вам нужно.

<?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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...