Puzzled over maths with xslt

Spent some time to extract a number with an xslt transformation from an XML (outside temperature from an Eta boiler). This got ridiculously difficult and needed some heavy googling, so I thought I’d share the result.

The target value is 5.8 (value / scaleFactor)

<?xml version="1.0" encoding="utf-8"?>
<eta version="1.0" xmlns="http://www.eta.co.at/rest/v1">
  <value uri="/user/var/120/10241/0/0/12197" strValue="6,0" unit="°C" decPlaces="1" scaleFactor="10" advTextOffset="0">58</value>
</eta>

There is a namespace, so the xpath’s get ugly:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.eta.co.at/rest/v1">

	<xsl:output method="text" />

	<xsl:template match="/">

		<xsl:call-template name="div">
			<xsl:with-param name="x"
				select="/*[name()='eta']/*[name()='value']/text()" />
			<xsl:with-param name="y"
				select="/*[name()='eta']/*[name()='value']/@scaleFactor" />
		</xsl:call-template>

	</xsl:template>

	<xsl:template name="div">
		<xsl:param name="x" />
		<xsl:param name="y" />

		<xsl:value-of select="$x div $y" />
	</xsl:template>

</xsl:stylesheet>

The stylesheet goes into conf/transform/xsl/t.xsl

and the Thing def is

Thing http:url:eta  [
	baseURL="http://nico/~weberjn",
	refresh=15] {
		Channels:
            Type number : outside_temperature [ stateExtension="/temperature.xml",
			stateTransformation="XSLT:xsl/t.xsl" ]  }

It helps a lot to have the xml and xsl in an Eclipse project and run the xsl.

1 Like