Skip to content
abinition edited this page Sep 23, 2014 · 6 revisions

Representing XHTML data structures within HS

Properly formatted HTML (XHTML) is a subset of XML. HS 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>

Representing XML data structures within HS

The data structures of HS 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\>**

The above XML utilizes:.

1. nested tags
2. attributes
3. namespaces, i.e. fab:lotid
4. empty tags, i.e. <customer /\>

In HS, 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 ;

When XML is stored within a HS data structure, you can manipulate the data very easily.

Describing a style sheet using HyperScript

    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"
                    }
                  }
                }
              }
            }
          }
        }
      }
    } ;
    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>
Clone this wiki locally