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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/**
* @file xpath.h
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief YANG XPath evaluation functions header
*
* Copyright (c) 2015 - 2025 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
;
;
/**
* @internal
* @page internals
* @section internalsXpath XPath Implementation
*
* XPath evaluator fully compliant with http://www.w3.org/TR/1999/REC-xpath-19991116/
* except the following restrictions in the grammar.
*
* @subsection internalsXpathGrammar Parsed Grammar
*
* Full axes are not supported, abbreviated forms must be used,
* "id()" function is not supported, and processing instruction and comment nodes are not supported,
* which is also reflected in the grammar. Undefined rules and constants are tokens.
*
* Modified full grammar:
* @code
* [1] Expr ::= OrExpr // just an alias
*
* [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
* [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
* [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
* [5] Step ::= (AxisName '::' | '@')? NodeTest Predicate* | '.' | '..'
* [6] NodeTest ::= NameTest | NodeType '(' ')'
* [7] NameTest ::= '*' | NCName ':' '*' | QName
* [8] NodeType ::= 'text' | 'node'
* [9] Predicate ::= '[' Expr ']'
* [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
* [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
* [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
* | PrimaryExpr Predicate* '/' RelativeLocationPath
* | PrimaryExpr Predicate* '//' RelativeLocationPath
* [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
* [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
* [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
* | EqualityExpr '!=' RelationalExpr
* [16] RelationalExpr ::= AdditiveExpr
* | RelationalExpr '<' AdditiveExpr
* | RelationalExpr '>' AdditiveExpr
* | RelationalExpr '<=' AdditiveExpr
* | RelationalExpr '>=' AdditiveExpr
* [17] AdditiveExpr ::= MultiplicativeExpr
* | AdditiveExpr '+' MultiplicativeExpr
* | AdditiveExpr '-' MultiplicativeExpr
* [18] MultiplicativeExpr ::= UnaryExpr
* | MultiplicativeExpr '*' UnaryExpr
* | MultiplicativeExpr 'div' UnaryExpr
* | MultiplicativeExpr 'mod' UnaryExpr
* [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
* [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
* @endcode
*/
/* expression tokens allocation */
/* XPath matches allocation */
/* building string when casting */
/* Maximum number of nested expressions. */
/**
* @brief Tokens that can be in an XPath expression.
*/
;
/**
* @brief XPath Axes types.
*/
;
/**
* @brief XPath (sub)expressions that can be repeated.
*/
;
/**
* @brief Types of context nodes, #LYXP_NODE_ROOT_CONFIG used only in when or must conditions.
*/
;
/**
* @brief Structure holding a parsed XPath expression.
*/
;
/*
* lyxp_expr repeat
*
* This value is NULL for all the tokens that do not begin an
* expression which can be repeated. Otherwise it is an array
* of expression types that this token begins. These values
* are used during evaluation to know whether we need to
* duplicate the current context or not and to decide what
* the current expression is (for example, if we are only
* starting the parsing and the first token has no repeat,
* we do not parse it as an OrExpr but directly as PathExpr).
* Examples:
*
* Expr: "/ *[key1 and key2 or key1 < key2]"
* Tokens: '/' '*' '[' NameTest 'and' NameTest 'or' NameTest '<' NameTest ']'
* Repeat: NULL NULL NULL _ NULL NULL NULL _ NULL NULL NULL
* | v
* v RelationalExpr 0
* AndExpr OrExpr 0
*
* Expr: "//node[key and node2]/key | /cont"
* Tokens: '//' NameTest '[' NameTest 'and' NameTest ']' '/' NameTest '|' '/' NameTest
* Repeat: _ NULL NULL _ NULL NULL NULL NULL NULL NULL NULL NULL
* | v
* v AndExpr 0
* UnionExpr 0
*
* Operators between expressions which this concerns:
* 'or', 'and', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', 'div', 'mod', '|'
*/
/**
* @brief Supported types of (partial) XPath results.
*/
;
/**
* @brief Item stored in an XPath set hash table.
*/
_PACKED;
/**
* @brief XPath variable bindings.
*/
;
/**
* @brief XPath set - (partial) result.
*/
;
/**
* @brief Get string format of an XPath token.
*
* @param[in] tok Token to transform.
* @return Token type string.
*/
const char *;
/**
* @brief Evaluate an XPath expression on data. Be careful when using this function, the result can often
* be confusing without thorough understanding of XPath evaluation rules defined in RFC 7950.
*
* Traverses (valid) opaque nodes in the evaluation.
*
* @param[in] ctx libyang context to use.
* @param[in] exp Parsed XPath expression to be evaluated.
* @param[in] cur_mod Current module for the expression (where it was "instantiated").
* @param[in] format Format of the XPath expression (more specifically, of any used prefixes).
* @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
* @param[in] cur_node Current data node, NULL in case of the root node. Equal to @p ctx_node unless a
* subexpression is being evaluated.
* @param[in] ctx_node Starting context data node, NULL in case of the root node. Equal to @p cur_node unless a
* subexpression is being evaluated.
* @param[in] tree Data tree on which to perform the evaluation, it must include all the available data (including
* the tree of @p ctx_node). Can be any node of the tree, it is adjusted.
* @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
* @param[out] set Result set.
* @param[in] options Whether to apply some evaluation restrictions.
* @return LY_EVALID for invalid argument types/count,
* @return LY_EINCOMPLETE for unresolved when,
* @return LY_EINVAL, LY_EMEM, LY_EINT for other errors.
*/
LY_ERR ;
/**
* @brief Get all the partial XPath nodes (atoms) that are required for @p exp to be evaluated.
*
* @param[in] ctx libyang context to use.
* @param[in] exp Parsed XPath expression to be evaluated.
* @param[in] cur_mod Current module for the expression (where it was "instantiated").
* @param[in] format Format of the XPath expression (more specifically, of any used prefixes).
* @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
* @param[in] cur_scnode Current schema node, NULL in case of the root node. Equal to @p ctx_scnode unless a
* subexpression is being atomized.
* @param[in] ctx_scnode Starting context schema node, NULL in case of the root node. Equal to @p cur_scnode unless a
* subexpression is being atomized.
* @param[out] set Result set.
* @param[in] options Whether to apply some evaluation restrictions, one flag must always be used.
* @return LY_ERR (same as ::lyxp_eval()).
*/
LY_ERR ;
/** used only internally, maps with @ref findxpathoptions */
/**
* @brief Cast XPath set to another type.
* Indirectly context position aware.
*
* @param[in] set Set to cast.
* @param[in] target Target type to cast @p set into.
* @return LY_ERR
*/
LY_ERR ;
/**
* @brief Free dynamic content of a set.
*
* @param[in] set Set to modify.
*/
void ;
/**
* @brief Check for duplicates in a schema node set.
*
* @param[in] set Set to check.
* @param[in] node Node to look for in @p set.
* @param[in] node_type Type of @p node.
* @param[in] skip_idx Index from @p set to skip.
* @param[out] index_p Optional pointer to store index if the node is found.
* @return Boolean value whether the @p node found or not.
*/
ly_bool ;
/**
* @brief Merge 2 schema node sets.
*
* @param[in] set1 Set to merge into.
* @param[in] set2 Set to merge. Its content is freed.
*/
void ;
/**
* @brief Insert schema node into set.
*
* @param[in] set Set to insert into.
* @param[in] node Node to insert.
* @param[in] node_type Node type of @p node.
* @param[in] axis Axis that @p node was reached on.
* @param[out] index_p Optional pointer to store the index of the inserted @p node.
* @return LY_SUCCESS on success.
* @return LY_EMEM on memory allocation failure.
*/
LY_ERR ;
/**
* @brief Parse an XPath expression into a structure of tokens.
* Logs directly.
*
* https://www.w3.org/TR/1999/REC-xpath-19991116/#exprlex
*
* @param[in] ctx Context for errors.
* @param[in] lnode Data node for logging.
* @param[in] expr_str XPath expression to parse. It is duplicated.
* @param[in] expr_len Length of @p expr, can be 0 if @p expr is 0-terminated.
* @param[in] reparse Whether to re-parse the expression to finalize full XPath parsing and fill
* information about expressions and their operators (fill repeat).
* @param[out] expr_p Pointer to return the filled expression structure.
* @return LY_SUCCESS in case of success.
* @return LY_EMEM in case of memory allocation failure.
* @return LY_EVALID in case of invalid XPath expression in @p expr_str.
*/
LY_ERR ;
/**
* @brief Duplicate parsed XPath expression.
*
* If @p start_idx and @p end_idx are both 0, the whole expression is duplicated.
*
* @param[in] ctx Context with a dictionary.
* @param[in] exp Parsed expression.
* @param[in] start_idx Starting @p exp index to duplicate.
* @param[in] end_idx Last @p exp index to duplicate.
* @param[out] dup Duplicated structure.
* @return LY_ERR value.
*/
LY_ERR ;
/**
* @brief Get the first XPath document root child, use ext instance callbacks if needed.
*
* @param[in] cur_node Current (original context) node.
* @param[in] tree Full data tree
* @return First document root child.
*/
const struct lyd_node *;
/**
* @brief Look at the next token and check its kind.
*
* @param[in] ctx Context for logging, not logged if NULL.
* @param[in] exp Expression to use.
* @param[in] tok_idx Token index in the expression \p exp.
* @param[in] want_tok Expected token.
* @return LY_EINCOMPLETE on EOF,
* @return LY_ENOT on non-matching token,
* @return LY_SUCCESS on success.
*/
LY_ERR ;
/**
* @brief Look at the next token and skip it if it matches the expected one.
*
* @param[in] ctx Context for logging, not logged if NULL.
* @param[in] exp Expression to use.
* @param[in,out] tok_idx Token index in the expression \p exp, is updated.
* @param[in] want_tok Expected token.
* @return LY_EINCOMPLETE on EOF,
* @return LY_ENOT on non-matching token,
* @return LY_SUCCESS on success.
*/
LY_ERR ;
/**
* @brief Look at the next token and skip it if it matches either of the 2 expected ones.
*
* @param[in] ctx Context for logging, not logged if NULL.
* @param[in] exp Expression to use.
* @param[in,out] tok_idx Token index in the expression \p exp, is updated.
* @param[in] want_tok1 Expected token 1.
* @param[in] want_tok2 Expected token 2.
* @return LY_EINCOMPLETE on EOF,
* @return LY_ENOT on non-matching token,
* @return LY_SUCCESS on success.
*/
LY_ERR ;
/**
* @brief Find variable named @p name in @p vars.
*
* @param[in] ctx Context for logging, not logged if NULL.
* @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
* @param[in] name Name of the variable being searched.
* @param[in] name_len Name length can be set to 0 if @p name is terminated by null byte.
* @param[out] var Variable that was found. The parameter is optional.
* @return LY_SUCCESS if the variable was found, otherwise LY_ENOTFOUND.
*/
LY_ERR ;
/**
* @brief Frees a parsed XPath expression. @p expr should not be used afterwards.
*
* @param[in] expr Expression to free.
*/
void ;
/* LY_XPATH_H */