-
Notifications
You must be signed in to change notification settings - Fork 4
Samples XSLT Transformations
openHAB allows you to post HTTP queries over the internet, and to put the received data in items that you can use in your rules. In most cases, you will get the information you need into a XML structured document, and you need a way to extract only the value you want: here is where XSLT transformations come in our help. XSLT is a standard method to transform an XML structure into a document with the structure you want. You can find a very good tutorial here: XSLT tutorial at W3Schools
In the following examples:
- the directive xsl:output says that the incoming document must be transformed into another XML-like document, without XML header (the part)
- the directive template specify a rule to apply when a specific XML node type is found. The template to match is contained in the match="/" clause, where "/" means "any type of node", so the whole document.
- inside the template directive we find the rule, in this case a value to extract from the selected node. In the first example, we want to extract the value temp from the node yweather:condition. Indeed, if we look at the syntax of the Yahoo Weather service, we can see that the weather conditions are returned into a XML document that contains a string like that:
<yweather:condition text="Mostly Cloudy" code="28" temp="50" date="Fri, 18 Dec 2009 9:38 am PST" />
Yahoo Weather - temperature
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="1.0">
<xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:value-of select="//yweather:condition/@temp" />
</xsl:template>
</xsl:stylesheet>
Yahoo Weather - feels like temperature
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="1.0">
<xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:value-of select="//yweather:wind/@chill" />
</xsl:template>
</xsl:stylesheet>
Yahoo Weather - humidity
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="1.0">
<xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:value-of select="//yweather:atmosphere/@humidity" />
</xsl:template>
</xsl:stylesheet>
Yahoo Weather - sunrise
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="1.0">
<xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:value-of select="//yweather:astronomy/@sunrise" />
</xsl:template>
</xsl:stylesheet>
Yahoo Weather - sunset
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="1.0">
<xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:value-of select="//yweather:astronomy/@sunset" />
</xsl:template>
</xsl:stylesheet>
Yahoo Weather - weather text
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="1.0">
<xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:value-of select="//item/yweather:condition/@text" />
</xsl:template>
</xsl:stylesheet>
Yahoo Weather - wind speed
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="1.0">
<xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:value-of select="//yweather:wind/@speed" />
</xsl:template>
</xsl:stylesheet>
Yahoo Weather - forecast tomorrow
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="1.0">
<xsl:output indent="yes" method="text" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:value-of select="//item/yweather:forecast[1]/@text" />
<xsl:text>, Low: </xsl:text>
<xsl:value-of select="//item/yweather:forecast[1]/@low" />
<xsl:text>°C, High: </xsl:text>
<xsl:value-of select="//item/yweather:forecast[1]/@high" />
<xsl:text>°C</xsl:text>
</xsl:template>
</xsl:stylesheet>
Yahoo Weather - forecast day after tomorrow
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="1.0">
<xsl:output indent="yes" method="text" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:value-of select="//item/yweather:forecast[2]/@text" />
<xsl:text>, Low: </xsl:text>
<xsl:value-of select="//item/yweather:forecast[2]/@low" />
<xsl:text>°C, High: </xsl:text>
<xsl:value-of select="//item/yweather:forecast[2]/@high" />
<xsl:text>°C</xsl:text>
</xsl:template>
</xsl:stylesheet>
Installation
Community
- Support
- News Archive
- Presentations
- How to Contribute
- IDE Setup
- How to Implement a Binding
- How to Implement an Action
- Projects using openHAB
- User Interfaces
- Classic UI
- iOS Client
- Android Client
- GreenT UI
- CometVisu
- Bindings
- Asterisk Binding
- Astro Binding
- Bluetooth Binding
- Comfo Air Binding
- CUL Binding
- CUPS Binding
- digitalSTROM Binding
- DMX512 Binding
- EnOcean Binding
- Epson Projector Binding
- Exec Binding
- Fritz!Box Binding
- Fritz AHA Binding
- GPIO Binding
- HDAnywhere binding
- Heatmiser Binding
- Homematic Binding
- HTTP Binding
- IHC / ELKO Binding
- Insteon Hub Binding
- Insteon PLM Binding
- Ir-Trans Binding
- KNX Binding
- Koubachi Binding
- MAX!Cube-Binding
- MiLight Binding
- Modbus TCP Binding
- MPD Binding
- MQTT Binding
- MQTTitude binding
- Neohub Binding (Preview)
- Netatmo Binding
- Network Health Binding
- Nibe Heatpump Binding
- Nikobus Binding
- Novelan/Luxtronic Heatpump Binding
- NTP Binding
- One-Wire Binding
- Onkyo AV Receiver Binding
- Open Energy Monitor Binding
- OpenPaths presence detection binding
- OpenSprinkler Binding
- OSGi Configuration Admin Binding
- Philips Hue Binding
- Piface Binding
- Pioneer-AVR-Binding
- Plugwise Binding
- PLCBus Binding
- Pulseaudio Binding
- RFXCOM Binding
- Samsung TV Binding
- Serial Binding
- Snmp Binding
- Squeezebox Binding
- System Info Binding
- Somfy URTSI II Binding
- Sonos Binding
- Swegon ventilation Binding
- TCP/UDP Binding
- Tellstick Binding
- TinkerForge Binding
- VDR Binding
- Velleman-K8055-Binding
- Wake-on-LAN Binding
- Withings Binding
- XBMC Binding
- xPL Binding
- Z-Wave Binding
- Persistence
- db4o Persistence
- rrd4j Persistence
- MySQL Persistence
- MongoDB Persistence
- Sen.Se Persistence
- Cosm Persistence
- Logging Persistence
- Exec Persistence
- MQTT Persistence
- Automation
- Scripts
- Rules
- Actions
- Misc
- REST-API
- Security
- Google Calendar Support
- Twitter Action
- Service Discovery
- Dropbox Bundle
Samples
- Item definitions
- Sitemap definitions
- Binding configurations
- Rules
- REST Examples
- Tips & Tricks
- FAQ
- XSLT Transforms
- Scripts
- Integration with other applications
- Syntax highlighting for external editors
- Update-Scripts
- Samples-Comfo-Air-Binding
- Samples WAC Binding
Release Notes