Solution 1 :

Here’s a way this could be done. Adapt the layout to what you need.

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

See it working here : https://xsltfiddle.liberty-development.net/gVhDDzf

Problem :

So I have the 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>

To get the text:

Forecast for the rest of Monday April 13 Some high cloud, clearing by
the afternoon. Light winds. Forecast for the rest of Tuesday April 14
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. Forecast for the rest of Wednesday April 15
Patchy morning fog. Mostly sunny day. Light winds.
…. etc

I used:

<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. How would I be able to get the text above and write it to a html as well as the temperatures inside the nodes:

/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. How would I do it so that it skips the first text in this loop? i.e. I don’t want the “Forecast for the rest of Monday April 13, only starting at Tuesday April 14 as well as the corresponding temperatures?

Desired output:

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

Something like this.. thanks!

Comments

Comment posted by Daichi

Seems to work, could I ask how exactly the //area that you used to select the text works? as well as what assigning currentF to a variable does? Thanks!

Comment posted by Daichi

Also, if I were to exclude the last 3 entries of text, how would I do it?

Comment posted by Sebastien

The // goes back to the root node and then it selects area. Could also have written it as //forecast/area. In your for-each you are in the context of a forecast node, so you have to go back up the tree. To exclude the last 3 entries you could try adding a condition in the predicate of the for-each.

By

Leave a Reply

Your email address will not be published. Required fields are marked *