简体   繁体   中英

not able to write for loop in XQuery MarkLogic

I am trying to write a for loop as:

for $conditions in $contents
return fn:concat("The value of contents is : ", $conditions)

However in place of pages its returning the values inside the $contents variable.

I am working on a mapping file in marklogic and trying to get the values of the elements from that mapping file.

"contents": {
      "sample": {
        "feature": ".........",
        "contents": "value1",
        "contents": {..................}

now I want to loop for the "contents" element from the mapping file but my for loop is returning me the whole "contents" map instead of giving output as: "The value of contents is: value1" Can anyone help in this regard?

Your question is not very clear, as mentioned by others as well, but let me hazard a guess. You sometimes want to count items. You can do that with something as simple as fn:count($books) .

In other cases you might want to keep track at which item number you are currently. In other programming languages you might write a for-loop which increments a variable with i = i + 1 . You can't do that in XQuery, since it is a functional language, and those typically do not allow this.

There are ways around in MarkLogic, but there is a simple trick. The FLWOR statement can keep track of the current position for you using the at keyword.

You use it like this:

for $item at $page in $books
return fn:concat("This is the page number... ", $page)

HTH!

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