HyperScript and XML, HTML, and CGI
Data structures created with the HyperScript language can be described as fully formatted XML statements. This includes XHTML as well, which is a subset of XML. There are many useful applications that can utilize this capability, such as:
- Representing XML data structures within HyperScript
- HTML page generation using HyperScripts CGI scripts
- Processing forms using HyperScript
- E-diagnostics, sending SECS/GEM data over the internet
- SOAP messages using HyperScript
- Describing a style sheet using HyperScript
HTML page generation using HyperScripts CGI scripts
Properly formatted HTML (XHTML) is a subset of XML. HyperScript can manipulate XHTML in the same way as XML. The following example shows how an HTML page can be built programmatically.
html.body.p = "hello world" ; xdescribe html ;
produces:
<html>
<body>
<p>
hello world
</p>
</body>
</html>
The complete CGI script would look like this:
#!/local/bin/hs -gif
puts { "Content-type: text/html\n\n" } ;
html.body.p = "hello world" ;
xdescribe html ;
Representing XML data structures within HyperScript
The data structures of HyperScript allow it to create and manipulate XML
data. Consider the following XML data.
<fab:lotid
id="F12345.1"
xmlns:fab="http://www.w3.ord.TR/html4/">
<part>AZ00000.11</part>
<components qty="3">
<fab:wafer>F12345.1</fab:wafer>
<fab:wafer>F12345.2</fab:wafer>
<fab:wafer>F12345.3</fab:wafer>
</component>
<customer />
<comment font="Arial" size="12">
This is a comment.
</comment>
</fab:lotid>
To make things interesting, the above XML utilizes:.
- nested tags
- attributes
- namespaces, i.e. fab:lotid
- empty tags, i.e. <customer />
In HyperScript, the above XML can be written concisely as:
list fab:lotid = {
attr xmlns:fab="http://www.w3.org/TR/html4/",
attr id="F12345.1",
list part = { "AZ00000.11" },
list components = {
attr qty="3",
list wafer = { "F12345.1" },
list wafer = { "F12345.2" },
list wafer = { "F12345.3" }
},
list customer = {},
list comment = {
attr font="Arial",
attr size="12",
"This is a comment."
}
} ;
It can be regenerated as XML with the single statement:
xdescribe fab:lotid ;
Execute the HyperScript View the HyperScript code
When XML is stored within a HyperScript data structure, you can manipulate the data very easily. For example, the above data is processed by a simple HyperScript.
Execute the HyperScript View the HyperScript code
Describing a style sheet using HyperScript
A XML-rich style sheet, for example can be programmed in HyperScript quite
easily, like this:
list xsl:stylesheet = {
attr 'version'="1.0",
attr xmlns:xsl="http://www.w3.org/1999/XSL/Transform",
list xsl:template = {
attr match="/",
list html = {
list body = {
list table = {
attr border="1",
list tr = {
str th = "Title" ,
str th = "Artist"
},
list 'xsl:for-each' = {
attr select="catalog/cd",
list tr = {
list td = {
list 'xsl:value-of' = {
attr select="title"
}
},
list td = {
list 'xsl:value-of' = {
attr select="artist"
}
}
}
}
}
}
}
}
} ;
...and with the xdescribe() function in Hyperscript, you can create XML (and XHTML) documents
xdescribe xsl:stylesheet ;
...produces
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td>
<xsl:value-of select="title" />
</td>
<td>
<xsl:value-of select="artist" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>