需要帮助从basex独立执行XQuery

时间:2014-10-16 15:21:36

标签: basex

我在ubuntu 14.04上使用Basex 8.0 beta

我的数据库包含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>

如果删除insert语句并插入db:output(&#34;。&#34;),脚本可以从命令行运行良好,但当然不会更新数据库。

<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 个答案:

答案 0 :(得分:1)

BaseX命令脚本的文件结尾始终应为.bxs outlined in the documentation

您的方法失败的原因是在XML命令脚本中您不能使用XML元素,因为脚本语言本身由XML元素组成。但是,由于您似乎只想执行xquery文件,因此只需将XQuery本身(即不包含命令和xquery元素)存储在文件中并使用

运行它
basex test_bxs.xqy

假设您覆盖现有文件。