Data-query specification that resembles files and directories.
char *result = dq_query(doc,"query"); // Return a single value from your data
array(char*) result = dq_queries(doc,"query"); // Return a result set from your data (array)
{
"books": [{
"@class": "book(ssf)",
"@category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"@class": "book(ssf)",
"@category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"@class": "book(ssf)",
"@category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"price": 8.99
},
{
"@class": "book(ssf)",
"@category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
Query | Result |
---|---|
/store/book[*]/author | The authors of all books in the store |
//author | All authors |
/store/* | All things in store, which are some books and a red bicycle |
/store//price | The price of everything in the store. |
//book[2] | The third book |
//book[0,1] | The first two books |
//book[1:2] | All books from index 1 (inclusive) until index 2 (exclusive) |
//book[2:] | All books from index 2 (inclusive) till end |
//book[:2] | All books from index 0 (inclusive) until index 2 (exclusive) the first 2 books |
//book[-2] | The second to last book |
//book[-2:] | Last two books |
//book[-1:] | The last book |
//book[isbn] | All books with an ISBN number |
//book[price<10] | All books in store cheaper than 10 |
//* | All Elements in XML document. All members of JSON structure. |
//book[author =~ //*REES/] | All books matching regex |
//@category | All categories |
//@category[2] | 'fiction' |
//book/*[1]/@category | 'fiction' |
/book[2]/@category | 'fiction' |
//book/*[2] | objects |
count(//book) | The number of books |
count(//@category) | 2 |
count(//book//*) | 2 |
type(//book/*[2]) | 'string' |
type(*/*[2]) | 'subData' |
text(*/book/*[2]/) | 'val' |
p[@x] | selects all P elements with an attribute named X, in current context. |
//p[@x] | selects all P elements with an attribute named X, in any context from current. |
//item[@price > 2*@discount] | selects all items whose price attribute is greater than twice their discount attribute. |
v[x or y] | w[z] | selects all V elements with X or Y child-elements, and all W elements with Z child-elements, that were found in the current context. |
a[1][@href=='help.php'] | selects a match only if the first A child of the context node satisfies the condition @HREF='HELP.PHP', while |
a[@href=='help.php'][0] | selects the first A child that satisfies this condition. |
a[/html/@lang='en'][@href=='help.php'][0]/@target | selects the value of the TARGET attribute of the first A element among the children of the context node that has its HREF attribute set to HELP.PHP, provided the document's html top-level element also has a LANG attribute set to EN. |
from(//data/fruits).startsWith("a").or().startsWith("b").count() | |
from(//records).reverse() | |
from(//records).join(//data/locations).orderBy(/location/city) | |
from(/data/fruits).case(0).equals("STRAWBERRY").count() | |
from(/data/fruits).startsWith("a").union(["tomato", "carrot"]).count() | |
from(/data/emails).matches("@.com").any() |
Operators | Description |
---|---|
. | The current object/element |
.. | Parent operator |
/ | The root object/element |
// | Recursive descent. |
child/ or [child] | Child operator (also array index) |
*? | Wildcards. Available anywhere a name or numeric are required. |
[] | Subscript operator. |
[,] | Union operator, allows alternate names or array indices as a set. |
[start:end] | Array slice operator |
@child | Attribute access. An attribute is some @meta info added to a document. |
$child | Convert child to string |
#child | Convert child to number |
Filters | Description |
---|---|
== | left is equal to right (note that 1 is not equal to '1') |
!= | left is not equal to right |
< | left is less than right |
<= | left is less than or equal to right |
> | left is greater than right |
>= | left is greater than or equal to right |
=~ | left matches regular expression [name =~ /foo*/] |
=~~ | left matches regular expression [name =~~ /foo*/] (case insensitive) |
- + - * div mod | left and right arithmetical operation (double) |
in | left exists in right |
nin | left does not exists in right |
subsetof | left is a subset of right |
anyof | left has an intersection with right |
noneof | left has no intersection with right |
empty | left (array or string) should be empty |
and && or || not ! | |
() grouping |
- Valid syntax: function(element), function(set) or set.function()
- function(element) evaluates on single element.
- function(set) evaluates to first element of set. Equivalent to function(set[0]).
- set.function() evaluates to all elements in set. Used for chaining (eg set.function1().function2().function3())
Prepare LINQ | Description |
---|---|
from(source) | Continues if selection exists. |
Debugging LINQ | Description |
debug(bool) | Continues into debug mode |
print() | Continues after printing current query |
break() | Continues after user agrees on debugger. |
Filtering LINQ | Description |
where(query) | Continues if expression matches. |
==(query) | Continues if two fields are equal |
<(query) | Continues if A is less than B. |
>(query) | Continues if A is greater than B. |
<=(query) | Continues if A is less than or equal to B. |
>=(query) | Continues if A is greater than or equal to B. |
between(query) | Continues if A belongs to (B,C) range. |
between=(query) | Continues if A belongs to [B,C] range. |
empty(query) | Continues if A is empty. |
in(query) | Continues if A is in B. |
contains(query) | Continues if B is in A. find(B) instead? |
startsWith(query) | Continues if the first element in A vgfbvis equal to B |
endsWith(query) | Continues if the last element in A is equal to B |
and(action) | Continues if A was true |
or(action) | Continues if A was false, else skip next chained linq |
Result LINQ | Description |
any(selection) | Continues if any of A matched the query |
all(selection) | Continues if all of A matched the query |
none(selection) | Continues if none of A matched the query |
Action LINQ | Description |
each(action) | Continues after executing action to each A record |
attach(selection) | Continues after attaching a property to each A record |
detach(selection) | Continues after detaching a property from each A record |
Grouping LINQ | Description |
subtract(selection) | Continues after removing all A present in B values (difference:123-234=1) |
unique(selection) | Continues after extracting distinct A and B values (symmetric difference:123^234=14) |
shared(selection) | Continues after extracting repeated A and B values (intersection:123&234=23) |
join(selection) | Continues after merging A and B both together (union:123+234=1234) |
Ordering LINQ | Description |
reverse(bool) | Continues after reversing ordering logic. |
sort(pred) | Continues after sorting by predicate. |
shuffle(N) | Continues after shuffling N elements. |
at(selection) | Continues after selecting the element at the specified index (allows a default match if nothing is found). |
last(selection) | Continues after selecting the last match of the set (allows a default match if nothing is found). |
first(selection) | Continues after selecting the first match of the set (allows a default match if nothing is found). |
others(selection) | Continues after selecting all elements but the first excluded. |
Stack LINQ | Description |
push() | Continues after pushing current LINQ into stack for further usage |
use(N) | Continues after using Nth query from stack |
pop() | Continues after popping current LINQ from stack |
Conversion LINQ | Description |
select(record,...) | Returns array of matching records. |
string() | Returns A as string. |
number() | Returns A as number. |
bool() | Returns A as bool. |
type() | Returns A type. |
Numerical LINQ | Description |
min() | Returns minimum value of A (as double) |
max() | Returns maximum value of A (as double) |
avg() | Returns average value of A (as double) |
sdv() | Returns standard deviation value of A (as double) |
sum() | Returns sum value of A (as double) |
floor() | Returns floor value of A (as double) |
ceil() | Returns ceil value of A (as double) |
round() | Returns round value of A (as double) |
count() | Returns total records found in A (as integer) |
String LINQ | Description |
case(bool) | Makes string comparisons and search case sensitive or not. |
matches(query) | Continues if A matches regular expression of B. Allows wildcards. |
begins() | Continues if A starts with B |
ends() | Continues if A ends with B |
finds() | Continues if A is found in B |
cat(B) | Returns concatenated A and B strings |
sub(start [,count]) | Returns substring of A "1999/04/01".sub(5,2) -> "04" |
left(B) | Returns left of A after first B find "1999/04/01".left("/") -> "1999" |
right(B) | Returns right of A after first B find "1999/04/01".right("/") -> "04/01" |
len() | Returns number of characters in A |
trim() | Returns trimmed A of leading/trailing/in-between whitespaces excesses. |
map(src,dst) | Returns remapped A string "--bar123--".map("abc-","ABC") -> "BAr123" |
lower() | Returns A in lowercase |
upper() | Returns A in UPPERCASE |
camel() | Returns A in camelCase |
- Data-query spec dual licensed into the public domain and CC0.
- Original work by @r-lyeh.
- Grabbed some ideas from XPath, JsonPath and Jsel READMEs.