-
Notifications
You must be signed in to change notification settings - Fork 2
/
package.lisp
285 lines (240 loc) · 7.94 KB
/
package.lisp
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
(defpackage :cxml-stp
(:use :cl)
(:nicknames :stp)
(:export #:*check-uri-syntax*
#:stp-error
#:node
#:parent
#:root
#:base-uri
#:detach
#:string-value
#:copy
#:serialize
;; #:query
#:map-children
#:do-children
#:list-children
#:nth-child
#:first-child
#:last-child
#:previous-sibling
#:next-sibling
#:find-child
#:find-child-if
#:child-position
#:child-position-if
#:number-of-children
#:count-children
#:count-children-if
#:filter-children
#:map-recursively
#:do-recursively
#:find-recursively
#:find-recursively-if
#:filter-recursively
#:document
#:make-document
#:document-element
#:parent-node
#:prepend-child
#:append-child
#:delete-child
#:delete-nth-child
#:insert-child
#:insert-child-before
#:insert-child-after
#:delete-child-if
#:delete-children
#:replace-child
;; #:named-node
#:local-name
#:namespace-prefix
#:namespace-uri
#:of-name
#:qualified-name
#:element
#:make-element
#:add-attribute
#:remove-attribute
#:find-attribute-named
#:find-attribute-if
#:attribute-value
#:list-attributes
#:with-attributes
#:map-attributes
#:find-namespace
#:find-local-namespace
#:find-extra-namespace
#:map-extra-namespaces
#:add-extra-namespace
#:remove-extra-namespace
#:attribute
#:make-attribute
#:value
#:rename-attribute
#:comment
#:make-comment
#:data
#:processing-instruction
#:make-processing-instruction
#:target
#:document-type
#:make-document-type
#:root-element-name
#:system-id
#:public-id
#:internal-subset
#:dtd
#:text
#:make-text
#:make-builder)
(:documentation
"STP is a data structure for well-formed XML documents.
@begin[Parsing and Serializing]{section}
To parse into STP, use an STP builder together with a function
generating SAX events:
@aboutfun{make-builder}
Serialize STP by sending SAX events for the tree to a sink:
@aboutfun{serialize}
@end{section}
@begin[Names and Namespace URIs]{section}
STP represents namespace-well-formed documents only. All functions
accepting names take a local-name and and a namespace URI as an argument.
Qualified names are accepted where it makes sense, and named nodes have
a namespace prefix that is taken into account for well-formedness checks.
There are two kinds of named nodes: @class{element} and @class{attribute}.
Their slots are:
@aboutfun{local-name}
@aboutfun{namespace-uri}
@aboutfun{namespace-prefix}
For @code{element}, all of the above can be changed using SETF (subject
to well-formedness checks).
For attribute, @fun{local-name} can be changed directly, while URI and
prefix always have to be changed in the same step using
@fun{rename-attribute}.
A node's qualified name can be be queried:
@aboutfun{qualified-name}
@code{of-name} is convenient when searching for elements or attributes:
@aboutfun{of-name}
@end{section}
@begin[Subclasses of Node]{section}
All STP nodes have a common superclass.
@aboutclass{node}
Documents and elements can have children:
@aboutclass{parent-node}
@aboutclass{document}
@aboutclass{element}
Attributes belong to an @code{element}:
@aboutclass{attribute}
Other kinds of nodes:
@aboutclass{comment}
@aboutclass{document-type}
@aboutclass{processing-instruction}
@aboutclass{text}
@end{section}
@begin[Creating nodes]{section}
Nodes are created using the following functions:
@aboutfun{make-attribute}
@aboutfun{make-comment}
@aboutfun{make-document}
@aboutfun{make-document-type}
@aboutfun{make-element}
@aboutfun{make-processing-instruction}
@aboutfun{make-text}
In addition, nodes can be copied including all their children:
@aboutfun{copy}
@end{section}
@begin[Listing Child Nodes]{section}
Nodes have an optional parent node and can have children.
@aboutfun{parent}
If a node has a @class{document} as its ancestor, it can be found using
the @fun{document} function.
@aboutfun{document}
Since the @code{parent} slot needs to be updated when children are added or
removed, the sequence of children is not exposed as a mutable Common
Lisp sequence.
@aboutfun{list-children}
@aboutfun{map-children}
@aboutmacro{do-children}
The following DOM-like functions are also offered:
@aboutfun{nth-child}
@aboutfun{first-child}
@aboutfun{last-child}
@aboutfun{previous-sibling}
@aboutfun{next-sibling}
A wide variety of sequence-related functions is offered that work
like the Common Lisp functions of the same name, but without the need
to call @fun{list-children} first:
@aboutfun{find-child}
@aboutfun{find-child-if}
@aboutfun{child-position}
@aboutfun{child-position-if}
@aboutfun{count-children}
@aboutfun{count-children-if}
@aboutfun{filter-children}
The functions listed above walk only across the direct children of the
parent node. In addition, the node hierarchy can be mapped recursively
using these functions:
@aboutfun{map-recursively}
@aboutmacro{do-recursively}
@aboutfun{find-recursively}
@aboutfun{filter-recursively}
@end{section}
@begin[Adding and Removing Child Nodes]{section}
While all nodes can be asked for their children, only documents and
elements permit actually adding children. (For all other nodes, the
sequence of children appears as empty.)
The most flexible function capable of changing the child nodes is
@fun{replace-children}. Perhaps more common is @fun{insert-child},
a specialized version for only one new child.
@aboutfun{replace-children}
@aboutfun{insert-child}
Various convenience functions are offered in addition:
@aboutfun{prepend-child}
@aboutfun{append-child}
@aboutfun{delete-child}
@aboutfun{delete-child-if}
@aboutfun{delete-nth-child}
@aboutfun{insert-child-before}
@aboutfun{insert-child-after}
@aboutfun{replace-child}
A node can also be deleted from its parent directly using @fun{detach}.
@aboutfun{detach}
@fun{detach} also works for attributes.
@end{section}
@begin[Elements and their Attributes]{section}
In addition to their children, elements have attributes and \"extra
namespaces\".
Attributes themselves are nodes and be accessed using these functions:
@aboutfun{add-attribute}
@aboutfun{remove-attribute}
@aboutfun{find-attribute-named}
@aboutfun{find-attribute-if}
@aboutfun{list-attributes}
@aboutfun{map-attributes}
@aboutmacro{with-attributes}
As a shortcut, the @fun{attribute-value} and its @code{setf} function
allow access to attribute values by name, without having to look up the
attribute node first:
@aboutfun{attribute-value}
There are three ways to declare a namespace: Using the name of the
element, using the name of one of its attributes, or using an \"extra
namespace\". A prefix can be looked up from any of these local
declarations. It is also possible to look up a namespace while taking
into account all declarations on parent elements.
@aboutfun{find-local-namespace}
@aboutfun{find-namespace}
Extra namespaces are needed only when a namespace must be declared even
though there is no element or attribute referencing it through its name.
For example, an attribute declared with type @code{QName} using
RelaxNG/XSD must reference a namespace in scope.
@aboutfun{add-extra-namespace}
@aboutfun{remove-extra-namespace}
@aboutfun{find-extra-namespace}
@aboutfun{map-extra-namespaces}
@end{section}"))
(defpackage :cxml-stp-impl
(:use :cl :stp)
(:shadow #:document #:document-type)
(:import-from :xpath-protocol #:define-default-method))