summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/luatex/luaxml/luaxml.tex
blob: 34363fbcf04f63d00a0c8aa32d3860ad6fd27d65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
\documentclass{ltxdoc}
\usepackage{tgschola,url}
\usepackage[english]{babel}
\begin{document}
	\title{The \textsc{LuaXML} library}
	\author{Paul Chakravarti \and Michal Hoftich}
	\maketitle
\tableofcontents
\section*{Introduction}

|LuaXML| is pure lua library for reading and serializing of the |xml| files.
Current release is aimed mainly as support for the odsfile package. 
In first release it was included with the odsfile package,
but as it is general library which can be used also with other packages, 
I decided to distribute it as separate library.

\noindent Example of usage:

\begin{verbatim}
xml = require('luaxml-mod-xml')
handler = require('luaxml-mod-handler')
\end{verbatim} 

First load the libraries. In |luaxml-mod-xml|, there is xml parser and also serializer. In |luaxml-mod-handler|, there are various handlers for dealing with xml data. Handlers are objects with callback functions which are invoked for every type of content in the |xml| file. More information about handlers can be found in the original documentation, section \ref{sec:handlers}.
\begin{verbatim}
sample = [[
<a>
  <d>hello</d>
  <b>world.</b>
  <b at="Hi">another</b>
</a>]]
treehandler = handler.simpleTreeHandler()
x = xml.xmlParser(treehandler)
x:parse(sample)
\end{verbatim} 

You have to create handler object, using |handler.simpleTreeHandler()| and xml parser object using |xml.xmlParser(handler object)|. |simpleTreehandler| creates simple table hierarchy, with top root node in |treehandler.root|
\begin{verbatim}
-- pretty printing function
function printable(tb, level)
  level = level or 1
  local spaces = string.rep(' ', level*2)
  for k,v in pairs(tb) do
    if type(v) ~= "table" then
      print(spaces .. k..'='..v)
    else
      print(spaces .. k)
      level = level + 1
      printable(v, level)
    end
  end
end

-- print table
printable(treehandler.root)
-- print xml serialization of table
print(xml.serialize(treehandler.root))
-- direct access to the element
print(treehandler.root["a"]["b"][1])

-- output:
--   a
--     d=hello
--     b
--       1=world.
--       2
--         1=another
--         _attr
--           at=Hi
-- <?xml version="1.0" encoding="UTF-8"?>
-- <a>
--   <d>hello</d>
--     <b>world.</b>
--     <b at="Hi">
--       another
--     </b>
-- </a>
-- 
-- world.
\end{verbatim}

Note that |simpleTreeHandler| creates tables that can be easily accessed using standard lua functions, but in case of mixed content, like
\begin{verbatim}
<a>hello
  <b>world</b>
</a>	  
\end{verbatim}
it produces wrong results. It is useful mostly for data |xml| files, not for text formats like |xhtml|.

Because at the moment it is intended mainly as support for the odsfile package, there is little documentation, 
what follows is the original documentation of |LuaXML|, which may be little bit obsolete now.

\clearpage
\noindent{\Huge Original documentation}
\medskip

\noindent This document was created automatically from original source code comments using Pandoc\footnote{\url{http://johnmacfarlane.net/pandoc/}} 

\section{Overview}


This module provides a non-validating XML stream parser in Lua. 
\section{Features}

\begin{itemize}
\item
  Tokenises well-formed XML (relatively robustly)
\item
  Flexible handler based event api (see below)
\item
  Parses all XML Infoset elements - ie.
  \begin{itemize}
  \item
    Tags
  \item
    Text
  \item
    Comments
  \item
    CDATA
  \item
    XML Decl
  \item
    Processing Instructions
  \item
    DOCTYPE declarations
  \end{itemize}
\item
  Provides limited well-formedness checking (checks for basic syntax \&
  balanced tags only)
\item
  Flexible whitespace handling (selectable)
\item
  Entity Handling (selectable)
\end{itemize}
\section{Limitations}

\begin{itemize}
\item
  Non-validating
\item
  No charset handling
\item
  No namespace support
\item
  Shallow well-formedness checking only (fails to detect most semantic
  errors)
\end{itemize}
\section{API}

The parser provides a partially object-oriented API with functionality
split into tokeniser and hanlder components.

The handler instance is passed to the tokeniser and receives callbacks
for each XML element processed (if a suitable handler function is
defined). The API is conceptually similar to the SAX API but implemented
differently.

The following events are generated by the tokeniser

\begin{verbatim}
handler:start       - Start Tag
handler:end         - End Tag
handler:text        - Text
handler:decl        - XML Declaration
handler:pi          - Processing Instruction
handler:comment     - Comment
handler:dtd         - DOCTYPE definition
handler:cdata       - CDATA 
\end{verbatim}
The function prototype for all the callback functions is

\begin{verbatim}
callback(val,attrs,start,end)
\end{verbatim}
where attrs is a table and val/attrs are overloaded for specific
callbacks - ie.

\begin{tabular}{llp{5cm}}
Callback   &  val        &    attrs (table)\\
\hline
start     &   name &   |{ attributes (name=val).. }|\\
end       &   name   &    nil\\
text      &   |<text>| &   nil\\
cdata     &   |<text> |  &   nil\\
decl      &   "xml"       &   |{ attributes (name=val).. }|\\
pi        &   pi name     &  \begin{verbatim}{ attributes (if present)..
  _text = <PI Text>
}\end{verbatim}\\
comment   &   |<text>|      &   nil\\     
dtd       &   root element  & \begin{verbatim}{ _root = <Root Element>,
  _type = SYSTEM|PUBLIC,
  _name = <name>,
  _uri = <uri>,
  _internal = <internal dtd>
}\end{verbatim}\\
\end{tabular}

(start \& end provide the character positions of the start/end of the
element)

XML data is passed to the parser instance through the `parse' method
(Nore: must be passed a single string currently)

\section{Options}

Parser options are controlled through the `self.options' table.
Available options are -

\begin{itemize}
\item
  stripWS

  Strip non-significant whitespace (leading/trailing) and do not
  generate events for empty text elements
\item
  expandEntities

  Expand entities (standard entities + single char numeric entities only
  currently - could be extended at runtime if suitable DTD parser added
  elements to table (see obj.\_ENTITIES). May also be possible to expand
  multibyre entities for UTF--8 only
\item
  errorHandler

  Custom error handler function
\end{itemize}
NOTE: Boolean options must be set to `nil' not `0'

\section{Usage}

Create a handler instance -

\begin{verbatim}
h = { start = function(t,a,s,e) .... end,
      end = function(t,a,s,e) .... end,
      text = function(t,a,s,e) .... end,
      cdata = text }
\end{verbatim}
(or use predefined handler - see luaxml-mod-handler.lua)

Create parser instance -

\begin{verbatim}
p = xmlParser(h)
\end{verbatim}
Set options -

\begin{verbatim}
p.options.xxxx = nil
\end{verbatim}
Parse XML data -

\begin{verbatim}
xmlParser:parse("<?xml... ")
\end{verbatim}
\section{Handlers}\label{sec:handlers}

\subsection{Overview}

Standard XML event handler(s) for XML parser module (luaxml-mod-xml.lua)

\subsection{Features}

\begin{verbatim}
printHandler        - Generate XML event trace
domHandler          - Generate DOM-like node tree
simpleTreeHandler   - Generate 'simple' node tree
simpleTeXhandler    - SAX like handler with support for CSS selectros
\end{verbatim}
\subsection{API}

Must be called as handler function from xmlParser and implement XML
event callbacks (see xmlParser.lua for callback API definition)

\subsubsection{printHandler}

printHandler prints event trace for debugging

\subsubsection{domHandler}

domHandler generates a DOM-like node tree  structure with 
a single ROOT node parent - each node is a table comprising 
fields below.

\begin{verbatim}
node = { _name = <Element Name>,
        _type = ROOT|ELEMENT|TEXT|COMMENT|PI|DECL|DTD,
        _attr = { Node attributes - see callback API },
        _parent = <Parent Node>
        _children = { List of child nodes - ROOT/NODE only }
      }

\end{verbatim}
The dom structure is capable of representing any valid XML document
\subsubsection{simpleTreeHandler}

simpleTreeHandler is a simplified handler which attempts to generate a
more `natural' table based structure which supports many common XML
formats.

The XML tree structure is mapped directly into a recursive table
structure with node names as keys and child elements as either a table
of values or directly as a string value for text. Where there is only a
single child element this is inserted as a named key - if there are
multiple elements these are inserted as a vector (in some cases it may
be preferable to always insert elements as a vector which can be
specified on a per element basis in the options). Attributes are
inserted as a child element with a key of `\_attr'.

Only Tag/Text \& CDATA elements are processed - all others are ignored.

This format has some limitations - primarily

\begin{itemize}
\item  Mixed-Content behaves unpredictably - the relationship between text
  elements and embedded tags is lost and multiple levels of mixed
  content does not work
\item  If a leaf element has both a text element and attributes then the text
  must be accessed through a vector (to provide a container for the
  attribute)
\end{itemize}
In general however this format is relatively useful.


\subsection{Options}

\begin{verbatim}
simpleTreeHandler.options.noReduce = { <tag> = bool,.. }

    - Nodes not to reduce children vector even if only 
      one child

domHandler.options.(comment|pi|dtd|decl)Node = bool 

    - Include/exclude given node types
\end{verbatim}
\subsection{Usage}

Pased as delegate in xmlParser constructor and called as callback by
xmlParser:parse(xml) method.

\section{History}

This library is fork of LuaXML library originaly created by Paul
Chakravarti. Its original version can be found at
\url{http://manoelcampos.com/files/LuaXML--0.0.0-lua5.1.tgz}. Some files not
needed for use with luatex were droped from the distribution.
Documentation was converted from original comments in the source code.

\section{License}

This code is freely distributable under the terms of the Lua license
(\url{http://www.lua.org/copyright.html})
\end{document}