MarkLogic 初学

2022-04-08 00:00:00 专区 订阅 付费 交易 评论

MarkLogic -> Groups ->Default -> App Servers -> App Server分为三类:

  1. Http
  2. WebDAV
  3. XDBC

其中:

Root 就是数据库根目录

Module是放的xquery,而xquery其实就是view

Database才是放的数据,即xml文件


//insert a xml into db

load.xqy

xquery version "1.0-ml";
(: load.xqy :)
xdmp:document-insert("books.xml",
<books xmlns="http://www.marklogic.com/ns/gs-books">
<book bookid="1">
<title>A Quick Path to an Application</title>
<author>
<last>Smith</last>
<first>Jim</first>
</author>
<publisher>Scribblers Press</publisher>
<isbn>1494-3930392-3</isbn>
<abstract>This book describes in detail the power of how to use 
XQuery to build powerful web applications that are built on the 
MarkLogic Server platform.</abstract>
</book>
</books>
),
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Database loaded</title>
</head>
<body>
<b>Source XML Loaded</b>
<p>The source XML has been successfully loaded into the database</p>
</body>
</html>


// get xml from db
dump.xqy

xquery version "1.0-ml";
(: dump.xqy :)
declare namespace bk = "http://www.marklogic.com/ns/gs-books";
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Database dump</title>
</head>
<body>
<b>XML Content</b>
{
for $book in doc("books.xml")/bk:books/bk:book
return
<pre>
Title: { $book/bk:title/text() }
Author: { ($book/bk:author/bk:first/text(), " ",
$book/bk:author/bk:last/text()) }
Publisher: { $book/bk:publisher/text() }
</pre>
}
<a href="update-form.xqy">Update Publisher</a>
</body>
</html>


update-form.xqy

xquery version "1.0-ml";
(: update-form.xqy :)
declare namespace bk="http://www.marklogic.com/ns/gs-books";
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Change Publisher</title>
</head>
<body>
{
let $book := doc("books.xml")/bk:books/bk:book[1]
return
<form action="update-write.xqy">
<input type="hidden" name="bookid" value="{ $book/@bookid }"/>
<p><b>
Change publisher for book <i>{ $book/bk:title/text() }</i>:
</b></p>
<input type="text" name="publisher"
value="{ $book/bk:publisher/text() }"/>
<input type="submit" value="Update publisher"/>
</form>
}
</body>
</html>


update-write.xqy

xquery version "1.0-ml";
(: update-write.xqy :)
declare namespace bk="http://www.marklogic.com/ns/gs-books";
declare function local:updatePublisher()
{
if (doc("books.xml")) then
let $bookid := xdmp:get-request-field("bookid")
let $publisher := xdmp:get-request-field("publisher")
let $b := doc("books.xml")/bk:books/bk:book[@bookid = $bookid]
return
if ($b) then
(
xdmp:node-replace($b/bk:publisher,
<bk:publisher>{ $publisher }</bk:publisher>)
,
xdmp:redirect-response("dump.xqy")
)
else
<span>Could not locate book with bookid { $bookid }.</span>
else
<span>Unable to access parent XML document.</span>
};
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Update In Process</title>
</head>
<body>
Attempting to complete update and redirect browser to detail page.
<p>
If you are seeing this page, either the redirect has failed
or the update has failed. The update has failed if there is
a reason provided below:
<br/>
{ local:updatePublisher() }
</p>
</body>
</html>

相关文章