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.
Input XML File : <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> My XQuery code: xquery version "1.0-ml"; declare namespace cpf = "http://cpfnamespace" ; (: ----------------------------------------------------------- Function cpf:getChilds() used for recursion. ----------------------------------------------------------:) declare function cpf:getNodes($currentnode,$newnode,$pos) { if ((fn:string-length($currentnode//sort/text())< fn:string-length($newnode/Record[$pos+1]//sort/text()))) then ( <Record> {$currentnode, cpf:getNodes($newnode/Record[$pos+1],$newnode,$pos+1) } </Record> ) else if ((fn:string-length($currentnode//sort/text()) = fn:string-length($newnode/Record[$pos+1]//sort/text()))) then ( $currentnode, cpf:getNodes($newnode/Record[$pos+1],$newnode,$pos+1) ) else ( $newnode/Record[$pos],<pos>{$pos}</pos>) }; (: ----------------------------------------------------------- Function cpf:getChilds() checking the child nodes and if any then cpf:getChilds() calling the getNodes() for recursion. ----------------------------------------------------------:) declare function cpf:getChilds($currentnode,$newnode,$pos) { if ((fn:string-length($currentnode//sort/text())< fn:string-length($newnode/Record[$pos+1]//sort/text()))) then ( <child> { cpf:getNodes($newnode/Record[$pos],$newnode,$pos) } </child>) else ( ) }; <result> { let $new :=<new> { for $i in doc("SortTest.xml")//Record order by $i/sort return $i }</new> let $prevpos := 0 for $i at $pos in $new//Record let $flag :=true() let $result := cpf:getChilds($i,$new,$pos+1) return ( if ( ($result//text() ) and ($prevpos < $pos) ) then ( <Records> { $i ,xdmp:set($prevpos,$result//pos/text()) ,cpf:getChilds($i,$new,$pos+1)} </Records>) else ( ) ) }</result> > ---------------------------------------------------------------------- > Date: Fri, 22 Jan 2010 09:48:05 +0000 > From: Andrew Welch <[email protected]> > Subject: Re: [MarkLogic Dev General] sort and group the records based > on number sorting. > To: General Mark Logic Developer Discussion > <[email protected]> > Message-ID: > <[email protected]> > Content-Type: text/plain; charset=ISO-8859-1 > > Your input isn't well-formed (spurious <Chapter>), it's not indented > to make it readable (if it was you would have spotted the mistake), > you haven't given any clues as to how it should be sorted other than > providing output that doesn't correspond to the input, and you haven't > posted any existing code to show where you are going wrong. >
SampleXML.doc
Description: MS-Word document
_______________________________________________ General mailing list [email protected] http://xqzone.com/mailman/listinfo/general
