summaryrefslogtreecommitdiff
path: root/man/2/w3c-xpointers
blob: deda4e329f333030623e3a8cf3b0551080312759 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
.TH W3C-XPOINTERS 2
.SH NAME
w3c-xpointers \- parser for XPointers framework including XPath
.SH SYNOPSIS
.EX
include "xpointers.m";

xpointers := load Xpointers Xpointers->PATH;
Xpath, Xstep: import xpointers;

# special operators ('+', '-', etc represent themselves)
One, Ole, Oge, Omul, Odiv, Omod, Oand, Oor, Oneg,
Onodetype, Onametest, Ofilter, Opath: con ...;

# axis types
Aancestor,
Aancestor_or_self,
Aattribute,
Achild,
Adescendant,
Adescendant_or_self,
Afollowing,
Afollowing_sibling,
Anamespace,
Aparent,
Apreceding,
Apreceding_sibling,
Aself: con iota;

Xstep: adt {
   axis:  int;  # Aancestor, ... (above)
   op:    int;  # Onametest or Onodetype
   ns:    string;
   name:  string;
   arg:   string;  # optional parameter to processing-instruction
   preds: cyclic list of ref Xpath;

   text:  fn(nil: self ref Xstep): string;
   axisname:  fn(i: int): string;
};

Xpath: adt {
   pick{
   E =>
      op:    int;
      l, r:  cyclic ref Xpath;
   Fn =>
      ns:    string;
      name:  string;
      args:  cyclic list of ref Xpath;
   Var =>
      ns:    string;
      name:  string;
   Path =>
      abs:   int;
      steps: list of ref Xstep;
   Int =>
      val:   big;
   Real =>
      val:   real;
   Str =>
      s:     string;
   }
   text: fn(nil: self ref Xpath): string;
};

framework:  fn(s: string):
               (string, list of (string, string, string), string);

# predefined schemes
element:    fn(s: string): (string, list of int, string);
xmlns:      fn(s: string): (string, string, string);
xpointer:   fn(s: string): (ref Xpath, string);
.EE
.SH DESCRIPTION
.B Xpointers
implements a parser for the World-Wide Web Consortium's XPointers framework,
including a parser for XPath expressions.
.PP
.B Init
must be called before any other operation in the module.
.PP
.B Framework
parses a string
.I s
according to the grammar for the XPointers framework,
and returns a tuple
.BI ( short,\ pointers,\ err ) .
On an error, the string
.I err
gives a diagnostic and the other two values are nil.
Otherwise, if
.I short
is non-nil, the XPointer was a `shorthand pointer', with the given value;
.I pointers
will be nil.
If a scheme-based pointer is used,
.I short
is nil and
.I pointers
has a list of tuples
.BI ( ns,\ scheme,\ data ) ,
each representing one pointer value.
.I Ns
is the XML name space for the given
.IR scheme ;
the default name space is represented by nil.
.I Scheme
is the XPointer scheme name within that name space; and
.I data
is the actual pointer value following the rules of that scheme.
(They all have completely different syntax.)
.PP
Three common schemes are directly supported by the module,
by functions named after the scheme.
All of them follow the convention of returning a tuple in which
the last element is a diagnostic string.
On an error, all but that last element of the tuple will be nil,
and the last element will be a non-nil string with a diagnostic.
.PP
.B Xmlns
parses an XML name space definition of the form
.IB ns = uri,
and returns its components.
.PP
.B Element
parses a value of the XPointer
.B element
scheme, given by the grammar:
.IP
.EX
.ft I
selector \f1::=\fP name child*  \f1|\fP  child+
child \f1::=\fP '\f5/\fP' \f5[1-9][0-9]\fP*
.EE
.PP
The optional
.I name
is an XPointer `shorthand pointer'.
Each
.I child
number selects the child with that index (origin 1) at the corresponding level of the XML tree
beneath the node selected by the
.IR name ,
or starting at the root of the XML tree.
.B Element
returns a tuple
.BI (( name,\ path ) ,\ err )
where
.I name
is the top element name or nil if none was specified,
and
.I path
is a
.B "list of int"
giving the path of child indices.
.PP
The most complex scheme is
.BR xpointer ,
because its syntax is that of XML's elaborate XPath expression.
.B Xpointer
parses such an expression and returns a tuple
.BI ( e,\ err )
where
.I e
refers to an
.B Xpath
value that represents the abstract syntax of the XPath
.BR Expr .
.B Xpointer
checks only the syntax of
.IR s ,
and does not check that functions are limited to those specified by the
.B xpointer
scheme (that is consistent with it being a parse of
.IR s ,
rather than an XPointer or XPath evaluator).
.PP
.B Xpath
and
.B Xstep
together represent an abstract syntax of the XPath grammar.
.PP
.B Xstep
represents the XPath
.B Step
grammar rule, with all abbreviations expanded to their full form:
.IP
.EX
.ft I
Step \f1::=\fP AxisName '\f5::\fP' NodeTest Predicate*
NodeTest \f1::=\fP NameTest \f1|\fP NodeType '\f5(\fP' '\f5)\fP'
NameTest \f1::=\fP '\f5*\fP' \f1|\fP NCName '\f5:\fP' '\f5*\fP' \f1|\fP (NCName '\f5:\fP')? NCName
Predicate \f1::=\fP '\f5[\fP' Expr '\f5]\fP'
.EE
.PP
The correspondence is as follows:
.TF s.text()
.PD
.TP
.IB s .axis
Represents the
.B AxisName
by one of the constants
.B Aancestor
to
.BR Aself .
.TP
.IB s .op
.B Onametest
or
.B Onodetype
to say which rule is represented
.TP
.IB s .ns
For a
.IR NameTest ,
gives the XML name space;
can be
.L *
for `any name space' or nil for the default name space.
For a
.IR NodeType ,
gives the type:
.BR comment ,
.BR node ,
.BR processing-instruction ,
or
.BR text .
.TP
.IB s .name
Gives the
.I name
for a
.IR NameTest ;
can be
.L *
for `any name'.
.TP
.IB s .arg
The optional literal parameter to a
.I NodeType
that is a
.BR processing-instruction .
.TP
.IB s .preds
A list of
.B Xpath
values representing the optional sequence of
.I Predicate
expressions
.TP
.IB s .text()
Returns a string representing the
.B Xstep
in textual form.
.TP
.IB s .axisname( a )
Returns the printable text for axis code
.I a
(ie,
one of
.B Aancestor
to
.BR Aself )
.PP
.B Xpath
values represent an abstract syntax for an XPath expression.
Briefly, an expression follows the grammar below (see the XPath specification for the full concrete syntax).
.IP
.EX
.ft I
.ta \w'e ::=  'u
e ::=	e '\f5or\fP' e
   |	e '\f5and\fP' e
   |	e \f1(\fP'\f5=\fP' \f1|\fP '\f5!=\fP'\f1)\fP e
   |	e \f1(\fP'\f5<\fP' \f1|\fP '\f5<=\fP' \f1|\fP '\f5>=\fP' \f1|\fP '\f5>\fP'\f1)\fP e
   |	e \f1(\fP'\f5+\fP' \f1|\fP '\f5-\fP'\f1)\fP e
   |	e \f1(\fP'\f5*\fP' \f1|\fP '\f5div\fP' \f1|\fP '\f5mod\fP'\f1)\fP e
   |	'\f5-\fP' e
   |	e '\f5|\fP' e
   |	filter
   |	path
filter ::= primary predicate* \f1(\fP\f1(\fP'\f5/\fP' \f1|\fP '\f5//\fP'\f1)\fP relpath\f1)\fP?
primary ::= '\f5$\fP' QName \f1|\fP '\f5(\fP' e '\f5)\fP' \f1|\fP Literal \f1|\fP Number \f1|\fP FunctionName '\f5(\fP' \f1(\fPe \f1(\fP'\f5,\fP' e\f1)\fP*\f1)\fP '\f5)\fP'
path ::= '\f5/\fP' relpath \f1|\fP relpath
relpath ::= relpath '\f5/\fP' relpath \f1|\fP relpath '\f5//\fP' relpath \f1|\fP Step
.EE
.PP
Most of
.I e
is represented by a binary tree using the pick
.BI Xpath.E( op,\ l,\ r )
where
.I op
is an operator symbol (either the character itself or one of the constants
.BR One ,
.BR Odiv ,
etc. for compound symbols),
and
.I l
and
.I r
represent the operands.
The only unary operator
.B Oneg
has its operand in
.IR l .
A
.I filter
uses the binary operator
.BI Xpath.E(Ofilter ,\ e,\ pred )
to apply each
.I predicate
to the preceding
.I primary
or
.IR predicate .
A
.I filter
also uses
.BI Xpath.E(Opath ,\ e,\ relpath )
to apply the optional
.I relpath
(represented by a value of
.BR Xpath.Path )
to the preceding part of the filter expression.
.PP
The other cases in the pick adt correspond to the various choices of
.I path
and
.IR primary .
Integer and real numbers are distinguished.
.I Literal
is represented by
.BR Xpath.Str ;
variable references (ie,
.BI $ QName\c
)
are represented by
.BR XPath.Var ,
where
.I ns
gives the optional XML name space of the
.IR name .
.I Path
is represented by
.BI Xpath.Path( abs,\ steps )
where
.I abs is non-zero if and only if the path is absolute (starts with `/' or `//'),
and
.I steps
lists the
.B Xstep
values corresponding to the slash-separated
.I Steps
in the grammar.
Abbreviated forms such as
.RB ` // '
are converted by
.B xpointer
to their full internal form in terms of
.RB ` / ',
as defined by the specification,
so there is no need to distinguish the delimiters in this representation.
.SH SOURCE
.B /appl/lib/w3c/xpointers.b
.SH SEE ALSO
``XML Path Language (XPath) Version 1.0'',
.B http://www.w3.org/TR/xpath
.br
``XPointer framework'',
.B http://www.w3.org/TR/xptr-framework/
.br
``XPointer element() scheme'',
.B http://www.w3.org/TR/xptr-element/
.br
``XPointer xmlns() scheme'',
.B http://www.w3.org/TR/xptr-xmlns/
.br
``XPointer xpointer() scheme'',
.B http://www.w3.org/TR/xptr-xpointer/