xquery "last" function with other selectors - xquery

I am curious how to use the last() function in Xquery along with other "selectors" (not sure of the correct lingo). Here is what I have (and this works):
for $lastscoreplay in //bbgame/plays/period[last()]/play[#action="GOOD"]
return <datarow>
<lastscoreplay>{data($lastscoreplay/#uni)}</lastscoreplay>
</datarow>
But, I want to select the last play where the #action equals GOOD. If that makes sense. I would think I could do this but it does not work:
for $lastscoreplay in //bbgame/plays/period[last()]/play[last()][#action="GOOD"]
return <datarow>
<lastscoreplay>{data($lastscoreplay/#uni)}</lastscoreplay>
</datarow>
Sorry I am still rather new to Xquery and coding in general so sorry if this is easy and I just don't see the problem. Thanks for any help!

You query get's the last play, regardless of #action, and then filters that last play by [#action="GOOD"]. Instead, you should first filter by #action, then get the last one:
//bbgame/plays/period[last()]/play[#action="GOOD"][last()]
The full query:
for $lastscoreplay in //bbgame/plays/period[last()]/play[#action="GOOD"][last()]
return
<datarow>
<lastscoreplay>{ data($lastscoreplay/#uni) }</lastscoreplay>
</datarow>

In some cases the accepted answer won't work, if that happens try this alternative:
let $all := /bbgame/plays/period[last()]/play[#action="GOOD"]
return $all[last()]

Related

I have an assigment where that I must describe some Python code, but it doesn't do anything. What should I be looking at?

I have an assigment where I have to describe what this bit of code does but when I type it does not do anything. Any ideas? Here the code.
def unknown(word):
for i in range(1,len(word)+1):
if i==len(word):
return word
if word[i]<word[i-1]:
word=word[:i-1]+word[i]+word[i-1]+word[i+1]
return unknown(word)
unknown('qwerty')
It's doing something. You just aren't seeing what it's doing because you aren't doing anything with the output of unknown. Try replacing
unknown('qwerty')
with
print(unknown('qwerty'))
and see what you get.
Edit:
It looks like there's a typo in your question:
word=word[:i-1]+word[i]+word[i-1]+word[i+1]
should very likely be:
word=word[:i-1]+word[i]+word[i-1]+word[i+1:]
That change should make it do a recursive bubble sort.

Execute function in FLWOR without using 'let'

Let's say I create a map:
let $map := map:map()
How can I put something in that map without using let? Usually I have to do something like
let $map := map:map()
let $useless-var := map:put($map, $key, $value)
Seems strange that if I want to execute something and I don't care about the return value, I still have to store the result. What am I missing here?
Note: The important part is not map(), but the fact that I can't run a function without storing the result in some pointless variable.
One approach is to execute the functions as items in a sequence where only one item (typically, the first or last) in the sequence supplies the real value for an assignment or return, as in:
let $roundedX := (
math:floor($x),
local:function1(...),
local:function2(...)[false()],
...
)
...
return (
local:functionA(...),
local:functionB(...)[false()],
...,
$roundedX * 10
)
If the function returns a value that you want to throw away, just use a false predicate, as with two of the functions above.
Of course, this approach is only useful for functions with side effects.
Don't use a FLWOR unless you need it. In my opinion FLWOR expressions are somewhat overused. I often see expressions like:
let $a := current-time()
return $a
...when it would work just as well to write:
current-time()
See also: http://blakeley.com/blogofile/2012/03/19/let-free-style-and-streaming/
In MarkLogic 7 you can use the map constructor to generate maps recursively. I think this is probably what you want:
let $map := map:new(
(1 to 10) ! map:entry(., .)
)
Or you execute map:put as part of another sequence, or in the return statement before you return the map:
let $map := map:map()
let $not-useless-var := ...
return (map:put($map, string($not-useless-var), $not-useless-var), $map)
In plain XQuery (ignoring extensions like the XQuery scripting extension) there are no side-effects, so calling a function without using its return value is meaningless.
What you may be missing here is that map:put() returns a new map with an extra item added, it does not mutate the original map. So your $useless-var is not actually useless.
EDIT: Actually I'm not sure if MarkLogic's map:put() mutates the map. (If it does, that is really gross.) I was thinking of the proposed XQuery 3.1 maps (which I've used in BaseX) which definitely are immutable.
Since the focus of your question is about running a function without storing intermediate results, you might find the map operator (!) helpful:
local:build-sequence() ! local:do-something-to-each(.)
That's good for processing sequences. If you're thinking more about processing the result of something, the answer is likely in embracing the functional nature of XQuery:
local:produce-result(
local:build-parameter(),
local:retrieve-config()
)
Not sure exactly what you're looking for, but hopefully those help.

Filter using edges / vertices

I try to filter nodes :
user = g.v(42);
g.idx('comparisons')[[id:Neo4jTokens.QUERY_HEADER + '*']]
.filter{
if (it.out('COMPARED_VALUE1').in('VOTED').in('VOTES').next().equals(user))
{
return true;
}
return false;
}.count();
I don't really understand how pipes works, but I understand that the next() breaks something in the filter "loop".
I should get 2 results, but I get none.
Regards,
I might need to amend my answer as I could require more specifics on what you are trying to achieve (as #Michael also requested), but if you think your problem is with next(), then consider the following:
user = g.v(42);
g.idx('comparisons')[[id:Neo4jTokens.QUERY_HEADER + '*']]
.filter{it.out('COMPARED_VALUE1').in('VOTED').in('VOTES').next().equals(user)}.count();
First, note above that your filter closure can immediately reduce to that (which will yield the same error, of course). Given that filter closure you are assuming that a user vertex will come out of the pipeline when you next(). That may not be the case. As such, I would re-write the filter closure as:
user = g.v(42);
g.idx('comparisons')[[id:Neo4jTokens.QUERY_HEADER + '*']].filter{
def p = it.out('COMPARED_VALUE1').in('VOTED').in('VOTES')
p.hasNext() ? p.next().equals(user) : false
}.count();
That should likely solve your problem right there given the assumption that you only need to evaluate the first item in the pipeline p which is effectively what you were doing before. I wonder if you couldn't simply use except/retain pattern here to get your answer as it is a bit less convoluted:
user = g.v(42);
g.idx('comparisons')[[id:Neo4jTokens.QUERY_HEADER + '*']]
.out('COMPARED_VALUE1').in('VOTED').in('VOTES').retain([user])
.count();
Hopefully something here puts on you on the right track to your answer.
What do you want to achieve?
Sorry my gremlin knowledge is close to zero these days.
In cypher it would probably look like this
START user=node(42), comp=node:comparisons("id:*")
MATCH comp-[:COMPARED_VALUE1]->()<-[:VOTED*2]-(user)
RETURN count(*)

print out xquery sequence and exit

Is there a way to "die" in execution flow in an xquery file and output a nicely formatted printout of a sequence variable?
I'm trying something like:
return { fn:error(xs:QName("ERROR"), $xml) }
but that doesn't quite seem to work.
Thanks!
Based on your comment (you need it for debugging) I guess you are looking for the fn:trace function, described here http://www.xqueryfunctions.com/xq/fn_trace.html
If you want to abort the execution flow and output an error in your application you should in fact use the XQuery exception handling.
Try something like this, omitting the return if this isn't part of a FLWOR expression.
...
return fn:error((), "DEBUG", $xml)
There's no need for curly braces unless you're enclosing an expression, for example <x>{ current-time() }</x>. The return expression is not enclosed.
With MarkLogic it's best to leave the first parameter of fn:error empty. That way you don't have to worry about a QName, and anyway some folks believe that it's reserved for predefined errors. MarkLogic uses the second parameter to fill in error:code, and the third parameter for data.
For more on fn:error, see http://docs.marklogic.com/fn:error and https://github.com/robwhitby/xray/pull/11

how to build string iteratively in xquery

I need the xquery structure which is the same with java code
string temp
for(int i=0,i<string[].length,i++)
temp=temp+string[i]
for example, in xquery, I have string /a/b/c I need to something like
let $temp:=""
for $x in tokenize(string,'/')
$temp=concat($temp,$x)
return $temp
and it should return the following at each iterate
a
ab
abc
but somehow it seams that this statement $temp=concat($temp,$x) is not working. so what's the right syntax to do this? Thanks in advance
I think, you need to get the notion of declarative programming. You are trying to tell the processor what to do (like you would do in java) instead of describing the overall result. For example, if you don't use the scripting extension (which is only supported by some processors, e.g. zorba) you cannot use assignments the way you would use them in java. Think of it as the complete query describing one resulting document.
This stuff is hard to get in the beginning, but it brings huge benefits in the end (productivity, robustness, performance).
I would translate your imperative pseudo code into this one-liner:
string-join(tokenize("/a/b/c",'/'))
You can test it on try.zorba-xquery.com. I really hope this helps. Sorry, if this is not the answer you were looking for...
The $temp=conct($temp, $x) doesn't accumulate because in XQuery, that's a new variable each time through the loop. Try the following (tested in MarkLogic but uses all standard syntax):
declare function local:build($prefix, $tokens)
{
if (fn:exists($tokens)) then
let $str := fn:concat($prefix, $tokens[1])
return (
$str,
local:build($str, fn:subsequence($tokens, 2))
)
else ()
};
let $string := "/a/b/c"
return local:build("", fn:tokenize($string, "/"))

Resources