In JSONata, where is documentation for wildcard * operator in array context? - jsonata

In JSONata query to flatten array of arrays SteveR points out that one can use $.* to flatten an array.
I have searched through the documentation and found references to wildcard () in object context. However I have not found a definition of the behavior of the wildcard () operator in array context in JSONata.
The flattening behavior seems to be similar to the flattening of sequences in XQuery.
Q: Where is the behavior of wildcard (*) in array context documented for JSONata?

The wildcard (*) is used to select the values of all properties of an object, or array of objects. The documentation is here.

Related

CloudKit: Query predicate list against array

I'm looking to construct a predicate which checks if a list of strings in CloudKit contains any element in an array of strings. I know I can check if a string is in a list in CloudKit by using the CONTAINS predicate, and I also know I can check if a string field is in an array using the IN predicate, however due to using an array/list of strings on both sides of the predicate I need form a CONTAINS IN predicate. anyone know how?
Use ANY array IN otherArray within the predicate

What is the difference between th:field="${something}" and th:field="*{something}"?

I am new to thymeleaf and I don't understand the difference between th:field="${something}" and th:field="*{something}". When to use * and when to use $?
Quoting the thymeleaf documentation
Variable expressions not only can be written in ${...} expressions,
but also in *{...} ones.
There is an important difference, though: the asterisk syntax
evaluates expressions on selected objects rather than on the whole
context variables map. This is: as long as there is no selected
object, the dollar and the asterisk syntaxes do exactly the same.
From here in the 4.3 Expressions on selections (asterisk syntax) section.
Technically the difference is that if you have selected an Object
like so th:object="${session.user}
then you can use the * for the properties of the user object
like so th:text="*{lastName}"
If you haven't selected an object they are the same.

Is there a short and elegant way to write an XPath 1.0 expression to get all HREF values containing at least one of many search values?

I was just wondering if there is a shorter way of writing an XPath query to find all HREF values containing at least one of many search values?
What I currently have is the following:
//a[contains(#href, 'value1') or contains(#href, 'value2')]
But it seems quite ugly, especially if I were to have more values.
First of all, in many cases you have to live with the "ugliness" or long-windedness of expressions if only XPath 1.0 is at your disposal. Elegance is something introduced with version 2.0, I'd daresay.
But there might be ways to improve your expression: Is there a regularity to the href attributes you'd like to find? For instance, if it is sufficient as a rule to say that the said href attribute values must start with "value", then the expression could be
//a[starts-with(#href,'value')]
I know that "value1" and "value2" are most probably not your actual attribute values but there might be something else that uniquely identifies the group of a elements you're after. Post your HTML input if this is something you want us to help you with.
Personally, I do not find your expression ugly. There is just one or operator and the expression is quite short and readable. I take
if I were to have more values.
to mean that currently, there are only two attribute values you are interested in and that your question therefore is a theoretical one.
In case you're using XPath 2 and would like to have exact matches instead of also matches only containing part of a search value, you can shorten with
//a[#href = ('value1', 'value2')]
For contains() this syntax wouldn't work as the second argument of contains() is only allowed to be 0 or 1 value.
In XPath 2 you could also use
//a[some $s in ('value1', 'value2') satisfies contains(#href, $s)]
or
//a[matches(#href, "value1|value2")]

Getting an array of a capture group's matches

I'm trying to capture the names of every method in a source file using Ruby, because I want to learn Ruby better. I've never used regex in Ruby, only Perl, so bear with me.
I'm using this regex pattern:
/\w* (.*)\([\w| |,]+\);/
which should capture the method name in a C method declaration, for example the mult in this line:
int mult ( int x, int y );
I want to get an array of every method name in the source file.
I tried this:
sourceCode.scan(/\w* (.*)\([\w| |,]+\);/)
And it returns an array of single element arrays containing all the matches, but there has to be a better way to get it in a 1D array than unwrapping that.
Does anybody know of a better way to get a 1D array of global capture group matches?
The array is only nested if you use capture groups, so if you want a flat array you could design a regexp without any capture groups. This can be done using positive look-ahead (?=pat) and look-behind (?<=pat):
sourceCode.scan(/(?<=\w)\s.+?(?=\s?\([\w| |,]+\);)/)
That begin said I think the flatten method (as mentioned in the comments) is the way to go as it is more readable and the regexp above should not really see the light of day.

Is this XPath technique reliable in all situations?

I am developing an application that accepts user-defined XPath expressions and employs them as part of its runtime operation.
However, I would like to be able to infer some additional data by programmatically manipulating the expression, and I am curious to know whether there are any situations in which this approach might fail.
Given any user-defined XPath expression that returns a node set, is it safe to wrap it in the XPath count() function to determine the number of nodes in the set:
count(user_defined_expression)
Similarly, is it safe to append an array index to the expression to extract one of the nodes in the set:
user_defined_expression[1]
Well an XPath expression (in XPath 1.0) can yield a node-set or a string or a number or a boolean and doing count(expression) only makes sense on any expression yielding a node-set.
As for adding a positional predicate, I think you might want to use parentheses around your expression i.e. to change /root/foo/bar into (/root/foo/bar)[1] as that way you select the first bar element in the node-set selected by /root/foo/bar while without them you would get /root/foo/bar[1] which would select the first bar child element of any foo child element of the root element.
Are you checking that such user-defined expressions always evaluate to node-set?
If yes, first Expr is ok. Datatype will be correct for fn:count
Second one is a lot trickier, with a lot of situations there predicate will overweight axis, for example. Check this answer for a simple analysis. It will be difficult to say, what a user really meant.
A more robust approach would be to convert the XPath expression to XQueryX, which is an XML representation of the abstract syntax tree; you can then do XQuery or XSLT transformations on this XML representation, and then convert back to a modified XPath (or XQuery) for evaluation.
However, this will still only give you the syntactic structure of the expression; if you want semantic information, such as the inferred static type of the result, you will probably have to poke inside an XPath process that exposes this information.

Resources