<html>
. HTML tags
normally come in matching pairs, such as <html>
and
</html>
(note the slash indicates the closing tag
of the pair). The material between two such tags is called an
element. Whitespace is generally ignored, except inside
indentifiers and quotes.
An HTML file is structured hierarchically as a nested set of elements. For example, the following is an HTML file that will display "Hello world!" in bold (with "Hello" in the title bar), when viewed in a browser:
<html> <head> <title>Hello</title> </head> <body> <b>Hello world!</b> </body> </html>
Elements may also optionally have one or more attributes, which are are specified using a keyword syntax in the opening tag, such as:
<form action="http://localhost:8088/hello">In this example, the
form
element has one attribute, named action
, whose value is the quoted string (which happens in this case to be a URL).
<html>
- toplevel element of HTML page
<head>
- header information for HTML page
<title>
- text for window title
<body>
- information to be displayed on page
<p>
- paragraph (separated by space before and after)
<b>
- display enclosed text in bold
<a href="...">
- hyperlink (href is url to link to)
<form action="...">
- display form (action is url to submit data)
<input type="text" name="...">
- display text box (used only inside form; name used to submit data)
<input type="submit" value="...">
- display submit button (used only inside form; value is button label)
xml
package which makes constructing HTML convenient. This package
is used in the simple http server that supports Lab 6
and Homework 7.
The basic idea of this package is to use symbols and nested lists instead of tags to construct the desired logical structure of the HTML page, and then only to convert the final nested structure to a string at the very end.
So for example, to wrap the bold tag around the string "Hello world!", instead of writing:
(string-append "<b>" "Hello world!" "</b>")you can simply write:
(list 'b "Hello world!")Similarly, instead of creating the string:
"<html> <body> ... </body> </html>"you can construct the list:
(list 'html (list 'body ...))These list forms are called x-expressions. You can see many more examples of using x-expressions in the simple http server code.
Attributes can also be optionally provided in x-expressions. If an element has attributes, these are specified as a initial sublist of attribute/value lists. For example, the following form element (from Hello.html)
<form action="http://localhost:8088/hello"> What is your first name? <input type="text" name="firstName"/> <input type="submit" value="Click Here"/> </form>would be constructed as follows:
(list 'form (list (list 'action "http://localhost:8088/hello")) "What is your first name?" (list 'input (list (list 'type "text") (list 'name "firstName"))) (list 'input (list (list 'type "submit") (list 'value "Click Here"))))yielding the following x-expression:
(form ((action "http://localhost:8088/hello")) "What is your first name?" (input ((type "text") (name "firstName"))) (input ((type "submit") (value "Click Here"))))
The function xexpr->string
converts
an x-expression into the corresponding XML/HTML string. (Notice where this function is used
in the server code.)
There is also an inverse function, string->xexpr
, which can sometimes be useful (see HTML tip at end of Homework 7).