Hi Michael, 

Thanks for looking, whole script attached.

Is a variable set in an outer loop, so last child of $a/xm:meta should still
evaluate at run time to the same thing. I suspect what is happening is that
the updates are not committed in the same order that they are placed in the
transaction, which is a bit odd.

let $tax := /xa:auth[@id='ae562ce5-6d71-1014-90bf-c1927c3ed365']
let $tax2 := <x>{
        for $a at $ix in
$tax/descendant-or-self::xa:auth[xm:meta/xm:field[@type='old-name']]
        return
                <a id="{$a/@id}" name="{ $a/xm:meta/xm:field[@type='name']
}" index="{ $ix }">
                {
                (       for $mo in $a/xm:meta/xm:field[@type='old-name']
                return
                                <old-name>{ normalize-space($mo)
}</old-name>
                )
                ,
                (
                        for $p in $a/ancestor::xa:auth
                        return
                                        <p id="{ $p/@id }" />
                )
                }
                </a>
                        
}</x>
let $journals-ids := (@@ids@@)
for $sid in $journals-ids
return
(       concat('\n\nSOURCE-ID=', $sid ,'\n'),
        let $auths := (
                (: get abstract linked to concrete - id any :)
                for $a in /xa:auth[@source-id = $sid and (@type='commentary'
or @type='commentary-doc')]
                return
                (
                        $a,
                        for $cal in $a//xa:link[ends-with(@link-type,'-CA')
and @dest-id]
                        return
                                /xa:auth[@id=$cal/@dest-id]
                )
        )
        for $a in $auths
        return
        (
                (: delete any existing keyword links :)
                (
                        for $ex in $a/xm:meta/xm:field[@type='subject-link']
                        return
                                xdmp:node-delete($ex)
                )
                ,
                concat('AUTH-ID=', $a/@id, '\n'),
                (
                        for $sub in $a/xm:meta/xm:field[@type='keyword']
                        let $csub := normalize-space($sub)
                        let $keys := $tax2//a[old-name = $csub] 
                        return
                        (
                                concat('s=', $sub, '\n'),
                                if ($keys) then
                                        for $k in $keys
                                        return
                                                concat(' - NEW=',
string($k/@name), '\n')
                                else
                                        '  - NO MATCH - \n'
                        )
                )
                ,                                                       
                let $keysall := (
                        for $sub in $a/xm:meta/xm:field[@type='keyword']
                let $csub := normalize-space($sub)
                        return
                                $tax2//a[old-name = $csub]
                )
                let $allpars := (for $k in $keysall return for $p in $k/p
return $p/@id)
                let $keys := distinct-values(
                                (: don't add id if there is already a lower
level item in the keys set :)
                                for $k in $keysall
                                return
                                        if (not(
                                                index-of($allpars, $k/@id)
                                        )) then
                                                $k/@id
                                        else
                                                ()
                        )
                return
                (
                        if ($keys) then
                        (
                                for $k in (
                                        for $kk in $keys
                                        return
                                                $tax2//a[@id=$kk]
                                        )
                                order by xs:integer($k/@index) descending
                                return
                                (
                                        concat(' - ACTUAL=', $k/@name, ' -
', $k/@id, ' - ', $k/@index, '&#13;&#10;'),
                                        xdmp:node-insert-child($a/xm:meta, 
                                                <xm:field
type='subject-link'>
                                                        <xa:link
link-type='{ concat($a/@type, "-taxonomy-subject-law") }' dest-id='{ $k/@id
}' />
                                                </xm:field>
                                        )
                                )
                                                                        
                        ) else ()
                )
        )               
)

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Michael
Blakeley
Sent: 19 September 2012 17:22
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] Multiple inserts, maintaining order

The heart of the update expression is something like:

    xdmp:node-insert-child($a/xm:meta, $new-node)

Each node-insert-child should add a node to the end of the existing children
of $a/xm:meta, like a FIFO. But "$a" is undefined, so it's difficult to
predict how this will evaluate. If you can provide a working test case,
someone here can probably tell you what is going awry.

-- Mike

On 19 Sep 2012, at 08:42 , Dominic Beesley wrote:

> Hi Damon,
>  
> Thanks for the suggestion. I think something along those lines is the way
I'll have to go, just seems a bit odd having to rebuild a whole, quite large
item rather than being able to poke a few in at the end!
>  
> Thanks
>  
> Dom
>  
> From: [email protected] 
> [mailto:[email protected]] On Behalf OfDamon 
> Feldman
> Sent: 19 September 2012 14:20
> To: MarkLogic Developer Discussion
> Subject: Re: [MarkLogic Dev General] Multiple inserts, maintaining 
> order
>  
> Dominic,
>  
> You could instead build the sequence, merge it together and then replace
the whole meta node. Un-tested code:
>  
> Let $new-fields := for .. return <xm:field type='subject-link'> 
> <xa:link Let $old-meta := . Let $old-fields := $old-meta//xm:field Let 
> $new-fields := ($old-fields, $new-fields) Let $new-meta :=
>   <xm:meta> {
>     {$old-meta/@*}  (: keep all old attributes :)
>     {$old-meta/node()[local-name(.) ne "field"]  (: keep all nodes except
fields :)
>     {$new-fields} (: add merged fields at the bottom :) } </xm:meta>
>  
> If you need the xm:meta to have the fields in the middle somewhere, that
can be done, but is trickier.
>  
> Yours,
> Damon
>  
> From: [email protected] 
> [mailto:[email protected]] On Behalf OfDominic 
> Beesley
> Sent: Wednesday, September 19, 2012 9:08 AM
> To: 'MarkLogic Developer Discussion'
> Subject: [MarkLogic Dev General] Multiple inserts, maintaining order
>  
> Hello,
>  
> I'm trying to insert multiple nodes into a document, the nodes are getting
inserted ok but the order is jumbled up.
>  
> I'm guessing this is something to do with how transactions are committed
after the script has finished.
>  
> Is there an easy way round this or a way of building a set of nodes in the
correct order and inserting them in one go to the parent?
>  
> Thanks
>  
> Dom
>  
> Snippet:
>  
> for $k in (
> for $kk in $keys
>                 return
>                 $tax2//a[@id=$kk]
>                 )
>                 order by xs:integer($k/@index) descending return (
>                 concat(' - ACTUAL=', $k/@name, ' - ', $k/@id, ' - ',
$k/@index, '&#13;&#10;'),
>                 xdmp:node-insert-child( $a/xm:meta,
>                                 <xm:field type='subject-link'>
>                                                 <xa:link link-type='{
concat($a/@type, "-taxonomy-subject-law") }' dest-id='{ $k/@id }' />
>                                 </xm:field>
>                 )
> )
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general

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

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

Reply via email to