XQuery/同步遠端集合
外觀
< XQuery
您想要更新在集合中比另一個集合更新或新的專案。
許多資料庫將建立日期和上次修改日期與資源一起儲存。這些日期可以用來檢視本地集合是否與遠端集合不同步。可以編寫一個 XQuery 指令碼,它將只列出比本地集合上的建立日期更新或新的檔案。
對於 eXist 資料庫,以下是用來訪問時間戳的兩個函式。
xmldb:last-modified($collection, $resource) xmldb:created($collection, $resource)
其中
$collection is the path to the collection (xs:string) $resource is the name of the resource (xs:string)
例如
let $my-file-last-modified := xmldb:last-modified('/db/test', 'myfile.xml')
將返回集合 /db/text 中檔案 myfile.xml 的上次修改日期和時間。時間戳的格式為 XML Schema dateTime 格式
"2009-06-04T07:50:04.828-05:00"
例如,這表示時間為 2009 年 6 月 4 日上午 7:50,對於比協調世界時 (UTC) 落後 5 小時的中央標準時間。
您可以將 xmldb:last-modified() 函式與另一個函式 xmldb:get-child-collections($collection) 相結合,該函式返回當前集合的所有子集合。透過使用尾遞迴呼叫自身,您可以找到集合及其所有子集合中所有最後修改的日期和時間。
這是一個示例 XQuery 函式,它返回集合及其所有子集合中所有資源的最後修改日期和時間列表。
declare function local:collection-last-modified($collection as xs:string) as node()* {
<collection>
{attribute {'cid'} {$collection} }
{for $resource in xmldb:get-child-resources($collection)
return
<resource>
{attribute {'id'} {$resource}}
{attribute {'last-modified'} {xmldb:last-modified($collection, $resource)}}
</resource>,
if (exists(xmldb:get-child-collections($collection)))
then (
for $child in xmldb:get-child-collections($collection)
order by $child
return
(: note the recursion here :)
local:collection-last-modified(concat($collection, '/', $child))
)
else ()
}
</collection>
};
請注意,向每個資源添加了兩個屬性。一個是資源 ID,它在每個集合中必須是唯一的,另一個是資源上次修改的日期。
您可以透過簡單地傳遞要開始的集合根來呼叫此函式。
xquery version "1.0";
let $collection := '/db/test'
return
<last-modified-report>
{local:collection-last-modified($collection)}
</last-modified-report>
這將返回以下檔案
<last-modified-report>
<collection cid="/db/test">
<resource id="get-remote-collection.xq" last-modified="2009-04-29T08:16:06.104-05:00"/>
<collection cid="/db/test/views">
<resource id="get-site-mod-dates.xq" last-modified="2009-04-30T09:01:58.599-05:00"/>
<resource id="site-last-modified.xq" last-modified="2009-04-30T09:07:10.016-05:00"/>
</collection>
</collection>
</last-modified-report>
您現在可以使用這些轉換來建立批處理檔案,這些檔案將只傳輸已更改或新的檔案。
許多資料庫提供 Apache Ant 任務,這些任務具有提取和儲存操作的功能。
這是一個示例 Apache Ant 目標,它在本地檔案上執行提取操作,並將結果儲存在遠端檔案上。
<target name="push-bananas">
<xdb:extract
xmlns:xdb="http://exist-db.org/ant"
uri="xmldb:exist://${local-host}/exist/xmlrpc/db/test/sync"
resource="bananas.xml"
destfile="C:/backup/db/test/sync/bananas.xml"
user="${local-user}"
password="${local-pass}"
/>
<xdb:store
xmlns:xdb="http://exist-db.org/ant"
uri="xmldb:exist://${remote-host}/exist/xmlrpc/db/test/sync"
srcfile="/backup/db/test/sync/bananas.xml" createcollection="true"
user="${remote-user}"
password="${remote-pass}"
/>
</target>
請注意,此 Ant 檔案中必須設定以下屬性。
<property name="local-host" value="localhost"/>
<property name="local-user" value="admin"/>
<property name="local-pass" value="put-local-pw-here"/>
<property name="remote-host" value="example.com"/>
<property name="remote-user" value="admin"/>
<property name="remote-pass" value="put-remote-pw-here"/>