Query Collection
Description
A XPath query selects all data objects in the data structure (application context) that match the query expression. The XPath language used within LogicNets is similar to the XPath language used in XML.
Use
The query_lua part can be used in process nodes and added through the elements menu collections > query collection. It is an extremely powerful part and can be used to extract any value or sub collection from a larger set, based on set or interpolated criteria.
Tips and Tricks
This part can be used interchangeably with Get Records and Execute Query. For large data sets with many records, query collection is usually the slowest of these parts and cannot benefit from SQL performance-tweaking, including table indexing. For broad data sets, and especially in medical and other fields where the "where" clause can get quite complex, query collection is often the easiest method for extracting target values.
Editor Fields
Field Name | Description/Use | Type/Options | Optional/Mandatory |
Query |
This is the XPath query. |
String |
Mandatory |
Result as collection |
When this is checked the system returns all results as a collection. When this is not checked the system only returns the first result. |
Checkbox |
Optional |
Result Data-object |
This is the data object in which the system stores the result of the XPath query. |
Data Object |
Optional |
Result paths |
This is the data object where the system stores the full path to the data objects that matches the query. |
Data Object |
Optional |
Result count (Rel7.3 on) | This is the data object in which the system stores the result count. |
Data Object |
Optional |
Context |
This is the context on which to query. If you leave this field empty the search will start from the root of the current context, otherwise the search will be started on the root of the given contextstring. |
String |
Optional |
XPath Syntax
A xpath query contains expressions divided with slashes ('/') and every expression have a path and optionally a condition.
For example: /level1/level2[condition2]/level3
An xpath query is executed on the root of the context or specified 'collection', from the first expression to the last expression. The final result contains all data-objects that fits all the expressions.
Expression Paths
Path |
Description |
data object name | This selects this data object. |
.. |
This selects the parent data object. |
. |
This selects the current data objects. |
* |
This selects all the child data objects. |
path() |
This returns the full path to the current level. |
subpath() |
This returns the name of the current level. |
Conditions
The condition is executed on the current data object. The condition is a Lua-based evaluation. The system selects the data object when the condition evaluates to TRUE.
In a condition all standard Lua 4-supported operators can be made like (strfind, AND, OR, ==, ~=, .....).
- Any text values in the condition should be quoted (e.g. title == 'Book 3').
[Double quotes will also work, except with non standard characters in the text, like ABC/DEF]
(e.g. title == "ABC/Book1" will not correctly operate as a condition, but title == 'ABC/Book1' will work) - Any numeric values should not be quoted (e.g. price > 35).
Also the following examples are valid in conditions.
- _.strlen(string)
=> returns the number of characters of the provided string - _.strsearch(string, pattern)
=> return 1 when the pattern is found inside the string; otherwise the result is nil - _.strfind(string, regular_expression)
=> returns 1 when the pattern is found inside the string; otherwise the result is nil;
see the patterns chapters in Lua: 4.0 reference manual for more information - _.strlower(string)
=> returns the lower-case version of the provided string - _.strupper(string)
=> returns the lower-case version of the provided string - _.getn(collection)
=> return the number of entries in the provided collection
See also https://www.lua.org/manual/4.0/manual.html#4.1 for more information.
The following table contains LogicNets-specific methods available for this part.
Method |
Description |
_.intersects(collection1, collection2, [parameters]) |
This returns TRUE when two collections have one or more of the same elements. Examples: intersects(collection_a, collection_b) intersects('a,b', { 'a' }) |
_.contains(collection1, value) |
This returns TRUE when the collection or string contains the given value. Examples: contains(my_collection, 'a')contains(my_string, 'a') notcontains(my_collection, 'a') |
_.overlaps(range|number|date, range|number|date) |
This returns TRUE when ranges overlap or if number is part of the range. Example: overlaps(onsetDateTime, {20071007, 20071014}) |
_.contains_collection(collection1, collection2, [parameters]) |
(Available in release 7.3 and later) |
_.equals_collection(collection1, collection2, [parameters]) |
(Available in release 7.3 and later) |
_.root |
This returns the root data object (typically the context of the application). |
_.position() |
This returns the position of the current data object if it is in the parent numbered collection. |
_.count() |
This returns the number of numbered items in the parent data object when this object is a collection. You can use this to select a last entry for example: [_.position() == _.count()] |
_,this |
This returns the current object. |
_.getsubpath() |
This returns the name of the current data object. |
_.getpath() |
This returns the full path of the current data object. |
Examples
Query |
Description |
/store/* |
This selects all of the child element data objects of the store data object. |
/store/book[price>35.00] |
This selects all of the book data objects of the store data object that have a price with a value greater than 35.00. |
/store/book[price>35.00]/title |
This selects all title data object values of the book data objects of the store data object that have a price with a value greater than 35.00. |
/store/*/*[price>35.00] |
This selects all data objects of the store data object that have a price with a value greater than 35.00. |
/store/book |
This selects all books. |
/store/book/* |
This returns all of the fields for all books. |
/store/book[price>35.00]/title/.. |
With .. you can navigate one level up. |
/store/*/*[price>55.00]/.. |
This returns all books. |
/store/*/*[price>35.00]/.[strfind(title, '3$')] |
This combines two separate conditions in one data object and uses a dot expression. |
/store/*/*[price>35.00 and title=='Book 3'] |
This also combines two separate conditions in one data object but uses a different syntax than the one above. |
/store/book[price>=100 and title~='Book 3'] |
This returns all books with a price that is equal to or more than 100 and that have a title this is not 'Book 3'. |
/store/book[intersects(genres, { 'science', 'nature'})]/store/book[intersects(genres, 'science,nature')]/store/book[intersects(genres, _.root.selected_genres)] |
This returns all books with 'science' or 'nature' as their genre. |
/store/book[not intersects(genres, _.root.selected_genres)] |
This returns all books that do not have 'science' or 'nature' as their genre. |
/patient1/Condition/code/coding[intersects(code, { '40930008', '40930007' }) and system=='http://snomed.info/sct']/../../..[onsetDateTime >= 19970101] |
This returns all conditions with code 40930008 or 40930007 that also have an onsetDateTime after 01 jan 1997. |
/patient1/Condition/code[intersects(coding, _.root.valueset, {'code', 'system'})]/..[] |
When the context has the following: values_six_months_ago = 19970101 the system returns all conditions of patient1 with code 40930008 and system 'http://snomed.info/sct' or code 40930007 and system 'http://snomed.info/sct'. |
/patient1/Condition[onsetDateTime and _.overlaps(onsetDateTime, {20071001, 20071013})] |
This returns all conditions with an onsetDateTime between or including 1-Oct-2007 and 13-Oct-2007. |
/store/book[_.contains(genres, 'nature')] |
This returns all books that have 'nature' as genre. |
/store/book[not _.contains(genres, 'nature')] |
This returns all books that do not have 'nature' as their genre. |
/store/book[_.contains(title, '3')] |
This returns all books that have a '3' in the title. |
/store/book[getn(genres) > 2] |
This returns all books that have more than two genres. |
/store/book[_.position() == 2] |
This returns the second book. |
/store/book[_.position() == _.count()]/title |
This returns the title of the last book. |
/store/book/*[_.this == 'Book 1'] |
This returns the title of the book that is equal to 'Book 1'. |
/xml_lua/saml2:Assertion/saml2:AttributeStatement/ |
Use _.this to select data objects that contains non-alphanumeric characters in the name. |
/store/book[title == 'Book 1']/path() |
This returns the full path to the first book: store.book[1] |
/store/book/*[_.this == 'Book 1']/subpath() |
This returns the name of the data object that is selected. In this case, 'title'. |
/store/book/*[_.getsubpath() == 'title'] |
This returns all title field values. |
/store/book/*[_.getpath() == 'store.book[1].title'] |
This returns the title of the first book. |
/kv_collection/*/*[_.getsubpath() == 'title'] |
This returns the titles. |
Advanced Feature
Use of Lua RegEx and strfind for "matching" conditions
For those users familiar with Regular Expression pattern matching, it is possible to employ Lua RegEx codes within the query string to match to a data object pattern. For example, the following condition will look for data objects in the store/book/ hierarchy that begin with A (Author, Allocation, Artwork, etc.).
/store/book[strfind(_.getsubpath(), '^A')]
where [....] contains the condition statement, and strfind('target_string','RegEx_expression') looks for the pattern in each subpath.'
Instead of using classic regular expression, the Lua string library has a special set of characters used in syntax matches. Both can be very similar, but Lua pattern matching is more limited and has a different syntax. For more information on Lua RegEx pattern matching, see the patterns chapters in Lua: 4.0 reference manual.
Example
Download and import the example project for searches listed above.
For an introductory guide to using query collection to extract information from collections, download and run the How To Guide - Query Collection project.