跳轉到內容

XQuery/eXist 速查表

來自華夏公益教科書

匯入和宣告

[編輯 | 編輯原始碼]

XQuery 宣告

[編輯 | 編輯原始碼]
xquery version "1.0";

匯入模組

[編輯 | 編輯原始碼]
import module namespace geo="http://www.cems.uwe.ac.uk/exist/coord" at "coord.xqm";

eXist 基礎名稱空間

[編輯 | 編輯原始碼]
  declare namespace exist = "http://exist.sourceforge.net/NS/exist"; 

標準 eXist 模組

[編輯 | 編輯原始碼]

在 XQuery 指令碼中是可選的,但在 1.2 版本中的模組中是必需的,但在 1.3 版本中不是必需的。

 import module namespace xmldb = "http://exist-db.org/xquery/xmldb";
 import module namespace util = "http://exist-db.org/xquery/util";
 import module namespace request = "http://exist-db.org/xquery/request";
 import module namespace response = "http://exist-db.org/xquery/response";
 import module namespace session = "http://exist-db.org/xquery/session";
 import module namespace transform = "http://exist-db.org/xquery/transform";
 import module namespace text = "http://exist-db.org/xquery/text";
 import module namespace system = "http://exist-db.org/xquery/system";

宣告名稱空間

[編輯 | 編輯原始碼]
 declare namespace tx="http://www.transxchange.org.uk/";

宣告預設元素名稱空間

[編輯 | 編輯原始碼]
declare default element namespace "http://www.w3.org/1999/xhtml";

如果你使用預設名稱空間,請務必小心。這意味著沒有名稱空間字首的所有內容都會自動序列化到此名稱空間中。它還阻止你使用空名稱空間,除非你明確地將其宣告為 xmlns=""。請注意,你沒有將字首與預設名稱空間相關聯。

宣告預設函式名稱空間

[編輯 | 編輯原始碼]
declare default function namespace "http://www.transxchange.org.uk/";

宣告 Java 繫結名稱空間

[編輯 | 編輯原始碼]
declare namespace math="java:java.lang.Math";

請注意,由於安全問題,Java 繫結預設情況下是停用的。現在可以透過數學擴充套件模組呼叫數學函式。

序列化選項

[編輯 | 編輯原始碼]

輸出 XML 文件

[編輯 | 編輯原始碼]

這是預設值,但可以明確宣告。

declare option exist:serialize "method=xml media-type=text/xml omit-xml-declaration=no indent=yes";

輸出 SVG 文件

[編輯 | 編輯原始碼]
declare option exist:serialize "method=svg media-type=application/svg+xml omit-xml-declaration=no indent=yes";


輸出 XHTML 文件

[編輯 | 編輯原始碼]
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=no indent=yes 
        doctype-public=-//W3C//DTD XHTML 1.0 Transitional//EN
        doctype-system=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";

如果你的程式碼包含許多位於空名稱空間 (xmlns="") 中的 XML 片段,則有一種方法可以自動將空名稱空間中的所有內容轉換為自動放入 XHTML 名稱空間。

declare option exist:serialize "method=xhtml enforce-xhtml=yes";

輸出 HTML4 文件

[編輯 | 編輯原始碼]
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=no indent=yes 
        doctype-public=-//W3C//DTD HTML 4.01 Transitional//EN
        doctype-system=http://www.w3.org/TR/loose.dtd";

輸出 HTML5 文件

[編輯 | 編輯原始碼]
declare option exist:serialize "method=html5 media-type=text/html omit-xml-declaration=yes indent=yes";

輸出沒有 doctype 的 HTML 文件

[編輯 | 編輯原始碼]
declare option exist:serialize "method=html media-type=text/html omit-xml-declaration=yes indent=yes";

輸出沒有 doctype 的 XHTML 文件

[編輯 | 編輯原始碼]
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=yes indent=yes";

輸出沒有 doctype 的純文字文件

[編輯 | 編輯原始碼]
declare option exist:serialize "method=text media-type=text/plain omit-xml-declaration=yes";

輸出 KML 文件

[編輯 | 編輯原始碼]
declare option exist:serialize  "method=xhtml media-type=application/vnd.google-earth.kml+xml highlight-matches=none";

輸出 XForms 文件

[編輯 | 編輯原始碼]
declare option exist:serialize "method=xhtml media-type=text/xml indent=yes";

模組和函式

[編輯 | 編輯原始碼]

模組頭

[編輯 | 編輯原始碼]
module namespace c="http://www.cems.uwe.ac.uk/exist/coord";

函式宣告

[編輯 | 編輯原始碼]
  • 在 XQuery 指令碼中的預設名稱空間中
declare function local:times($tt, $dt) {
  if (exists($dt))
  then local:times(($tt, $tt[last()]+ $dt[1]), remove($dt,1))
  else $tt
};
  • 在模組的名稱空間中
module namespace time = 'http://www.cems.uwe.ac.uk/fold/time';
declare function time:times($tt, $dt) {
  if (exists($dt))
  then time:times(($tt, $tt[last()]+ $dt[1]), remove($dt,1))
  else $tt
};

宣告變數

[編輯 | 編輯原始碼]
declare variable $pi as xs:double := 3.14159265;

在具有名稱空間字首 fxx 的模組中

declare variable $fxx:file := doc('/db/me/file.xml');

嵌入 CSS 樣式表

[編輯 | 編輯原始碼]
  <style language="text/css">
  <![CDATA[
    .good {background-color: green;}
    .bad {background-color:red;}
  ]]>
  </style>

獲取 HTTP POST URL 引數

[編輯 | 編輯原始碼]

從 HTTP POST 中獲取兩個 URL 引數

   https://:8080/exist/rest/db/my-collection/my-xquery.xq?color=blue&shape=circle

新增以下內容

  let $my-color := request:get-parameter('color', 'red')
  let $my-shape := request:get-parameter('shape', '')

如果沒有提供顏色引數,將使用預設顏色“紅色”。

獲取 HTTP POST 資料

[編輯 | 編輯原始碼]

要獲取 HTTP POST 中的所有 XML 資料

  let $my-data := request:get-data()

擴充套件輸出大小限制

[編輯 | 編輯原始碼]

預設情況下,輸出大小為 10,000 位元組。您可以透過新增以下內容來擴充套件它

declare option exist:output-size-limit "new-size";

例如,要將輸出的大小限制提高三倍,請使用以下行

declare option exist:output-size-limit "30000";

華夏公益教科書