2014-10-16 116 views
0

我使用Ubuntu的Basex 8.0測試版從basex獨立的XQuery 14.04需要幫助執行

我的數據庫中包含的XML文檔與這個樣子

<messages> 
    <message>  
    <message-time-stamp> 
     <date>20141004</date> 
     <time>193843</time> 
     <utc-offset>-0400</utc-offset> 
    </message-time-stamp> 
    </message> 
</messages> 

我可以使用元素BaseX gui中的以下xquery使用級聯時間戳更新所有消息時間戳記節點

declare variable $data := db:open('testdb'); 

    (: Find all elements with time and return the parent element :) 
    for $n in $data//../time/.. 
    return (
    if (exists($n/utc-time-stamp)) then() 
    else insert node 
    <utc-time-stamp> 
    {fn:concat (
     fn:substring($n/date, 1, 4),"-", 
     fn:substring($n/date, 5, 2),"-", 
     fn:substring($n/date, 7, 2),"T", 
     fn:substring($n/time, 1, 2),":", 
     fn:substring($n/time, 3, 2),":", 
     fn:substring($n/time, 5, 2), 
     fn:substring($n/utc-offset, 1, 3), 
     fn:substring($n/utc-offset, 4, 2))} 
    </utc-time-stamp> into $n 
) 

我在basex腳本test_bxs.xqy嵌入查詢,如下所示:

<commands> 
    <xquery> 
    declare variable $data := db:open('testdb'); 
    (: Find all elements with time and return the parent element :) 
    for $n in $data//../time/.. 
    return (
    if (exists($n/utc-time-stamp)) then() 
    else insert node 
    <utc-time-stamp> 
    {fn:concat (
     fn:substring($n/date, 1, 4),"-", 
     fn:substring($n/date, 5, 2),"-", 
     fn:substring($n/date, 7, 2),"T", 
     fn:substring($n/time, 1, 2),":", 
     fn:substring($n/time, 3, 2),":", 
     fn:substring($n/time, 5, 2), 
     fn:substring($n/utc-offset, 1, 3), 
     fn:substring($n/utc-offset, 4, 2))} 
    </utc-time-stamp> into $n 
) 
    </xquery> 
</commands> 

,並執行如下:

$ basex -d -c test_bxs.xqy 
org.basex.query.QueryException: Syntax: <xquery>[query]</xquery> 
     at org.basex.core.parse.XMLParser.error(XMLParser.java:323) 
     at org.basex.core.parse.XMLParser.check(XMLParser.java:313) 
     at org.basex.core.parse.XMLParser.command(XMLParser.java:167) 
     at org.basex.core.parse.XMLParser.parse(XMLParser.java:47) 
     at org.basex.core.parse.CmdParser.parse(CmdParser.java:57) 
     at org.basex.core.parse.CommandParser.parse(CommandParser.java:75) 
     at org.basex.core.parse.CommandParser.parse(CommandParser.java:54) 
     at org.basex.core.CLI.execute(CLI.java:88) 
     at org.basex.core.CLI.execute(CLI.java:77) 
     at org.basex.BaseX.<init>(BaseX.java:85) 
     at org.basex.BaseX.main(BaseX.java:42) 
org.basex.core.BaseXException: Syntax: <xquery>[query]</xquery> 
     at org.basex.core.CLI.execute(CLI.java:93) 
     at org.basex.core.CLI.execute(CLI.java:77) 
     at org.basex.BaseX.<init>(BaseX.java:85) 
     at org.basex.BaseX.main(BaseX.java:42) 
Caused by: org.basex.query.QueryException: Syntax: <xquery>[query]</xquery> 
     at org.basex.core.parse.XMLParser.error(XMLParser.java:323) 
     at org.basex.core.parse.XMLParser.check(XMLParser.java:313) 
     at org.basex.core.parse.XMLParser.command(XMLParser.java:167) 
     at org.basex.core.parse.XMLParser.parse(XMLParser.java:47) 
     at org.basex.core.parse.CmdParser.parse(CmdParser.java:57) 
     at org.basex.core.parse.CommandParser.parse(CommandParser.java:75) 
     at org.basex.core.parse.CommandParser.parse(CommandParser.java:54) 
     at org.basex.core.CLI.execute(CLI.java:88) 
     ... 3 more 
Syntax: <xquery>[query]</xquery> 

如果我刪除插入語句並插入一個db:output(「。」),腳本可以在命令行中正常運行,但是co urse不更新數據庫。

<commands> 
    <xquery> 
    declare variable $data := db:open('testdb'); 
    (: Find all elements with time and return the parent element :) 
    for $n in $data//../time/.. 
    return (
    if (exists($n/utc-time-stamp)) then (db:output(".")) 
    else (db:output(".")) 
) 
    </xquery> 
</commands> 

由於腳本作爲預期在GUI中進行,從命令行上使用時,我期望類似的結果。

任何幫助,將不勝感激提前

感謝

回答

1

BaseX命令腳本應該總是有.bxs文件結尾爲outlined in the documentation

您的方法失敗的原因是在XML命令腳本中不能使用XML元素,因爲腳本語言本身由XML元素組成。但是,因爲它似乎你只需要執行您的XQuery文件,所以簡單地存儲的XQuery本身(即沒有周圍的命令和它的元素)的文件,並

basex test_bxs.xqy 

假設您覆蓋現有的文件中運行它。

+0

感謝您的快速回復。 – user1836694 2014-10-16 16:16:16

+0

其實正確的命令是$ basex test.xqy。現在我讀了文件,很清楚。但是,這並不能解釋爲什麼當我刪除'insert'語句時腳本工作。 – user1836694 2014-10-16 16:28:37

+0

當然,對不起,這只是一個錯字。我想你不只是刪除insert關鍵字,而是整個語句(即包括concat和utc-timestamp)。這樣,查詢中就不會出現任何元素,這應該可以正常工作 – dirkk 2014-10-16 16:31:31