Dynamic predicate in XQuery - xquery

I notice one fact that when predicate has dynamic field to compare then it doesn't work.
For example:
db:open("library")//book[$filterFields = $pattern]
for this I get 0 results,
but when I put for example category instead of $filterField then I have some results.
How can I use variable in predicate as field?

If $filterFields is supposed to contain a list of element names, you can possibly use the following query:
db:open("library")//book
[*[name() = $filterFields] = $pattern]

Related

Custom sorting issue in MarkLogic?

xquery version "1.0-ml";
declare function local:sortit(){
for $i in ('a','e','f','b','d','c')
order by $i
return
element Result{
element N{1},
element File{$i}
}
};
local:sortit()
the above code is sample, I need the data in this format. This sorting function is used multiple places, and I need only element N data some places and only File element data at other places.
But the moment I use the local:sortit()//File. It removes the sorting order and gives the random output. Please let me know what is the best way to do this or how to handle it.
All these data in File element is calculated and comes from multiple files, after doing all the joins and calculation, it will be formed as XML with many elements in it. So sorting using index and all is not possible here. Only order by clause can be used.
XPath expressions are always returned in document order.
You lose the sorting when you apply an XPath to the sequence returned from that function call.
If you want to select only the File in sorted order, try using the simple mapping operator !, and then plucking the F element from the item as you are mapping each item in the sequence:
local:sortit() ! File
Or, if you like typing, you can use a FLWOR to iterate over the sequence and return the File:
for $result in local:sortit()
return $result/File

LINQ query on dotnet code and how concat will work here

i am not a dot net programmer but need to migrate dotnet code to java .having issue understanding this follwing piece
Lets say specificTermical and ShipTo have latitutde property with different value so what happends when we use concat what will be the final value eg. 23.10+43.10 or something else
List<OrderDispatchItemDTO> locations =(List<OrderDispatchItemDTO>) msg.Details.Select(x => x.SpecificTerminal).Concat(msg.Details.Select(x => x.ShipTo));
The line of code that you provide returns a List of OrderDispatchItemDTO objects, that contains the values of both the SpecificTerminal and ShipTo properties of the Details objects.
It doesn't make any kind of calculation between the values of SpecificTerminal and ShipTo properties; it only adds both of them in a common list.
More detailed:
The Select method returns a new IEnumerable of the selected objects
And the Concat method concatenates the second collection into the first.
Concat is a string method. When you concatenate "23.10" and "43.10", it gives "23.1043.10". Therefore combining the two strings together.
To do any calculation in c#, you have to convert from strings data types to other mathematical data type that fits the say.
You may convert those two values to float and add them as shown below:
Float sum = Convert.ToFloat(23.10) + Convert.ToFloat(43.10);

getNodeSet by attribute values

I'm parsing xml with R's getNodeSet function by attribute value with the following code:
getNodeSet(doc, "/body//*[#attribution='HM'][#*='checkmark'][#*='underline']")
The code above returns node content that includes all three of the above values (effectively, 'HM' And 'checkmark' And 'underline').
I'd like the function to return nodes in which the first value remains constant, but for which additional values are EITHER/OR (effectively, 'HM' AND 'checkmark' OR 'underline').
Grateful for any help.
The solution is to combine the type attribute values to be OR'd within a single set of square brackets, and supply 'or' without quotes:
getNodeSet(doc, "/body//*[#attribution='HM'][#*='underline' or #*='checkmark']")

Xquery Where clause not working when using nested query

Good Day,
I'm new to xquery.
I'm trying to execute xquery with a where clause that returns the values greater than a value returned from a nested query as show below. It runs but returns values that are not greater than the returned value
If I use the where clause directly with the value it works fine. I'm using BaseX to execute my query. Appreciate any feedback, I believe the parser may be reading my subquery wrong.
for $y in doc("url.xml")/taxi_stations/stand
where $y/taxis>=
(
for $x in doc("url.xml")/taxi_stations/stand
where $x/name="Jacksonville"
return data($x/taxis)
)
return ($y/taxis,$y/name)
What is the value of $taxis? It's possible your comparisons are happening on strings, not numbers.
In your nested return, you call data() on $x/taxis - assuming that correctly returns a number value, then $y/taxis in your outer where clause should probably also be wrapped in data().
However, if there is no schema on your document, then data() will simply return a string. In that case you should convert your taxis element to a number using fn:number() or directly casting it like $y/taxis/xs:integer(.).

TALES expression to compare numeric input in Plone?

TALES expression is new to me. Can I get some good reference for the same? Actually I wish to define a content rule for numeric input field using ploneformgen. Something like:
python: request.form.get('amt', False) <= 5000
then apply the rule.
Here 'amt' is a numeric/whole number field on the input form.
For reference, you should look at the official TALES specification, or refer to the TALES section of the Zope Page Templates reference.
In this case, you are using a plain python expression, and thus the normal rules of python code apply.
The expression request.form.get('amt', False) would return the request parameter 'amt' from the request, and if that's missing, return the boolean False, which you then compare to an integer value.
There are 2 things wrong with that expression: first of all you assume that the 'amt' parameter is an integer value. Even a PFG integer field however, is still a string in the request object. As such you'll need to convert in to an integer first before you can compare it.
Also, you fall back to a boolean, which in integer comparisons will be regarded as the equivalent of 0, better be explicit and use that instead:
python: int(request.form.get('amt', 0)) <= 5000
Note that for a PFG condition, you can also return a string error message instead of boolean True:
python: int(request.form.get('amt', 0)) <= 5000 or 'Amount must be not be greater than 5000'
Usually form parameters are passed in as strings if they are not defined on the application level otherwise e.g.
Zope will under the hood use the fieldname amt:int in order to convert the value to an integer.
So you may want to try to put an int(....) around the first expression.

Resources