1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【MarkLogic】サンプルであそぶ(XQuery)

Last updated at Posted at 2019-04-18

※試用版を使っています。

(入門ガイド)Getting Started With MarkLogic Server — Chapter 6

サンプルを読み込む

MarkLogicと一緒にインストールされたサンプルで遊ぶ。
クエリが描ける画面を呼び出す。(マニュアル上は:8000だが、:8002が正解のようだ)

http://localhost:8002/
  • コンソールが出現
    image.png

  • サンプルをインポートする
    (marklogic-dir)/Samples/w3c-use-cases.xml
    image.png

  • サンプルを含む新たなWorkSpaceが誕生
    image.png

  • サンプルデータをロードするモジュールを実行(XQuery)
    image.png

  • Database:Documentsにロードされたようだが、この時点では管理画面を見ても変化はよくわからなかった。Size増えた?
    image.png

サンプルクエリでデータを参照する

1991年以降に発売された「Addison-Wesley」の書籍の発売年とタイトルを表示。
XQueryは初だが、シンプルなforループの処理なので解りやすい。

Q1.1_Value_Constraints.xq
(:  __________________________________________________________
 ::  (Q1.1)
 ::  List books published by Addison-Wesley after 1991
 ::  including their year and title.
 :: __________________________________________________________
  :)
<bib>
 {
  for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
  where $b/publisher = "Addison-Wesley" and $b/@year > 1991
  return
    <book year="{ $b/@year }">
     { $b/title }
    </book>
 }
</bib>
  • XML
    image.png

  • TEXT形式
    image.png

条件を変更して実行してみた

  • 1991以降 → 1993以降
  • priceの値を追加表示
Q1.1_Value_Constraints.xq
<bib>
 {
  for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
  where $b/publisher = "Addison-Wesley" and $b/@year > 1993
  return
    <book year="{ $b/@year }">
     { $b/title }
     { $b/price }
    </book>
 }
</bib>

変更が効いたようだ。
image.png

まとめ(やったこと)

  1. xdmpを利用して任意のURLを持つ文書をデータベースへ登録
  2. URLを利用してデータを検索

【参考】xdmp:document-insert

【参考】データソース

bstore1.example.com/bib.xml
xdmp:document-insert("http://bstore1.example.com/bib.xml",
<bib>
    <book year="1994">
        <title>TCP/IP Illustrated</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
 
    <book year="1992">
        <title>Advanced Programming in the Unix environment</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
 
    <book year="2000">
        <title>Data on the Web</title>
        <author><last>Abiteboul</last><first>Serge</first></author>
        <author><last>Buneman</last><first>Peter</first></author>
        <author><last>Suciu</last><first>Dan</first></author>
        <publisher>Morgan Kaufmann Publishers</publisher>
        <price>39.95</price>
    </book>
 
    <book year="1999">
        <title>The Economics of Technology and Content for Digital TV</title>
        <editor>
               <last>Gerbarg</last><first>Darcy</first>
                <affiliation>CITI</affiliation>
        </editor>
            <publisher>Kluwer Academic Publishers</publisher>
        <price>129.95</price>
    </book>
 
</bib>);
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?