# XML node engine

This chapter is dedicated to the XML simple model (xsm), a subpart of the standard xml processing module. The XML simple model is designed to simplify the interpretation of a general sgml document such like, html or xhtml document. In the simple model approach, there is no tree. Instead, a vector of simple nodes is built, and a document interface can be used to access the nodes. Therefore, this simple model should be considered as a mean to quickly parse document, but should not be used when tree operations come into play. In such case, the xml model is by far more appropriate. The simple model can be used to parse a html document for instance. Note also that the simple model is a relaxed model in terms of parsing rules. For example, the tag start/end consistency is not checked and the attribute parsing is not aggressive as it can be found generally in poorly written html document.

## Simple model node

In the simple model, a `XsmNode` is just a text place holder. The node transports its type which can be either text, tag, reference of end node. For the tag node, a subtype that identifies reserved nodes versus normal type is also available.

### Creating a node

A xsm node is created by name or byte and name. In the first case, the node is a text node. In the second case, the node subtype is automatically detected for tag node.

# create a xsm text node const ntxt (afnix:xml:XsmNode "afnix"> # create a xsm tag node const ntag (afnix:xml:XsmNode afnix:xml:XsmNode:TAG "afnix">

Note that the text corresponds to the node content. For example, the string "!-- example --" might corresponds to a comment in html which is to say a reserved tag when the type is tag or a simple text if the type is a text node. A reserved tag is defined by a string which start either with the '!' character or the '[' character.

# create a reserved tag const rtag (afnix:xml:XsmNode afnix:xml:XsmNode:TAG "!-- example --")

### Node representation

The xsm node is a literal node. This means that the `to-string` and `to-literal` methods are available. When the `to-literal` method is called, the node text is automatically formatted to reflect the node type.

# create a reserved tag const rtag (afnix:xml:XsmNode afnix:xml:XsmNode:TAG "!-- example --") # print the node literal rtag:to-literal # <!-- example -->

If the node is a reference node, the node literal is represented with the original definition while the `to-string` method will produce the corresponding character if it known.

### Node information

With a xsm node, the operation are a limited number of node information operations. The `get-name` method returns the first name found in a node. If the node is a normal tag, the `get-name` will return the tag name. For the other node, the method will return the first available string. This also means, that the method will behave correctly with end tag node.

# create a tag node const ntag (afnix:xml:XsmNode afnix:xml:XsmNode:TAG "afnix"> # get the tag name ntag:get-name

There is a predicate for all types. For example, the `text-p` predicate returns true if the node is a text node. The `tag-p` predicate returns true if the node is a normal or reserved tag.

## Document reading

A document is read in a way similar to the `XmlDocument` with the help of the `XsmDocument` object. Once created, the document holds a vector of nodes.

### The document object

The `XsmDocument` object is a special xsm object designed to ease the reading process of a document. The process of creating a document consists of creating a document object, then binding a xsm reader, parsing the input stream and storing the nodes in a vector. When the operation is completed, the vector can be accessed by index.

# create a xms document const xdoc (afnix:xml:XsmDocument "example.htm") # get the document length xdoc:length

### Node information object

The `XsoInfo` object is a node information object designed to hold a node name, an attributes list and eventually a text associated with the node. For example, if a html document contains a anchor node, the associated information node, will have the anchoring text stored as the node information text.

# create a xso node by name and text const info (afnix:xml:XsoInfo "a" "click here")

### Simple model operations

The `XsmDocument` is designed to perform simple operations such like searching all nodes that matches a particular name. While this operation can be done easily, it is done in such a way that a vector of node information is returned instead of a vector of nodes which can always be constructed with a simple loop.

# create a xsm document const xdoc (afnix:xml:XsmDocument "example.htm") # get all node named "a" - forcing lower case xdoc:get-info-vector "a" true