Reputation: 29
I want to create a weather app and for that to work i need to be able to get a specefic tag from an XML file. I have managed to get a specific child node, but i cant seem to get a tag.
My main target is to get the tag "value="4" in temperature.
My XML and XSL file is shown below:
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<html>
<head>
<title>Sjekk været</title>
<link rel="stylesheet" type="text/css" href="weather.css"/>
</head>
<body>
<h1>Været og valutaen i Kanada:</h1>
<table>
<tr>
<th>Vær</th>
<th>Norske kroner</th>
<th>Kanadiske dollar</th>
</tr>
<xsl:for-each select="valuta/valutakurs[land='Canada']/overforsel">
<xsl:for-each select="document('http://www.yr.no/sted/Canada/Ontario/Toronto/varsel.xml')/weatherdata/forecast/tabular/time">
<tr>
-------> I want to show the tag in this field<-------
<td><xsl:value-of select="temperature"/></td>
<td>1 NOK</td>
<td><xsl:value-of select="kjop"/> CAD</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML:
<weatherdata>
<location>...</location>
<credit>...</credit> <links>...</links>
<meta>...</meta>
<sun rise="2018-10-24T07:43:16" set="2018-10-24T18:19:47"/>
<forecast>
<tabular>
<time from="2018-10-24T07:00:00" to="2018-10-24T12:00:00" period="1">
<!-- Valid from 2018-10-24T07:00:00 to 2018-10-24T12:00:00 -->
<symbol number="3" numberEx="3" name="Delvis skyet" var="03d"/>
<precipitation value="0"/>
<!-- Valid at 2018-10-24T07:00:00 -->
<windDirection deg="317.9" code="NW" name="Nordvest"/>
<windSpeed mps="3.7" name="Lett bris"/>
<temperature unit="celsius" value="4"/>
<pressure unit="hPa" value="1024.5"/> </time>
Upvotes: 0
Views: 761
Reputation: 70598
To answer your immediate question, to get the value of the temperature in the external document, do this:
<xsl:value-of select="temperature/@value"/>
This is because value
is an "attribute" of the temperature
element, so you use the @
prefix to denote attributes.
However, you will also have a problem with getting your kjop
element, because at the point you try to get it, you are in the context of your external document, whereas kjop
is an element on your original document. So, what you need to do is save a reference to the element in your original document, so you can refer back to it when you start selecting from the external document.
Try this snippet:
<xsl:for-each select="valuta/valutakurs[land='Canada']/overforsel">
<xsl:variable name="overforsel" select="." />
<xsl:for-each select="document('http://www.yr.no/sted/Canada/Ontario/Toronto/varsel.xml')/weatherdata/forecast/tabular/time">
<tr>
<td><xsl:value-of select="temperature/@value"/></td>
<td>1 NOK</td>
<td><xsl:value-of select="$overforsel/kjop"/> CAD</td>
</tr>
</xsl:for-each>
</xsl:for-each>
Upvotes: 1