| ||||||||||||||
| ||||||||||||||
|
||||||||||||||
Quick'n'easy HomeSeer Device Formatting Using XSLI was looking for a quick and simple way to format by HomeSeer device listings. HomeSeer 1.6 uses an XML-based device list (which is good), but it is simply a result of ADO 2.5's XML save feature (which is bad), so some rather horrible XML is produced. Nevertheless, it is valid XML and can be used by us to retrieve our device data.
XML and XSLThis is not a tutorial on how to write/read XML and XSL. You can learn about XML, XSL and ASP (the 3 technologies used here) on W3Schools, where I learnt how to do all this! In short though, XML (eXtensible Markup Language) can be used to store data, where XSL (eXtensible Stylesheet Language) is used to format them. XSL is made up of 3 different technologies too: XSLT, XPath and XSL Formatting Objects - so it is all fairly complicated stuff if you want to get your teeth into it.Luckily, for our purposes it is relatively simple. The HomeSeer XML file (<HomeSeer>\config\sample.xml) is broken up into two sections. The first deals with the various data types (the schema of the file). This is one of the primary reasons the file looks so horrible. We don't care about this section. The second part is the data, <rs:data>, and this holds all your devices and events. Here is a partial line from my XML file: <z:row record_type='1' can_dim='True' dc='1' dev_type_string='Lamp Module' hc='A' interface='' iomisc='' iotype='0' location='Unknown' misc='0' name='Main light' ref='47312' sldim='100' status='0' .../> You can see it is a lamp module called "Main light" and has the code A1. What we now want to do is take out all the important data and format it into a nice-looking HTML page. This is where XSL comes in.
XSLHere is the XSL that we will used:<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<table border="0" cellspacing="1" cellpadding="4" class="tblMain">
<tr>
<th>Code</th>
<th>Name</th>
<th>Location</th>
</tr>
<xsl:for-each select="//rs:data">
<xsl:apply-templates />
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="//z:row">
<xsl:if test="@record_type[.='1']">
<tr>
<td class="code"><xsl:value-of select="@hc"/><xsl:value-of select="@dc"/></td>
<td><xsl:value-of select="@name"/> (<xsl:value-of select="@dev_type_string"/>)</td>
<td><xsl:value-of select="@location"/></td>
</tr>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Basically, the initial xsl:template matches all parts of the XML file. Then though, we apply the templates to
our rs:data rows. If you look down to the xsl:template section, you can see we check whether the record_type is 1 (a device).
If so, we print a row in the table with the device code, name and location.
ASP ScriptThe problem now is that we have an XML file, an XSL file, but no way to tie it together. If an XML file is to use an XSL stylesheet, it should specify it in its header. Unfortunately, the default ADO scheme has no such facility, so we must force one. We do this using HomeSeer 1.6's new ASP facility. Thankfully, it is very simple since we will use Microsoft's XMLDOM object:
<%
' Change the HomeSeer path to suit yours.
Dim xml, xsl
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("C:\Program Files\HomeSeer\config\sample.xml"))
Set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("C:\Program Files\HomeSeer\config\homeseer.xsl"))
Response.Write(xml.transformNode(xsl))
%>
This does all the loading, formatting, and displaying of the XML file!
Additionally, if you look closely at the XSL file, you'll notice we put in a few CLASS statements allowing
us to format the colour, font face (etc.) even further using CSS. What I did in my ASP file was to wrap
the above ASP script in HTML with some additional CSS formatting to make the table look a little prettier:
ConclusionDownload the zip file below, put homeseer.xsl in the same directory as your XML file, and the ASP file in your html directory. Load the file into your web browser (by default, http://localhost:8080/displayState.asp) and hopefully everything should work!
Submitted: 22/12/2002 Article content copyright © James Matthews, 2002.
|
|
|||||||||||||
All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -