element.lsp

Module index



Module: Element

Author: Jeff Ober
Version: 1.0
Original location: http://static.artfulcode.net/newlisp/element.lsp
Source: element.lsp
Package definition: element.qwerty

A simple way of generating valid XML content (requires newlisp 10).

Often, XML modules attempt to provide everything but the kitchen sink when all that you really want is to write valid XML simply and quickly. Element is a purely practical module that lets you write XML fast and efficiently. It performs no validation and does nothing apart from serialize data to XML. If you need extra functionality like indentation or serialization of SXML, look at the XML module.

Version history

1.0 • initial release

Bugs and todos

• XML declaration function with encoding

- § -

Element:encode

syntax: (Element:encode str)
parameter: str - a string

Encodes characters in a string to be valid for XML.



- § -

Element:Element

syntax: (Element:Element str-tag attributes [str-content ...])
parameter: str-tag - the tag name
parameter: attributes - an association list of key/value pairs, a string, or nil
parameter: str-content - any number of strings

Builds an element tag with the name str-tag and attributes attributes around the content str-content, which generally is also built using this function. If attributes is an association list, it is converted to a series of key="value" pairs. If it is a string, it is inserted as-is. Otherwise, it is ignored. str-content may be strings generated by XmlBuilder:element or otherwise (in which case it is inserted as text content). Attribute values are encoded as needed (if an association list); str-content must be encoded if a text value.

example:
 (Element "div" '(("class" "content"))
   (Element "h2" nil (Element:encode "Welcome"))
   (Element "p" {class="message"} (Element:encode "Hello world.")))
 
 => <div class="content"><h2>Welcome</h2><p class="message">Hello world.</p></div>


- § -

Element:doc

syntax: (Element:doc str-encoding [str-content ...])
parameter: str-encoding - encoding for XML declaration
parameter: str-content - any number of strings

Adds an XML declaration to a string. If encoding is nil, defaults to UTF-8 when newlisp is compiled with UTF-8 support, ASCII otherwise.

example:
 (Element:doc "UTF-8" (Element "root" nil "Hello world."))
 =>
 <?xml version="1.0" encoding="UTF-8" ?>
 <root>Hello world.</root>

- ∂ -

Artful Code

generated with newLISP  and newLISPdoc