Any XSLT gurus out there? How to convert hex -> decimal...

616 views
Skip to first unread message

Ben Jones

unread,
May 27, 2014, 3:52:37 AM5/27/14
to ope...@googlegroups.com
Hi,

I have a solar inverter that reports usage in hex (via an inbuilt web server), e.g.

<response>
  <EnergyToday>049A</EnergyToday>
  <EnergyYesterday>011E</EnergyYesterday>
  <EnergyLifetime>0001346E</EnergyLifetime>
</response>

I want to query this using the HTTP binding and convert the values to decimal for displaying in openHAB.

I have scoured Google and have found a few examples that claim to do this, but none of them work.

Are there are XSLT experts out there in openHAB-land that might be able to help with this?

Many thanks!
Ben

Jayesh Patel

unread,
May 27, 2014, 1:11:10 PM5/27/14
to ope...@googlegroups.com
try following stylesheet code which will give you output like this:

049A= 1178
011E= 286
0001346E= 78958

Stylesheet code:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0" xmlns:user="http://www.openHab.com">
  <xsl:template match="/">
    <html>
      <head>
        <title></title>
      </head>
      <body>
        <xsl:variable name="engToday" select="//EnergyToday"/>
        <xsl:value-of select="$engToday"/>=
        <xsl:value-of select ="user:GetLongValFromHexString(string($engToday))"/>
        <br/>
        <xsl:variable name="engYesterday" select="//EnergyYesterday"/>
        <xsl:value-of select="$engYesterday"/>=
        <xsl:value-of select ="user:GetLongValFromHexString(string($engYesterday))"/>
        <br/>
        <xsl:variable name="engLifetime" select="//EnergyLifetime"/>
        <xsl:value-of select="$engLifetime"/>=
        <xsl:value-of select ="user:GetLongValFromHexString(string($engLifetime))"/>
        <br/>
      </body>
    </html>
  </xsl:template>

  <msxsl:script language="JScript" implements-prefix="user">
    function GetLongValFromHexString(hexString)
    {
      var decimalVal = parseInt('0x' + hexString);
      return decimalVal;
    }
  </msxsl:script>
</xsl:stylesheet>

Ben Jones

unread,
May 27, 2014, 4:42:51 PM5/27/14
to ope...@googlegroups.com
Thanks for that Jayesh!

Unfortunately when I try to run this in an online XSLT tester I get the following error...

Error: Cannot find a matching 1-argument function named {http://www.openHab.com}GetLongValFromHexString()

Any idea what could be wrong? Sorry I am very much a noobie when it comes to XSLT...

Jayesh Patel

unread,
May 27, 2014, 9:17:08 PM5/27/14
to ope...@googlegroups.com
Not sure why it gives that error as I see that all calls to that function has one parameter and function body also has one parameter.
Do you mind sending link to that online tester page so I can try to make it work and paste modified code back here?
Thanks

Ben Jones

unread,
May 27, 2014, 9:19:56 PM5/27/14
to ope...@googlegroups.com
I tried a few, but this is the one I use the most, although it doesn't give any error messages;


This is the one that gave the error message I posted (from memory);


I could get it to work using this one;


But only if I selected the .NET4 XSLT processor option.

Ben Jones

unread,
Jun 1, 2014, 6:37:15 PM6/1/14
to ope...@googlegroups.com
Hi Jayesh,

Have you had any luck with that hex-2-decimal function?

I am still struggling to get anything to work.

Cheers,
Ben

Jayesh Patel

unread,
Jun 1, 2014, 10:50:12 PM6/1/14
to ope...@googlegroups.com
No luck, tried many things, sorry. 
Still if find any solution will post here.

Ben Jones

unread,
Jun 1, 2014, 11:46:09 PM6/1/14
to ope...@googlegroups.com
May have to just convert manually in my openHAB rules. Thanks for trying.

Ben Jones

unread,
Jul 17, 2014, 1:10:04 AM7/17/14
to ope...@googlegroups.com
Anyone else out there got any clever ideas about how to do this?

I want to convert the values below into decimals, using an XSLT transfer (via an HTTP binding);

  <response>
  <EnergyToday>0281</EnergyToday>
  <EnergyYesterday>0000</EnergyYesterday>
  <EnergyLifetime>00000281</EnergyLifetime>
  <HoursExportedToday>400</HoursExportedToday>
  <HoursExportedYesterday>0</HoursExportedYesterday>
  <HoursExportedLifetime>00000190</HoursExportedLifetime>
  <DaysProducing>0000</DaysProducing>
</response>
Reply all
Reply to author
Forward
0 new messages