The reason for the XDMP-UNEXPECTED error is that your query has no return 
statement (although the code is formatted to look as if it does). In 
pseudo-code, the main query is (ignoring the variable declarations):

let $emails_with multiple_policies :=
  for [iteration]
  let [variable assignment]
  where [condition]
  return
    ( stuff to return)

The return clause is parsed as part of the variable assignment for $emails_with 
multiple_policies. Thus the main query itself has no return clause and is 
incomplete. To fix that you probably want to add one line to the variable 
assignment so that it reads

let $emails_with_multiple_policies :=
    for $em at $i in 
fn:distinct-values(fn:collection($coll)//insured_email/text())
    let $policies := 
fn:count(fn:distinct-values(fn:collection($coll)//policy[insured_email/text() = 
$em]/policy_num/text()))
    where $policies > 1
    return $em

The query still won’t succeed as there are undefined variables remaining but 
hopefully that sets things on the right track.

David S.

> On Oct 4, 2017, at 8:08 PM, Matt Moody <matt.mo...@marklogic.com> wrote:
> 
> I am getting the error XDMP-UNEXPECTED: (err:XPST0003) Unexpected token 
> syntax error, unexpected $end from the below query, and cannot figure out why.
>  
> Any ideas would be appreciated!
>  
> The idea here, is that an Email may show up in multiple Policy Records, and 
> that same Email may be linked to multiple Policy Numbers, and each Policy 
> Number may show up in multiple Records. I want to use this query to return 
> all Emails with multiple Policies, return each Policy number linked to the 
> Email, and then show each Record (Document) where that Policy number is 
> contained.
>  
> xquery version "1.0-ml";
>  
> declare variable $coll := "insurance-policies ";
>  
> declare function local:recordsByPolicy($policyNum as xs:string) {(
>     for $doc in fn:collection($coll)//policy[policy_num/text() = $policyNum]
>     return element document {
>         element doc_uri { xdmp:node-uri($doc) }
>     }
> )};
>  
> declare function local:policiesByEmail($sourceEmail as xs:string) {(
>     for $policy_num in 
> fn:distinct-values(fn:collection($coll)//policy[insured_email/text() = 
> $email]/policy_num/text())
>     let $num_recs := fn:count(fn:collection($coll)//policy[policy_num/text() 
> = $policy_num])
>     order by $num_recs descending
>     return element policy {
>         element policy_num {$policy_num},
>         element number_of_records {$num_recs},
>         element records {(
>             local:recordsByPolicy($policy_num)
>         )}
>     }
> )};
>  
> let $emails_with_multiple_policies :=
>     for $em at $i in 
> fn:distinct-values(fn:collection($coll)//insured_email/text())
>     let $policies := 
> fn:count(fn:distinct-values(fn:collection($coll)//policy[insured_email/text() 
> = $em]/policy_num/text()))
>     where $policies > 1
>  
> return (
>     element total_source_records {(fn:count(fn:collection($coll)))},
>     element unique_source_emails {(fn:count($unique_source_emails))},
>     element emails_w_mpolicies {(fn:count($emails_with_multiple_policies))},
>     element results {(
>         for $email in 
> fn:distinct-values(fn:collection($coll)//insured_email/text())
>         let $num_policies := 
> fn:count(fn:distinct-values(fn:collection($coll)//policy[insured_email/text() 
> = $email]/policy_num/text()))
>         where $num_policies > 1
>         order by $num_policies descending
>         return element result {
>             element email {$email},
>             element number_of_policies_found {$num_policies},
>             element policies {(
>                 local:policiesByEmail($email)
>             )}
>         }
>     )}
> )
>  
>  
>  
>  
>  
>  
>  
>  
> Matt Moody
> Sales Engineer
> MarkLogic Corporation
> matt.mo...@marklogic.com <mailto:matt.mo...@marklogic.com>
> Mobile: +61 (0)415 564 355
>  
> <image001.jpg> <https://world.marklogic.com/>
>  
> This e-mail and any accompanying attachments are confidential. The 
> information is intended solely for the use of the individual to whom it is 
> addressed. Any review, disclosure, copying, distribution, or use of this 
> e-mail communication by others is strictly prohibited. If you are not the 
> intended recipient, please notify us immediately by returning this message to 
> the sender and delete all copies. Thank you for your cooperation.
> _______________________________________________
> General mailing list
> General@developer.marklogic.com <mailto:General@developer.marklogic.com>
> Manage your subscription at: 
> http://developer.marklogic.com/mailman/listinfo/general 
> <http://developer.marklogic.com/mailman/listinfo/general>

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
General mailing list
General@developer.marklogic.com
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to