Search code examples
xmlxquerymarklogic

Marklogic/XQuery - Cannot limit results to Date Descending and use the element XML tag


I would like to use the "element xml {" syntax. However, I have been received error:

XDMP-UNEXPECTED: (err:XPST0003) Unexpected token syntax error, unexpected Rbrace_, expecting Function30_ or Percent

and the last right brace is highlighted. I have tried unquote, but this does not give the correct effect.

I have attached the code below.

xquery version "1.0-ml";
element xml {

let $sorted-articles :=
for $item in (doc()//article)
where ($item//article-meta//article-categories//subj-group[@subj-group-type="content-type"]//subject) = "MyNews"
and ($item//journal-title) = "MyJournal"
order by $item//pub-date/year descending, $item//pub-date/month descending, $item//pub-date/day descending
return $item

for $item at $count in subsequence($sorted-articles, 1, 5)
    return
      element article {
       element journal-title { $item//journal-title },
        element title { $item//article-title },
        element byline { $item//contrib/string-name },
        element body { $item//body },
      element images {
       for $graphic in $item//body//graphic
          return element image { ($graphic//@*[name()="xlink:href"]/data()) }}
          ,element year {$item//pub-date/year/string()},
        element month {$item//pub-date/month/string()},
        element day {$item//pub-date/day/string()},
        element doi { $item//article-id[@pub-id-type="doi"]/text() },
        element doitrim {substring-after($item//article-id[@pub-id-type="doi"]/text(),"/")}
};
};

Solution

  • You have redundant semi-colons at the end of your code, try this instead:

    xquery version "1.0-ml";
    element xml {
    
    let $sorted-articles :=
    for $item in (doc()//article)
    where ($item//article-meta//article-categories//subj-group[@subj-group-type="content-type"]//subject) = "MyNews"
    and ($item//journal-title) = "MyJournal"
    order by $item//pub-date/year descending, $item//pub-date/month descending, $item//pub-date/day descending
    return $item
    
    for $item at $count in subsequence($sorted-articles, 1, 5)
        return
          element article {
           element journal-title { $item//journal-title },
            element title { $item//article-title },
            element byline { $item//contrib/string-name },
            element body { $item//body },
          element images {
           for $graphic in $item//body//graphic
              return element image { ($graphic//@*[name()="xlink:href"]/data()) }}
              ,element year {$item//pub-date/year/string()},
            element month {$item//pub-date/month/string()},
            element day {$item//pub-date/day/string()},
            element doi { $item//article-id[@pub-id-type="doi"]/text() },
            element doitrim {substring-after($item//article-id[@pub-id-type="doi"]/text(),"/")}
    }
    }
    

    HTH!