doc/us/lom.html

1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
<html>
4
<head>
5
	<title>LuaExpat: XML Expat parsing for the Lua programming language</title>
6
    <link rel="stylesheet" href="../doc.css" type="text/css"/>
7
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
8
</head>
9
<body>
10
 
11
<div id="container">
12
	
13
<div id="product">
14
	<div id="product_logo"><a href="http://www.keplerproject.org">
15
        <img alt="LuaExpat logo" src="luaexpat.png"/>
16
	</a></div>
17
	<div id="product_name"><big><strong>LuaExpat</strong></big></div>
18
	<div id="product_description">XML Expat parsing for the Lua programming language</div>
19
</div> <!-- id="product" -->
20
 
21
<div id="main">
22
 
23
<div id="navigation">
24
<h1>LuaExpat</h1>
25
	<ul>
26
		<li><a href="index.html">Home</a>
27
			<ul>
28
				<li><a href="index.html#overview">Overview</a></li>
29
				<li><a href="index.html#status">Status</a></li>
30
				<li><a href="index.html#download">Download</a></li>
31
				<li><a href="index.html#history">History</a></li>
32
				<li><a href="index.html#references">References</a></li>
33
				<li><a href="index.html#credits">Credits</a></li>
34
				<li><a href="index.html#contact">Contact</a></li>
35
			</ul>
36
		</li>
37
		<li><a href="manual.html">Manual</a>
38
			<ul>
39
				<li><a href="manual.html#introduction">Introduction</a></li>
40
				<li><a href="manual.html#installation">Installation</a></li>
41
				<li><a href="manual.html#parser">Parser Objects</a></li>
42
			</ul>
43
		</li>
44
		<li><a href="examples.html">Examples</a></li>
45
		<li><strong>Lua Object Model</strong></li>
46
        <li><a href="http://luaforge.net/projects/luaexpat/">Project</a>
47
            <ul>
48
                <li><a href="http://luaforge.net/tracker/?group_id=13">Bug Tracker</a></li>
49
                <li><a href="http://luaforge.net/scm/?group_id=13">CVS</a></li>
50
            </ul>
51
        </li>
52
		<li><a href="license.html">License</a></li>
53
	</ul>
54
</div> <!-- id="navigation" -->
55
 
56
<div id="content">
57
 
58
<h2><a name="introduction"></a>Introduction</h2>
59
 
60
<p>Lua Object Model (LOM) is a representation of XML elements
61
through Lua data types. Currently it is not supposed to be 100%
62
complete, but simple.</p>
63
 
64
<p>LuaExpat's distribution provides an implementation of LOM that
65
gets an XML documenta (a string) and transforms it to a Lua table.
66
The only function exported is <strong><code>lxp.lom.parse</code></strong>.</p>
67
 
68
 
69
<h2><a name="characteristics"></a>Characteristics</h2>
70
 
71
<p>The model represents each XML element as a Lua table. A LOM
72
table has three special characteristics:</p>
73
 
74
<ul>
75
    <li>a special field called <strong><code>tag</code></strong> that holds the
76
    element's name;</li>
77
 
78
    <li>an optional field called <strong><code>attr</code></strong> that stores
79
    the element's attributes (see <a href="#attributes">attribute's
80
    section</a>); and</li>
81
    
82
    <li>the element's children are stored at the <em>array-part</em> of
83
    the table. A child could be an ordinary string or another XML
84
    element that will be represented by a Lua table following these
85
    same rules.</li>
86
</ul>
87
 
88
 
89
<h3><a name="attributes"></a>Attributes</h3>
90
 
91
<p>The special field <strong><code>attr</code></strong> is a Lua table that
92
stores the XML element's attributes as pairs
93
<em>&lt;key&gt;=&lt;value&gt;</em>. To assure an order (if
94
necessary), the sequence of <em>key</em>s could be placed at the
95
<em>array-part</em> of this same table.</p>
96
 
97
 
98
<h2><a name="examples"></a>Examples</h2>
99
 
100
<p>For a simple string like</p>
101
 
102
<pre class="example">
103
    s = [[&lt;abc a1="A1" a2="A2"&gt;inside tag `abc'&lt;/abc&gt;]]
104
</pre>
105
 
106
<p>A call like</p>
107
 
108
<pre class="example">
109
    tab = lxp.lom.parse (s))
110
</pre>
111
 
112
<p>Would result in a table equivalent to</p>
113
 
114
<pre class="example">
115
tab = {
116
        ["attr"] = {
117
                [1] = "a1",
118
                [2] = "a2",
119
                ["a2"] = "A2",
120
                ["a1"] = "A1",
121
        },
122
        [1] = "inside tag `abc'",
123
        ["tag"] = "abc",
124
}
125
</pre>
126
 
127
<p>Now an example with an element nested inside another element</p>
128
 
129
<pre class="example">
130
tab = lxp.lom.parse(
131
[[&lt;qwerty q1="q1" q2="q2"&gt;
132
    &lt;asdf&gt;some text&lt;/asdf&gt;
133
&lt;/qwerty&gt;]]
134
)
135
</pre>
136
 
137
<p>The result would have been a table equivalent to</p>
138
 
139
<pre class="example">
140
tab = {
141
        [1] = "\
142
        ",
143
        [2] = {
144
                ["attr"] = {
145
                },
146
                [1] = "some text",
147
                ["tag"] = "asdf",
148
        },
149
        ["attr"] = {
150
                [1] = "q1",
151
                [2] = "q2",
152
                ["q2"] = "q2",
153
                ["q1"] = "q1",
154
        },
155
        [3] = "\
156
",
157
        ["tag"] = "qwerty",
158
}
159
</pre>
160
 
161
<p>Note that even the <em>new-line</em> and <em>tab</em> characters are stored 
162
on the table.</p>
163
 
164
</div> <!-- id="content" -->
165
 
166
</div> <!-- id="main" -->
167
 
168
</div> <!-- id="container" -->
169
 
170
</body>
171
</html>