Your code would be easier to read if you used literal result elements rather
than element constructors, e.g.:
<policy>
<policy_num>{$policy_num}</policy_num>
<number_of_records>{$num_recs}</number_of_records>
<records>{
local:recordsByPolicy($policy_num)
}</records>
</policy>
As a general rule, it’s only necessary (or useful) to use element constructors
when the element type is dynamically determined.
Cheers,
Eliot
--
Eliot Kimber
http://contrext.com
From: <[email protected]> on behalf of Matt Moody
<[email protected]>
Reply-To: MarkLogic Developer Discussion <[email protected]>
Date: Wednesday, October 4, 2017 at 7:08 PM
To: MarkLogic Developer Discussion <[email protected]>
Subject: [MarkLogic Dev General] Trouble with syntax... trying to return XML
tree
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
[email protected]
Mobile: +61 (0)415 564 355
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
[email protected]
Manage your subscription at:
http://developer.marklogic.com/mailman/listinfo/general