Hi Arpitha,

How about something like this?

xquery version "1.0-ml";

declare function local:wrap-children($Record, $Records) {
    let $match-pattern := concat('^', $Record/sort, '\.[^.]+$')
    return
    element {name($Record)} {
        $Record/@*,
        $Record/node(),
        for $child in $Records[matches(sort, $match-pattern)]
        return
            local:wrap-children($child, $Records)
    }
};

let $xml :=
<Records>
<Record><sort>0.0.a</sort></Record>
<Record><sort>0.0.a.a</sort></Record>
<Record><sort>0.0.a.b</sort></Record>
<Record><sort>0.0.a.c</sort></Record>
<Record><sort>0.0.b</sort></Record>
<Record><sort>0.0.b.a</sort></Record>
<Record><sort>0.0.b.a.b</sort></Record>
<Record><sort>0.0.b.a.b.c</sort></Record>
<Record><sort>0.0.b.b</sort></Record>
<Record><sort>0.0.b.c</sort></Record>
<Record><sort>0.0</sort></Record>
</Records>

let $expected-result :=
<Records>
  <Record>
    <sort>0.0</sort>
    <Record>
       <sort>0.0.a</sort>
      <Record>
        <sort>0.0.a.a  </sort>
       </Record>
       <Record>
         <sort>0.0.a.b</sort>
        </Record>
        <Record>
         <sort>0.0.a.c</sort>
        </Record>
     </Record>
  </Record>
  <Record>
      <sort>0.0.b</sort>
          <Record>
        <sort>0.0.b.a</sort>
        <Record>
        <sort>0.0.b.a.b</sort>
        <Record>
        <sort>0.0.b.a.b.c</sort>
        </Record>
          </Record>
       </Record>
   </Record>
</Records>

let $new :=
    <new> {
        for $i in $xml//Record
        order by $i/sort
        return $i
    }</new>

let $result :=
    <Records>{
        local:wrap-children($new/Record[1], $new/Record)
    }</Records>

return (
    deep-equal($result, $expected-result),
    $result
)

Kind regards,
Geert

>


Drs. G.P.H. Josten
Consultant


http://www.daidalos.nl/
Daidalos BV
Source of Innovation
Hoekeindsehof 1-4
2665 JZ Bleiswijk
Tel.: +31 (0) 10 850 1200
Fax: +31 (0) 10 850 1199
http://www.daidalos.nl/
KvK 27164984
De informatie - verzonden in of met dit emailbericht - is afkomstig van 
Daidalos BV en is uitsluitend bestemd voor de geadresseerde. Indien u dit 
bericht onbedoeld hebt ontvangen, verzoeken wij u het te verwijderen. Aan dit 
bericht kunnen geen rechten worden ontleend.


> From: [email protected]
> [mailto:[email protected]] On Behalf Of
> Arpitha Chittumalla
> Sent: vrijdag 29 januari 2010 8:21
> To: [email protected]
> Subject: [MarkLogic Dev General] Re: General Digest, Vol 67, Issue 28
>
> Hi Andrew,
>
> Here I am providing the well formed XML  and my Code with the out put.
> I  succeeded 95%.
> But at one level  I missed the expected output. It seems to be I
> missed some logic while arranging the records, so can you suggest me.
>

*snip*

_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

Reply via email to