开发者

Including weather report in website using XSLT

I want to include the weather report at ftp://ftp.knmi.nl/pub_weerberichten/basisverwachting.xml to my website.

The specific element I want to include is located at:

report/data/location/block

where block contains:

<field_id>Verwachting</field_id>

How do I do this using simple XSL Transformations?

Update:

I have the following XSL Transformation however it has no results:

<xsl:variable name="weerknmi" select="document('ftp://ftp.knmi.nl/pub_weerberichten/basisverwachting.xml')/report/data/location"/>
<xsl:value-of select="$weerknmi/block[field_id = 'Verwachting'开发者_运维百科]/field_content"/>


Use:

report/data/location/block[field_id='Verwachting']/field_content

Here is a complete and really short XSLT transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vDoc" select=
 "document('ftp://ftp.knmi.nl/pub_weerberichten/basisverwachting.xml')"/>

 <xsl:template match="/">
  <xsl:apply-templates select="$vDoc" mode="doc"/>
 </xsl:template>  

 <xsl:template match="/" mode="doc">
     <xsl:value-of select=
      "report/data/location/block[field_id='Verwachting']/field_content"/>
 </xsl:template>
</xsl:stylesheet>

when applied to any document (not used), the transformation dynamically obtains the XML document located ar ftp://ftp.knmi.nl/pub_weerberichten/basisverwachting.xml, processes it and the wanted result is produced:

Vrij veel bewolking en vooral in het noorden af en toe regen of motregen. In het zuiden blijft het overwegend droog. De middagtemperatuur loopt uiteen van een graad of 14 in het Waddengebied tot 19 graden in het zuidoosten. De zuidwestelijke wind is matig tot vrij krachtig. Aan de kust is de wind krachtig of hard, 6 tot 7.

Komende nacht is er vrij veel bewolking met in het noorden en midden mogelijk nog lichte regen. De minimumtemperatuur ligt rond een graad of 11. De zuidwestelijke wind neemt af naar zwak tot matig, rond kracht 3. Aan de kust is de wind af en toe vrij krachtig, 5.

Morgen overdag breekt op steeds meer plaatsen de zon door en is het vrijwel overal droog. De middagtemperatuur loopt uiteen van een graad of 16 in het noordwestelijk kustgebied tot 23 graden in het zuidoosten. De zuidwestelijke wind trekt weer aan en wordt matig tot vrij krachtig, kracht 4 of 5, aan de kust en op het IJsselmeer krachtig tot hard, 6 of 7 Bft. ’s Avonds neemt de wind af. (Bron: KNMI)


A simple XPath expression will do this:

report/data/location/block[field_id = 'Verwachting']/field_content

If you want to use this in XSLT, you can just do:

<xsl:value-of select="document('ftp://ftp.knmi.nl/pub_weerberichten/basisverwachting.xml')/report/data/location/block[field_id = 'Verwachting']/field_content" />

I might have the syntax slightly wrong, I don't often use document() and I can't easily check it at the moment, but you get the idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜