XML Compiler

 

XML Compiler Overview

The XML compiler is a Lisp function Library designed as a tiny XML compiler for a subset of SAX and DOM compliant XML implementations. The xml function is not, itself, a full SAX and DOM compliant XML compiler; instead, allows applications to read and write a subset of standards compliant XML without the size and expense of a full SAX and DOM standards compliant XML compiler. The XML compiler allows applications to store their data in XML compliant format. XML documents written by the XML compiler can always be read by full standards compliant compilers.

The xml Function implements an XML compiler via the method of recursive descent. The xml function is a tiny XML compiler and not a parser such as the lisp or javaScript functions. The result of running the xml function on an XML input string is a completed XML document tree model.

Document Tree Models

The XML document tree model is a recursive Structure with the data inside the XML document addressable by attribute as well as by numeric index. For example the following XML input string:

<?xml version = '1.0' standalone='yes' encoding = 'hello' ?>
		<!-- This is a dynamic name and address example -->
		<Address FirstName = "yes" LastName = 'yes'> 
		This is just general content for the Address element.
		<FirstName>Michael</FirstName>
		<LastName>Korns</LastName>
		<Street>214 Shorebreaker</Street>
		<City>Laguna Niguel</City>
		<State>California</State>
		This is more content for the Address element.
		</Address>
		Returns the following XML document tree model:
		#{
		 __attlist: #{version: '1.0' standalone: 'yes' encoding: 'hello'}
			Address: #{
			    __attlist: #{FirstName: "yes" LastName: 'yes'}
				__content: "This is just general content for the Address element."
			    FirstName: "Michael"
			    LastName:  "Korns"
			    Street:    "214 Shorebreaker"
			    City:      "Laguna Niguel"
				State:     "California"
			   __content: "This is more content for the Address element."
			  }
		}			
		

Compiling a String

The xml function supports the compilation of a single source string and returning an XML document tree model.
Syntax: (xml [document] inputString)

document (optional) A Structure to be extended as an XML document in tree model form. After being extended, this structure is then returned as the result of XML compilation.
inputString A string containing the XML source to be compiled
Returns An XML document in tree model form

Syntax: (xml [document] inputLambda)

document (optional) A Structure to be extended as an XML document in tree model form. After being extended, this structure is then returned as the result of XML compilation.
inputLambda An Lambda which provides the input string, containing the XML source, to the xml function on demand (see the Input Source Providers chapter)
Returns An XML document in tree model form

Example1

(xml "<?xml version=?1??><Name>John Doe</Name>")
;; Returns
#{xml: true __attlist: #{version: 1} Name: "John Doe" }

Example2

(setq xmlDoc (new Structure:))
		(xml xmlDoc "<?xml version=?1??><Name>John Doe</Name><*%><City>San Francisco</City>")

Returns the following error message:

!Invalid element start tag:
		<Name>John Doe</Name><*%>
		            ^ error
		<City>San Francisco</City>
		!

However xmlDoc contains the XML document-in-progress at the time of the error:

''xmlDoc is
		#{xml: true __attlist: #{version: 1} Name: "John Doe"}

Note: Passing the optional, (xmlDoc) argument allows the xml compiler to return an error message and still allow the caller access to the XML document-in-progress as it was at the time of the error.

Compiling A String with PI

The xml function supports the compilation of a single source string with active process instruction handling and returning an XML document tree model. The second argument must be a Structure of active process instruction bindings (piStructure). If the xml compiler should encounter a process instruction, the pi target name is looked up in the piStructure. If found, the xml function invokes the specified processing Lambda to process the character data. If not found, the xml function saves the process instruction source, but attempts no processing.
Syntax: (xml [document] inputString piStructure)

document (optional) A Structure to be extended as an XML document in tree model form. After being extended, this structure is then returned as the result of XML compilation.
inputString A string containing the XML source to be compiled
piStructure A Structure containing the process instruction Lambdas bound to the active pi target names
Returns An XML document in tree model form

Syntax: (xml [document] inputLambda piStructure)

document (optional) A Structure to be extended as an XML document in tree model form. After being extended, this structure is then returned as the result of XML compilation.
inputLambda An Lambda which provides the input string, containing the XML source, to the xml function on demand (see the Input Source Providers chapter)
inputLambda A string containing the XML source to be compiled
piStructure A Structure containing the process instruction Lambdas bound to the active pi target names
Returns An XML document in tree model form

Example1

(xml "<?xml version=?1??><?noScript Hello there?>" 
			#{javaScript: (lambda(piStructure document this source) 
			          (eval (compile 
			                         (morph (javaScript source)) 
			                         (new Lambda: Pv: (new Structure: document: document this: this))
			                         ))})
			Returns #{xml: true
			        __attlist: #{version: 1}
			   noScript:  "Hello there"
			  }

Example2

(xml "<?xml version=?1??><?javaScript writeln(?Hello world?); ?>" 
			#{javaScript: (lambda(piStructure document this source) 
			      (eval (compile 
			            (morph (javaScript source)) 
			            (new Lambda: Pv: (new Structure: document: document this: this))
			       ))})
			;;Returns the structure
			#{xml: true __attlist: #{version: 1} javaScript:  "writeln(?Hello world?);"}
			;;and Displays
			"Hello World"

Note: Each processing instruction target name, in the piStructure, must be bound to a lambda value expecting four arguments: (piStructure) the piStructure itself so that scripts can add to the available processing instruction targets, (document) the xml document in progress, (this) the current xml element, and (source) the processing instruction content.

Compiling A String with event handling

The xml function supports the compilation of a single source string with event handling. No XML document tree model is returned. The second argument must be an Lambda or a Host Object which will handle document events (eventHandler). The eventHandler must support the methods exposed in the Chapter on Document Event Handlers.
Syntax: (xml inputString eventHandler)

inputString A string containing the XML source to be compiled
eventHandler An Lambda or Host Object which will handle document events as described in the Chapter on Document Event Handlers.
Returns An XML document in tree model form

Syntax: (xml inputLambda eventHandler)

inputLambda An Lambda which provides the input string, containing the XML source, to the xml function on demand (see the Input Source Providers chapter)
inputString A string containing the XML source to be compiled
eventHandler An Lambda or Host Object which will handle document events as described in the Chapter on Document Event Handlers.
Returns An XML document in tree model form

Example1

(xml "<?xml version=?1??><?javaScript writeln(?Hello world?);?>"  eventHandler)
		;;Sends the following events to eventHandler
		;;(eventHandler.startDocument)
		;;(eventHandler.startElement "xml" #{version: 1})
		;;(eventHandler.processingInstruction "javaScript"  "writeln(?Hello world?);")
		;;(eventHandler.endElement "xml")
		;;(eventHandler.endDocument)

Input Lambda

The xml function supports the compilation of a multiple input strings served from an input Lambda. This allows compilation of include files and other instances where multiple XML source strings must be compiled as a single XML document. The first argument must be an Lambda or a Host Object which will provide strings of complete XML source in response to the (moreSource) message. The inputLambda must support the moreSource method by returning either #void (if there is no more source) or the next input string in sequence.

Note:Each input string must be a complete XML fragment.
Syntax:(inputLambda.moreSource)

Returns An complete XML input string or #void

Example1

(inputLambda.moreSource)
		;;Returns the string
		"<?xml version=?1??><Name>John</Name>"

Note: Each input string must be a complete XML fragment. This means that each input string must end at an end tag boundary.