简体   繁体   中英

XQuery: Change variable inside return

I am currently working on a project on XQuery, I am new to this type of language. In my code, I am trying to set a variable to 1 after a condition is met, in the full code, this condition is inside a for loop .

<foo>{
        let $i := 0
        return(
            if (condition) then (
                $i := 1,
                <test>{$i}</test>
            ) else ())
}</foo>

Unfortunately, this is not possible in XQuery, I get the following error: XPST0003 XQuery syntax error near #...then ( $i:= 1#: expected ")", found ":="

For the full context, I am using the DBLP database, and trying to calculate the Bacon Number (the "distance" between 2 authors) using XQuery. I thought of doing a Breadth-first search .

Could you orient me on the correct way of doing this? I know my way of thinking is more oriented on functional programming rather than declarative programming, so I would love to learn some tricks !

You need to describe the problem you are trying to solve, not your faulty attempt to solve it. How can we reverse engineer your requirements from code that doesn't work?

You've given a very high-level description (the Bacon Number) but that's not useful except to someone who knows what the Bacon Number rules are and what the XML looks like.

It sounds like it's some kind of algorithm for finding the distance between two nodes in a graph. Unfortunately an awful lot of the published algorithms for that kind of thing use procedural approaches, and it takes some experience to create an equivalent solution in a functional language.

The techniques invariably make heavy use of recursion. In my book on XSLT programming I have a couple of examples for detecting whether a graph contains cycles, which is a very similar problem (and at an abstract level, a similar language). Writing a breadth-first search as a recursive function isn't difficult.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM