retrieving an array from entity services - Marklogic

Viewed 15

i am trying to follow some instructions for creating entity services: https://docs.marklogic.com/guide/entity-services.pdf They mention in this document the possibility to create and array. I prepare my description file, conversion file, and run the code, but it doesn't create the array like i want it to. It actually creates many arrays, each with one value in it. I don't know how this happend and how i can fix it. This is based on the Data hub tutorial data Basically, i want to createan instance, for the auto insurance, where i get an array of , each in its own element for the insurance policies for each of the cars that might be owned by someone. What i would like to have, assuming the person has 3 cars:

<policies>
 <policy>
     <policyType>Mercedes-Benz</policyType>
     <policyNumber>40-294-9278</policyNumber>
     <policyZip>18706</policyZip>
 </policy>
 <policy>
     <policyType>Mitsubishi</policyType>
     <policyNumber>17-516-9783</policyNumber>
     <policyZip>18706</policyZip>
 </policy>
 <policy>
     <policyType>Subaru</policyType>
     <policyNumber>71-370-4976</policyNumber>
     <policyZip>18706</policyZip>
 </policy>
</policies>

but instead, i get this:

<policies datatype="array">
   <policy>
    <policyType>Mercedes-Benz</policyType>
    <policyNumber>40-294-9278</policyNumber>
   </policy>
</policies>
<policies datatype="array">
   <policy>
    <policyType>Mitsubishi</policyType>
    <policyNumber>17-516-9783</policyNumber>
   </policy>
</policies>
<policies datatype="array">
   <policy>
    <policyType>Subaru</policyType>
    <policyNumber>71-370-4976</policyNumber>
   </policy>
</policies>

I tried chaning many settings but cannot get to it. Can someone please help?

here is the description (just the part about the policies array, because rest works fine)

       <policies>
          <es:datatype>array</es:datatype>
          <es:items><es:ref>#/definitions/policy</es:ref></es:items>
      </policies>

and a bit lower in the description file i have the definition of the array:

<policy>
 <es:properties>

   <policyType>
     <es:datatype>string</es:datatype>
     <es:facetable>false</es:facetable>
     <es:sortable>false</es:sortable>
     <es:collation>http://marklogic.com/collation/codepoint</es:collation>
   </policyType>

   <policyNumber>
     <es:datatype>string</es:datatype>
     <es:facetable>false</es:facetable>
     <es:sortable>false</es:sortable>
     <es:collation>http://marklogic.com/collation/codepoint</es:collation>
   </policyNumber>

   <policyZip>
     <es:datatype>string</es:datatype>
     <es:facetable>false</es:facetable>
     <es:sortable>false</es:sortable>
     <es:collation>http://marklogic.com/collation/codepoint</es:collation>
   </policyZip>

  </es:properties>
 </policy>

i generate the conversion file and modify it to meet the underlying data:

    let $policies  :=             es:extract-array($source-node/policies, auto:extract-instance-policy#1)

and the auto:extract-instance-policy function:

 declare function auto:extract-instance-policy(
     $source as item()?
     ) as map:map
  {
      let $source-node := es:init-source($source, 'policy')
      let $policyType  :=             $source-node/make[not(. instance of null-node())] ! xs:string(.)
      let $policyNumber  :=             $source-node/number[not(. instance of null-node())] ! xs:string(.)
      let $policyZip  :=             $source/addrZip[not(. instance of null-node())] ! xs:string(.)
      let $instance := es:init-instance($source-node, 'policy')
      (: Comment or remove the following line to suppress attachments :)
      =>es:add-attachments($source)

      return
        if (empty($source-node/*))
        then $instance
        else $instance
          =>es:optional('policyType', $policyType)
          =>es:optional('policyNumber', $policyNumber)
          =>es:optional('policyZip', $policyZip)
     };

but it doesn't return it as i would like to have it, it returns many policies arrays

the source for the auto insurance example:

{
   "addrCity": "Wilkes Barre", 
   "addrState": "PA", 
   "addrStreet": "21156 Alpine Terrace", 
   "addrZip": "18706", 
   "birthDate": "10/19/1977", 
   "email": "yfridayeqz@e-recht24.de", 
   "ssn": "827-54-4139", 
   "policies": [
    {
     "make": "Mercedes-Benz", 
     "model": "E-Class", 
     "number": "40-294-9278", 
     "vin": "WA1VKAFP6AA688103", 
     "year": 2008
    }, 
    {
     "make": "Mitsubishi", 
     "model": "Lancer", 
     "number": "17-516-9783", 
     "vin": "WBAPK5C51AA695692", 
     "year": 2009
   }, 
   {
     "make": "Subaru", 
     "model": "XT", 
     "number": "71-370-4976", 
     "vin": "WA1CGAFP1EA477275", 
     "year": 1989
   }
  ]
 }

so what am i doing wrong? How can i get the output i would like to have

thanks

0 Answers
Related