XML Processing in LAML

Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark


Abstract

Index References Contents
This lecture describes XML in LAML. The lecture was is given in Görlitz, Germany, on December 15 2005.


Introduction

Where is Denmark?
Slide Note Contents Index
References 

Figure. The location of Denmark in Europe

Where is Aalborg?
Slide Note Contents Index
References 

Figure. The location of Aalborg in Denmark

Plan of this talk
Slide Note Contents Index
References 

  • XML fragments in relation to program fragments

  • LAML basics

  • Authoring of static web content in LAML

    • Example throughout: Food recipes

  • Review of existing XML languages in LAML

  • Related work

  • Conclusions


XML fragments and Program Fragments

Mixing XML and program fragments
Slide Note Contents Index
References 

As an overall interest we will analyze how to mix XML markup and programming notation in a single document

  • Markup hosting

  • Program hosting

  • Markup subsumption

  • Program subsumption

Markup hosting
Slide Note Contents Index
References 

Program fragments appear as constituents in a complete XML document

Program: An short example of a JSP document. The table part of the example is taken from www.jsptut.com
<html>
  <head> <title> JSP Example </title> </head>
  <body
    <table border=2>
    <%
        for ( int i = 0; i < n; i++ ) {
            %>
            <tr> <td>Number</td>
                 <td><%= i+1 %></td>
            </tr>
            <%
        }
    %>
    </table>
  </body>
</html>

Program hosting
Slide Note Contents Index
References 

XML fragments appear as constituents in a complete program

Program: An example of a CGI program. The example is taken from a CGI tutorial in C: http://www.cs.tut.fi/~jkorpela/forms/cgic.html
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  char *data;
  long m,n;
  printf("%s%c%c\n",
    "Content-Type:text/html;charset=iso-8859-1",13,10);
  printf("<TITLE>Multiplication results</TITLE>\n");
  printf("<H3>Multiplication results</H3>\n");
  data = getenv("QUERY_STRING");
  if(data == NULL)
    printf("<P>Error! Error in passing data from form to script.");
  else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
    printf("<P>Error! Invalid data; Data must be numeric.");
  else
    printf("<P>The product of %ld and %ld is %ld.",m,n,m*n);
  return 0;
}

Markup subsumption
Slide Note Contents Index
References 

Program fragments are represented in XML markup

An XML-only solution

Program: A factorial function programmed in the XML language XEXPR. The examples is taken from http://www.w3.org/TR/xexpr/
<define name="factorial" args="x">
 <if>
   <lt><x/>2</lt>
   <x/>
   <multiply>
     <x/>
     <factorial><subtract><x/>1</subtract></factorial>
   </multiply>
 </if>
</define>

<html>
  <head> <title> Factorial </title> </head>
  <body>
    <p> factorial(10) is <factorial> 10 </factorial> </p>
  </body>
</html>

Program subsumption
Slide Note Contents Index
References 

The XML fragments are represented as program fragments in the programming language

A program-only solution

Program: An example of a LAML document written in Scheme.
(load (string-append laml-dir "laml.scm"))
(laml-style "simple-xhtml1.0-transitional-validating")
(set-xml-accept-extended-contents-in 'xhtml10-transitional #t)

(define (factorial x)
  (if (< x 2)
      x 
      (* x (factorial (- x 1)))))

(write-html '(raw prolog)
 (html 
   (head 
     (title "Factorial"))
  (body 
     (p "factorial(10) is" (factorial 10))))
)

(end-laml)


LAML Basics

LAML Basics
Slide Note Contents Index
References 

LAML means Lisp Abstracted Markup Language

  • LAML provides a Scheme mirror library for a given XML language -
    defined by an XML DTD

    • Each element in the XML language gives rise to a function in Scheme

    • A mirror function returns an abstract syntax tree

    • A mirror function has a very flexible parameter list

    • A mirror function validates a document fragment at document generation time

Reference

We will do some life playing with the a simple HTML document to expore the way LAML is used in practice


Authoring of Static Web Contents

Recipes
Slide Note Contents Index
References 

As an example, we want to publish food recipes as HTML pages on the web

  • Approaches:

    • Write/generate ordinary HTML pages

    • Carry out some ad hoc abstractions on top of the HTML details

    • Define a recipe datastructure and functions that process the datastructure

    • Establish a new recipe language and write processors for it

Simple HTML recipe pages
Slide Note Contents Index
References 

Using XHTML in LAML

Program: A single recipe.
(div 'css:margin-bottom "2cm"
     (span (b "Recipe:") "Pita bread") (br)
     "Ingredients:"
     (ul
      (li "1.5 tsp dry yeast") (li "1 cup water")
      (li "1 tbs honey")       (li "1.5 tsp salt")
      (li "3 cups flour")
     )

     "Procedure:" 
     (ol
      (li "Dissolve the yeast")  (li "Add honey, folur and salt")
      (li "Oil the mixing bowl") (li "Punch the dough down")
      (li "Roll each ball to 0.5 inch thickness")
    ))

Program: Two recipes in context of the entire document.
(load (string-append laml-dir "laml.scm"))
(laml-style "simple-xhtml1.0-transitional-validating")
(lib-load "xhtml1.0-convenience.scm")

(define laml-generation-meta (meta 'name "Generator" 'content "LAML"))
(define meta-props (list 'http-equiv "Content-Type" 'content "text/html; charset=iso-8859-1"))
(define html-props (list 'xmlns "http://www.w3.org/1999/xhtml"))

(define ttl "My Favorite Recipies")

(write-html '(pp prolog)
 (html html-props
  (head 
   (meta meta-props) laml-generation-meta
  (title ttl))
  (body 
    (h1 ttl)

    (div 'css:margin-bottom "2cm"
     (span (b "Recipe:") "Pita bread") (br)
     "Ingredients:"
     (ul
      (li "1.5 tsp dry yeast") (li "1 cup water")
      (li "1 tbs honey")       (li "1.5 tsp salt")
      (li "3 cups flour")
     )

     "Procedure:" 
     (ol
      (li "Dissolve the yeast")  (li "Add honey, folur and salt")
      (li "Oil the mixing bowl") (li "Punch the dough down")
      (li "Roll each ball to 0.5 inch thickness")
    ))

    (div 'css:margin-bottom "2cm"
     (span (b "Recipe:") "Apricot Compote") (br)
     "Ingredients:" (br)
     (ul
      (li "3 cups dried pears")
      (li "0.5 cups maple suryp")
      (li "1 cup water")
      (li "dash of salt")
      (li "2 cups heavy cream")
      )
     "Procedure:" (br)
     (ol
      (li "Cook the pears, sury and water")
      (li "Simer over low heat until soft")
      (li "Add salt")
      (li "Pure'e in blender")
      (li "Cool completely")
      (li "Add whiped cream and fold")
     ))))
)


(end-laml)

Reference

Commonalities are not shared

The level of abstraction is low

Recipe abstractions on top of HTML
Slide Note Contents Index
References 

Introducing simple Scheme functions on top of the HTML clauses

Program: The recipe function.
(define (recipe title ingredient-list step-list comment)
  (div 'css:margin-bottom "2cm"
     (span "Recipe:" title) (br)
     "Ingredients:" (br)
     (ul (map li ingredient-list))
     "Procedure:" (br)
     (ol (map li step-list))
     (font 'size "1" comment)))

Program: A call of the show-recipe function.
(recipe      
       "Pita Bread" 
       (list "1.5 tsp dry yeast" "1 cup water" "...")
       (list "Dissolve the yeast" "Add honey, flour and salt" "...") 
       "")

Program: The entire document.
(load (string-append laml-dir "laml.scm"))
(laml-style "simple-xhtml1.0-transitional-validating")
(lib-load "xhtml1.0-convenience.scm")

(define laml-generation-meta (meta 'name "Generator" 'content "LAML"))
(define meta-props (list 'http-equiv "Content-Type" 'content "text/html; charset=iso-8859-1"))
(define html-props (list 'xmlns "http://www.w3.org/1999/xhtml"))

(define ttl "My Favorite Recipies")

; Poor recipe markup. Ordinary positional parameters. Pure lists. 
(define (recipe title ingredient-list step-list comment)
  (div 'css:margin-bottom "2cm"
     (span "Recipe:" title) (br)
     "Ingredients:" (br)
     (ul (map li ingredient-list))
     "Procedure:" (br)
     (ol (map li step-list))
     (font 'size "1" comment)))


(write-html '(pp prolog)
 (html html-props
  (head 
   (meta meta-props) laml-generation-meta
  (title ttl))
  (body 
    (h1 ttl)

    (recipe      
       "Pita Bread" 
       (list "1.5 tsp dry yeast" "1 cup water" "...")
       (list "Dissolve the yeast" "Add honey, flour and salt" "...") 
       "")      

    (recipe   
       "Apricot Compote" 
       (list "3 cups dried pears" "0.5 cups maple syrup" "...")
       (list "Cook the pears, suryp and water" "...") 
       (span "Tastes" (b "ugly") ))   
    
  )
 )
)


(end-laml)

Reference

Poor markup - Use of positional parameters - Use of lists as generic markup

Recipe abstractions on top of HTML
Slide Note Contents Index
References 

Introducing XML-in-LAML abstractions on top of the HTML clauses

Program: The recipe XML-in-LAML function.
(define recipe
  (xml-in-laml-abstraction  
    (lambda (c a)
      (let ((title (defaulted-get-prop 'title a "NO TITLE"))
            (ingredient-list (get-asts c "ingredient"))
            (step-list (get-asts c "step"))
            (comment (get-asts c "comment")))
        (div 'css:margin-bottom "2cm" (span "Recipe:" title) (br)
             "Ingredients:" (br)      (ul (map li ingredient-list))
             "Procedure:" (br)        (ol (map li step-list))
             (font 'size "1" comment))))))

Program: An application of recipe.
(recipe 'title "Pita Bread"
     (ingredient "1.5 tsp dry yeast") (ingredient "1 cup water") (ingredient "...")
     (step "Dissolve the yeast") (step "Add honey, folur and salt")  (step "...")

Program: The entire document.
(load (string-append laml-dir "laml.scm"))
(laml-style "simple-xhtml1.0-transitional-validating")
(lib-load "xhtml1.0-convenience.scm")

(define laml-generation-meta (meta 'name "Generator" 'content "LAML"))
(define meta-props (list 'http-equiv "Content-Type" 'content "text/html; charset=iso-8859-1"))
(define html-props (list 'xmlns "http://www.w3.org/1999/xhtml"))

(define ttl "My Favorite Recipies")

(define (mark-as m)
  (xml-in-laml-abstraction 
    (lambda (c a)
      (span c 'class m))))

(define ingredient (mark-as "ingredient"))
(define step (mark-as "step"))
(define comment (mark-as "comment"))

(define (get-asts content-list what)
  (filter
    (lambda (x) (and (ast? x) (equal? (ast-element-name x) "span") (equal? (ast-attribute x 'class) what)))
    content-list))

(define recipe
  (xml-in-laml-abstraction  
    (lambda (c a)
      (let ((title (defaulted-get-prop 'title a "NO TITLE"))
            (ingredient-list (get-asts c "ingredient"))
            (step-list (get-asts c "step"))
            (comment (get-asts c "comment")))
        (div 'css:margin-bottom "2cm" (span "Recipe:" title) (br)
             "Ingredients:" (br)      (ul (map li ingredient-list))
             "Procedure:" (br)        (ol (map li step-list))
             (font 'size "1" comment))))))

(write-html '(pp prolog)
 (html html-props
  (head 
   (meta meta-props) laml-generation-meta
  (title ttl))
  (body 
    (h1 ttl)

    (recipe 'title "Pita Bread"
     (ingredient "1.5 tsp dry yeast") (ingredient "1 cup water") (ingredient "...")
     (step "Dissolve the yeast") (step "Add honey, folur and salt")  (step "..."))

    (recipe 'title "Apricot Compote"
     (ingredient "3 cups dried pears") (ingredient "0.5 cups maple suryp") (ingredient "...")
     (step "Cook the pears, sury and water") (step "...") 
     (comment "Tastes" (b "ugly") ))

  )
 )
)


(end-laml)

References

Better markup - Better parameter passing - Manual programming

Recipe datastructure (1)
Slide Note Contents Index
References 

Representing recipes as lists in Scheme

Program: A recipe list structure.
   '(recipe
      "Pita Bread"
      (("dry yeast" "1.5" "tsp")
       ("water" "1" "cup")
       ("..." "1" "..."))
      ("Dissolve the yeast"
       "Add honey, folur and salt" "..."))  

Recipe datastructure (2)
Slide Note Contents Index
References 

Representing recipes as lists in Scheme

Program: A recipe list structure.
(define (present-recipe r)
  (div 'class "recipe"
    (h3 (title-of-recipe r))
    (b "Ingredients")
    (ul      
      (map 
        (compose li present-ingredient) 
        (ingredient-list-of-recipe r)))
    (b "Procedure:")
    (ol(map li (steps-of-recipe r)))))

Program: Entire document.
(load (string-append laml-dir "laml.scm"))
(laml-style "simple-xhtml1.0-transitional-validating")

(define current-xml-language 'xhtml10-transitional)
(define laml-generation-meta (meta 'name "Generator" 'content "LAML"))
(define meta-props (list 'http-equiv "Content-Type" 'content "text/html; charset=iso-8859-1"))
(define html-props (list 'xmlns "http://www.w3.org/1999/xhtml"))

; Selectors
(define title-of-recipe second)
(define ingredient-list-of-recipe third)
(define steps-of-recipe fourth)

(define name-of-ingredient first)
(define amount-of second)
(define unit-of third)

(define (present-recipe r)
  (div 'class "recipe"
    (h3 (title-of-recipe r))
    (b "Ingredients")
    (ul      
      (map 
        (compose li present-ingredient) 
        (ingredient-list-of-recipe r)))
    (b "Procedure:")
    (ol(map li (steps-of-recipe r)))))

(define (present-ingredient ingr)
  (span (amount-of ingr) (unit-of ingr) (name-of-ingredient ingr)))


(define my-recipes
 (list
   '(recipe
      "Pita Bread"
      (("dry yeast" "1.5" "tsp")
       ("water" "1" "cup")
       ("..." "1" "..."))
      ("Dissolve the yeast"
       "Add honey, folur and salt" "..."))    

   '(recipe
      "Apricot Compote"
      (("pears" "3" "cups")
       ("maple surup" "0.5" "cup")
       ("..." "1" "..."))
      ("Cook the pears, sury and water"
      "...")) ))  


(write-html '(pp prolog)
 (html html-props
  (head 
   (meta meta-props) laml-generation-meta
   (title "My Recipes"))
  (body 
    (map present-recipe my-recipes))
 )
)


(end-laml)

Reference

A raw list approach - Easy to make errors in list structure - misspelling and structural errors

A Recipe XML language
Slide Note Contents Index
References 

Given a recipe XML language we whish to establish a mirror of the XML language in Scheme, and to show recipes on the web.

The recipe XML DTD and the recipe example document are due to Anders Møller and Michael Schwartzbach from Aarhus University

The Manual DTD, documented on this page, is taken from Anders Møller and Michael Schwartzbachs forthcoming book on Internet technology.

Programmatic Authoring
Slide Note Contents Index
References 

By writing documents directly in a programming language you have excellent possibilities to fight complexity at any location in the document, and at any time in the development process

  • Complexity fighting means

    • Introduction of functional abstractions

    • Avoiding (repetitive) manual work by programmed solutions

  • Can documents in natural languages really be written in a programming language?

    • Textual contributions in quoted strings

    • Implicit concatenation, implicit list flattening, adequate white space handling

    • Good editor support for common editing tasks


Existing XML-in-LAML languages

Basic Mirrors
Slide Note Contents Index
References 

LAML supports mirrors of XHTML1.0 (strict, transitional, frameset), XHTM1.1, and SVG1.1

Given an XML DTD it is usually straightforward to generate a mirror of it in LAML

LENO
Slide Note Contents Index
References 

These slides are made with the LENO system

LENO is an XML language for programmatic authoring of lecture notes

  • LENO features:

    • Several different views

    • Especially oriented towards slides with program examples

    • Allows the author to grow a text book from a set of slides

    • And more...

Program: The XML-in-LAML Scheme source of this page.
(note-page 'id "leno"
  (title (main-text "LENO") )

  (point
   (main-text
    (p "These slides are made with the LENO system ")
    (p "LENO is an XML language for programmatic authoring of lecture notes")
    )
  )

  (items 'margin-top "1.5cm"
   (item 
    (main-text "LENO features:" )
    (items 
     (item 
      (main-text "Several different views" ))
     (item 
      (main-text "Especially oriented towards slides with program examples" ))
     (item 
      (main-text "Allows the author to grow a text book from a set of slides" ))
     (item 
      (main-text "And more..."
  )))))

  (source-program 'margin-top "1.5cm"
   'src (in-startup-directory "xp.leno")
   'from-mark "(note-page 'id \"leno\""
   'to-mark end-of-slide-marker
   'slide-mode "external"
   'book-mode "inline"
   (color-decorations )
   (main-text "The XML-in-LAML Scheme source of this page" )
  )

  (cross-references 
    (internet-reference 'href "http://www.cs.aau.dk/~normark/c-prog-05/html/notes/index.html" 'target "c-prog"
                        (main-text "C Programming Material (in Danish)" ))
    (internet-reference 'href "http://www.cs.aau.dk/~normark/leno/index.html" 'target "c-prog"
                        (main-text "The LENO Home page (WEB)"))
  )

) ; end leno slide

References

XML Languages for Program Documentation
Slide Note Contents Index
References 

SchemeDoc is a language and tool for description and extraction of interface documentation

The Scheme Elucidator is language and tool for internal program documentation

Educational XML languages
Slide Note Contents Index
References 

In addition to LENO, LAML supports a number of additional XML languages for educational purposes

Hobby Languages
Slide Note Contents Index
References 

For my own pleasure I have made XML languages for photo management and songs with chords


Ending

Related Work
Slide Note Contents Index
References 

There are a number of other people who have done similar work in Scheme

There are many other people who have done similar work in other functional programming languages

  • Scribe and Skribe by Serrano and Gallesio

    • Supports its own document language, with several backends

    • Makes use of a special read syntax for "semi-constant strings"

    • Inspirred, in part, by LAML

  • SXML by Oleg Kiselyov

    • A list format for XML

    • Supported by a parser and transformation software (SXSLT)

  • WebIt by Jim Bender

    • Relies on SXML

    • Does also support CSS in Scheme

  • BRL by Bruce Lewis

    • For server side web applications

    • Relies on a non-standard Scheme reader

Status
Slide Note Contents Index
References 

  • Recent LAML papers

    • "Web Programming in Scheme with LAML", Journal of Functional Programming, January 2005

    • "Scheme Program Documentation tools", The Scheme workshop 2004

    • "XML Transformations in Scheme with LAML - a minimalistic approach", International Lisp Conference, 2003

  • New distributions on a regular basis

LAML is free and open source available from

http://www.cs.aau.dk/~normark/laml/


Collected references
Contents Index
More details
HTML recipe document
HTML recipe document
HTML recipe document
xml-in-laml-abstraction documentation
HTML recipe document
The LENO Home page (WEB)
C Programming Material (in Danish)

 

XML Processing in LAML
Course home     Author home     About producing this web     Previous lecture (top)     Next lecture (top)     Previous lecture (bund)     Next lecture (bund)     
Generated: December 19, 2005, 14:23:06