How to share markup snippets amongst functions in eXist-db? - xquery

I wonder whether there is a way how to share html code snippets in eXist-db. I have two different (more expected later) functions returning the same big html form for different results. It is annoying to maintain the same code when I change something in one of these. I have tried:
Saving it like html file and load it with doc() function (eXist complains it is not an xml file, it is binary.
Saving it like global variable into a separate module (eXist complains there is a problem with contexts). I don’t know how to pass such a variable without the namespace prefix.
Saving it like a function returning its own huge variable (eXist complains there is a problem with contexts).
What is the best practice?
UPDATE
Well, I have tried to put the snippet into a variable insinde a function loaded as a module. For me, it seems reasonable. However, I got an error when try to return that:
err:XPDY0002 Undefined context sequence for 'child::snip:snippet' [at line 62, column 13, source: /db/apps/karolinum-apps/modules/app.xql]
In function:a pp:book-search(node(), map, xs:string?) [34:9:/db/apps/karolinum-apps/modules/app.xql]
I am calling it like so:
declare function app:list-books($node as node(), $model as map(*)) {
for $resource in collection('/db/apps/karolinum-apps/data/mono')
let $author := $resource//tei:titleStmt/tei:author/text()
let $bookName := $resource//tei:titleStmt/tei:title/text()
let $bookUri := base-uri($resource)
let $imgPath := replace($bookUri, '[^/]*?$', '')
let $fileUri := ( '/exist/rest' || $bookUri )
let $fileName := replace($bookUri, '.*?/', '')
return
if ($resource//tei:titleStmt/tei:title)
then
snip:snippet
else ()
};
Any ideas, please?
UPDATE II
Here I have the function in the module:
module namespace snip = "http://46.28.111.241:8081/exist/db/apps/karolinum-apps/modules/snip";
declare function snip:snippet($node as node(), $model as map(*), $author as xs:string, $bookTitle as xs:string, $bookUri as xs:anyURI, $fileUri as xs:anyURI) as element()* {
let $snippet :=
(
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{$bookTitle} ({$author})</h3>
</div>
<div class="panel-body">
...
</div>
)
return $snippet
};
Here I am trying to call it:
declare function app:list-books($node as node(), $model as map(*)) {
for $resource in collection('/db/apps/karolinum-apps/data/mono')
let $author := $resource//tei:titleStmt/tei:author/text()
let $bookTitle := $resource//tei:titleStmt/tei:title/text()
let $bookUri := base-uri($resource)
let $fileUri := ('/exist/rest' || $bookUri)
let $fileName := replace($bookUri, '.*?/', '')
where not(util:is-binary-doc($bookUri))
order by $bookTitle, $author
return
snip:snippet($author, $bookTitle, $bookUri, $fileUri)
};
It throws:
err:XPST0017 error found while loading module app: Error while loading module app.xql: Function snip:snippet() is not defined in namespace 'http://46.28.111.241:8081/exist/db/apps/karolinum-apps/modules/snip' [at line 35, column 9]
When I tried to put the snippet into a variable, it was not possible to pass there those local variables used (it threw $fileUri is not set). Besides that I tried to change the returned type element()* but nothing helped.

All of your approaches should work. Let me address each one:
Is the HTML snippet well-formed XML? If so, save it as, e.g., form.xml or form.html (since by default eXist assumes files with the .html extension are well-formed; see mime-types.xml in your eXist installation folder) and refer to it with doc($path). If it is not well-formed, you can save it as form.txt and pull it in with util:binary-to-string(util:binary-doc($path)). Or make the HTML well-formed and use the first alternative.
This too is valid, so you must not be properly declaring or referring to the global variable. What is the exact error you are getting? Can you post a small example snippet that we could run to reproduce your results?
See #2.

I was very close. It was necessary to somehow pass parameters to the nested function and omit eXist’s typical $node as node(), $model as map(*) as arguments.
Templating function:
declare function app:list-books($node as node(), $model as map(*)) {
for $resource in collection('/db/apps/karolinum-apps/data/mono')
let $author := $resource//tei:titleStmt/tei:author/text()
let $bookTitle := $resource//tei:titleStmt/tei:title/text()
let $bookUri := base-uri($resource)
let $bookId := xs:integer(util:random() * 10000)
let $fileUri := ('/exist/rest' || $bookUri)
let $fileName := replace($bookUri, '.*?/', '')
where not(util:is-binary-doc($bookUri))
order by $bookTitle, $author
return
snip:snippet($author, $bookTitle, $bookUri, $bookId, $fileUri)
};
Snippet function:
declare function snip:snippet($author as xs:string, $bookTitle as xs:string, $bookUri as xs:anyURI, $bookId as xs:string, $fileUri as xs:anyURI) as element()* {
let $snippet :=
(
<div class="panel panel-default">
...
</div>
)
return $snippet
};

Related

How to manipulate file-paths

I know this seems like a duplicate, and I am sure it more or less is ...
However, it really bugs me, and I cannot make anything of the posts before:
I am building a digital edition, utlizing TEI, XML, XSLT, (and probably existDB, maybe I switch to node/javascript).
I built a php-function that should transforme each file in a specified directory to html. (My xsl-file works well)
declare function app:XMLtoHTML-forAll ($node as node(), $model as map(*), $query as xs:string?){
let $ref := xs:string(request:get-parameter("document", ""))
let $xml := doc(concat("/db/apps/BookOfOrders/data/edition/",$ref))
let $xsl := doc("/db/apps/BookOfOrders/resources/xslt/xmlToHtml.xsl")
let $params :=
<parameters>
{for $p in request:get-parameter-names()
let $val := request:get-parameter($p,())
where not($p = ("document","directory","stylesheet"))
return
<param name="{$p}" value="{$val}"/>
}
</parameters>
return
transform:transform($xml, $xsl, $params)
};
There is a list of files in the apps/BookofOrders/data/edition/ named FolioX.html, where x is the page-number. (I'll probably change names to [FolioNumber].xml, but that's not the issue)
I am trying to make a text slider (so that when I open the page, a page is presented and further buttons are created, and I can slide to the right and read the rest of the pages).
I have a table of content, that is linked to the transformed files:
declare function app:toc($node as node(), $model as map(*)) {
for $doc in collection("/db/apps/BookOfOrders/data/edition")/tei:TEI
return
<li>{document-uri(root($doc))}</li>
};
I guess I am wondering on how to change the link inside to for example Folio29 to Folio30.
Can I take a part of the provided link and make the destination of a link flexible, similar but not identical to what I did in the toc-function above?
I'd be really happy if anyone could point me in the right direction.
Given an expression like document-uri(root($doc)) (perhaps more simply util:document-name($doc), since you're using eXist) that returns the path to (or filename of) the document ending in "FolioX", you just need to isolate X, then cast it as an integer so you can perform addition/subtraction on the value:
document-uri(root($doc)) => substring-after("Folio") => xs:integer()
util:document-name($doc) => substring-after("Folio") => xs:integer()
Then add 1, and you've got your next document. Subtract one, and you've got the previous
However, this could lead to broken links: Folio0 or Folio98 (assuming there are only 97). To avoid this, you might want to retrieve determine the complete list of Folios, find the current position, and then never hit 0 or 98:
let $this-folio := $doc => util:document-name()
let $collection := $doc => util:collection-name()
let $all-folios := xmldb:get-child-resources($collection)
(: sort the filenames using UCA Numeric collation to ensure Folio2 < Folio10.
: see https://www.w3.org/TR/xpath-functions-31/#uca-collations :)
let $sorted-folios := $all-folios => sort("?numeric=yes")
let $this-folio-n := index-of($all-folios, $this-folio)
let $prev-folio := if ($this-folio-n gt 1) then "Folio" || $this-folio-n - 1 else ()
let $next-folio := if ($this-folio-n lt count($all-folios)) then "Folio" || $this-folio-n + 1 else ()
return
<nav>
<prev>{$prev-folio}</prev>
<this>{"Folio" || $this-folio-n}</this>
<next>{$next-folio}</next>
</nav>

Handling undefined number of external variables

I'm using baseX in a REST environment and I'm quite stuck trying to run an .xq script with an undefined number of GET variables (could be 1 but could be 10)
I'd like to make my xq script generic about that and construct my query independently.
Is there a way to achieve that, playing with array or sending differently my variables, or I dunno how ?
here is my API call
http://basex:8984/rest/?run=WEB-INF/data/test.xq&$tag=p&value=sciences&tag2=p&value2=test&tag3=testdzq
here is my text.xq
declare variable $tag external;
declare variable $value external;
declare variable $tag2 external;
declare variable $value2 external;
<documents>
{for $doc in collection("testdb2")
where $doc//*[name() eq $tag]/text()[matches(., $value )]
and $doc//*[name() eq $tag2]/text()[matches(., $value2 )]
return <doc>{$doc//titleStmt/title/text()}</doc>
}
</documents>
Thanks !
found this here (see http-params function) https://www.balisage.net/Proceedings/vol18/print/Murray01/BalisageVol18-Murray01.html
(: BaseX example :)
(: In the controller ... :)
module namespace c = "http://balisage.net/ns/Bal2016murr0319/controller";
import module namespace request = "http://exquery.org/ns/request";
import module namespace item = "http://balisage.net/ns/Bal2016murr0319/views/item" at "views/item.xqm";
(:~ Returns a map containing the HTTP request parameters. :)
declare function c:http-params()
as map(*)
{
map:merge(
for $name in request:parameter-names()
let $value := request:parameter($name)
return map:entry($name, $value)
)
};
(:~ Calls the appropriate view, based on user input. :)
declare function c:get-view()
as element(html)
{
(: get HTTP request parameters :)
let $params := c:http-params()
return
if (map:get($params, "id")) then
(: the presence of "id" indicates that the user is requesting the item-level page for this unique identifier :)
(: call the item-level view :)
item:get-html($params)
else if ... (: call some other view :)
else if ... (: call some other view :)
else (: call the view for the home page ... :)
};

How to update nodes in a different Database or how to update external nodes ?- XDMP-UPEXTNODES

I am trying to update a document in a different Database then my current DB. But it is giving me the below error-
XDMP-UPEXTNODES: xdmp:node-replace(fn:doc("/C:/Users/Downloads/abc.csv-0-2")/*:envelope/*:root/*:Status, <Status>1000</Status>) -- Cannot update external nodes
I am using the below code-
let $temp :=
for $i in $result
let $error := $i/*:envelope/*:ErrorMessage
let $status := $i/*:envelope/*:Status
return
if(fn:exists($i) eq fn:true()) then (
xdmp:invoke-function(
function() {
xdmp:node-replace($status,<Status>1000</Status>),
xdmp:node-replace($error,<ErrorMessage>Change Error in other Database-2</ErrorMessage>)
},
<options xmlns="xdmp:eval">
<database>{xdmp:database("DATABASE-2")}</database>
</options>))
else ()
I want to update the Error and Status node of my Database-2.
$result is the document i fetched from Database-2.
This code i am running from Database-1
Any Suggestions ?
You cannot pass database nodes as variable for updating purposes like that. Instead you should pass through the database uri, and get a fresh copy of the element you'd like to update inside the invoked function. Maybe you can push a bit more logic inside the invoked function to make that easier. Something like:
for $i in $result
let $uri := xdmp:node-uri($i)
return xdmp:invoke-function(function() {
let $doc := fn:doc($uri)
let $error := $doc/*:envelope/*:ErrorMessage
let $status := $doc/*:envelope/*:Status
return if(fn:exists($doc) eq fn:true()) then (
xdmp:node-replace($status, <Status>1000</Status>),
xdmp:node-replace($error, <ErrorMessage>Change Error in other Database-2</ErrorMessage>)
) else ()
}, map:entry("database", xdmp:database("DATABASE-2")))
Be careful though. It sounds like $i is pointing to the actual document in Database-2 as well, and it could easily result in dead-locks; the invoking query could be putting a read lock on $i, causing the invoked function to be unable to update it.
HTH!

split document by using MarkLogic Flow Editor

i try to split my incoming documents using "Information Studio Flows" (MarkLogic v 8.0-1.1). The problem is in "Transform" section.
This is my importing documents. For simplicity i reduce it content to one stwtext-element
<docs>
<stwtext id="RD-10-00258" update="03.2011" seq="RQ-10-00001">
<head>
<ti>
<i>j</i>
</ti>
<ff-list>
<ff id="0103"/>
</ff-list>
</head><p>
Symbol für die
<vw idref="RD-19-04447">Stromdichte</vw>
.
</p>
</stwtext>
</docs>
This is my "xquery transform" content:
xquery version "1.0-ml";
(: Copyright 2002-2015 MarkLogic Corporation. All Rights Reserved. :)
(:
:: Custom action. It must be a CPF action module.
:: Replace this text completely, or use it as a template and
:: add imports, declarations,
:: and code between START and END comment tags.
:: Uses the external variables:
:: $cpf:document-uri: The document being processed
:: $cpf:transition: The transition being executed
:)
import module namespace cpf = "http://marklogic.com/cpf"
at "/MarkLogic/cpf/cpf.xqy";
(: START custom imports and declarations; imports must be in Modules/ on filesystem :)
(: END custom imports and declarations :)
declare option xdmp:mapping "false";
declare variable $cpf:document-uri as xs:string external;
declare variable $cpf:transition as node() external;
if ( cpf:check-transition($cpf:document-uri,$cpf:transition))
then
try {
(: START your custom XQuery here :)
let $doc := fn:doc($cpf:document-uri)
return
xdmp:eval(
for $wpt in fn:doc($doc)//stwtext
return
xdmp:document-insert(
fn:concat("/rom-data/", fn:concat($wpt/#id,".xml")),
$wpt
)
)
(: END your custom XQuery here :)
,
cpf:success( $cpf:document-uri, $cpf:transition, () )
}
catch ($e) {
cpf:failure( $cpf:document-uri, $cpf:transition, $e, () )
}
else ()
by running of snippet, i take the error:
Invalid URI format
and long description of it:
XDMP-URI: (err:FODC0005) fn:doc(fn:doc("/8122584828241226495/12835482492021535301/URI=/content/home/admin/Vorlagen/testing/v10.new-ML.xml")) -- Invalid URI format: "
j
Symbol für die
Stromdichte
"
In /18200382103958065126.xqy on line 37
In xdmp:invoke("/18200382103958065126.xqy", (xs:QName("trgr:uri"), "/8122584828241226495/12835482492021535301/URI=/content/home/admi...", xs:QName("trgr:trigger"), ...), <options xmlns="xdmp:eval"><isolation>different-transaction</isolation><prevent-deadlocks>t...</options>)
$doc = fn:doc("/8122584828241226495/12835482492021535301/URI=/content/home/admin/Vorlagen/testing/v10.new-ML.xml")
In /MarkLogic/cpf/triggers/internal-cpf.xqy on line 179
In execute-action("on-state-enter", "http://marklogic.com/states/initial", "/8122584828241226495/12835482492021535301/URI=/content/home/admi...", (xs:QName("trgr:uri"), "/8122584828241226495/12835482492021535301/URI=/content/home/admi...", xs:QName("trgr:trigger"), ...), <options xmlns="xdmp:eval"><isolation>different-transaction</isolation><prevent-deadlocks>t...</options>, (fn:doc("http://marklogic.com/cpf/pipelines/14379829270688061297.xml")/p:pipeline, fn:doc("http://marklogic.com/cpf/pipelines/15861601524191348323.xml")/p:pipeline), fn:doc("http://marklogic.com/cpf/pipelines/15861601524191348323.xml")/p:pipeline/p:state-transition[1]/p:default-action, fn:doc("http://marklogic.com/cpf/pipelines/15861601524191348323.xml")/p:pipeline/p:state-transition[1])
$caller = "on-state-enter"
$state-or-status = "http://marklogic.com/states/initial"
$uri = "/8122584828241226495/12835482492021535301/URI=/content/home/admi..."
$vars = (xs:QName("trgr:uri"), "/8122584828241226495/12835482492021535301/URI=/content/home/admi...", xs:QName("trgr:trigger"), ...)
$invoke-options = <options xmlns="xdmp:eval"><isolation>different-transaction</isolation><prevent-deadlocks>t...</options>
$pipelines = (fn:doc("http://marklogic.com/cpf/pipelines/14379829270688061297.xml")/p:pipeline, fn:doc("http://marklogic.com/cpf/pipelines/15861601524191348323.xml")/p:pipeline)
$action-to-execute = fn:doc("http://marklogic.com/cpf/pipelines/15861601524191348323.xml")/p:pipeline/p:state-transition[1]/p:default-action
$chosen-transition = fn:doc("http://marklogic.com/cpf/pipelines/15861601524191348323.xml")/p:pipeline/p:state-transition[1]
$raw-module-name = "/18200382103958065126.xqy"
$module-kind = "xquery"
$module-name = "/18200382103958065126.xqy"
In /MarkLogic/cpf/triggers/internal-cpf.xqy on line 320
i thought, it was a problem with "Document setting" in "load" section of "Flow editor"
URI=/content{$path}/{$filename}{$dot-ext}
but if i remove it, i recive the same error.
i have no idea what to do. i am really new. please help
First of all, Information Studio has been deprecated in MarkLogic 8. I would also recommend very much looking in to the aggregate_record feature of MarkLogic Content Pump:
http://docs.marklogic.com/guide/ingestion/content-pump#id_65814
Apart from that, there are several issues with your code. You are calling fn:doc twice, effectively trying to interpret the doc contents as a uri. There is an unnecessary xdmp:eval wrapping the FLWOR statement, which expects a string as first param. I think you can shorten it to (showing inner part of the action only):
(: START your custom XQuery here :)
let $doc := fn:doc($cpf:document-uri)
for $wpt in $doc//stwtext
return
xdmp:document-insert(
fn:concat("/roempp-data/", fn:concat($wpt/#id,".xml")),
$wpt
)
(: END your custom XQuery here :)
HTH!
very many thanks #grtjn and this is my approach. Practically it is the same solution
(: START your custom XQuery here :)
xdmp:log(fn:doc($cpf:document-uri), "debug"),
let $doc := fn:doc($cpf:document-uri)
return
xdmp:eval('
declare variable $doc external;
for $wpt in $doc//stwtext
return (
xdmp:document-insert(
fn:concat("/roempp-data/", fn:concat($wpt/#id,".xml")),
$wpt,
xdmp:default-permissions(),
"roempp-data"
)
)'
,
(xs:QName("doc"), $doc),
<options xmlns="xdmp:eval">
<database>{xdmp:database("roempp-tutorial")}</database>
</options>
)
(: END your custom XQuery here :)
Ok, now it works. It is fine, but i found, that after the loading is over, i see in MarkLogic two documents:
my splited document "/rom-data/RD-10-00258.xml" with one root element "stwtext" (as desired)
origin document "URI=/content/home/admin/Vorlagen/testing/v10.new-ML.xml" with root element "docs"
is it possible to prohibit insert of origin document ?

Encode string as html in eXist-db / XQuery

I'm trying to generate a treeview from a collection (filesystem). Unfortunately some Files have special characters like ü ä and ö. And I'd like to have them html encoded as &­auml;
When I get them from the variable, they are URL encoded. First I decode them to UTF-8 and then .... i don't know how to go further.
<li>{util:unescape-uri($child, "UTF-8")}
The function util:parse is doing the exact opposite from that what I want.
Here is the recursive function:
xquery version "3.0";
declare namespace ls="ls";
declare option exist:serialize "method=html media-type=text/html omit-xml-declaration=yes indent=yes";
declare function ls:ls($collection as xs:string, $subPath as xs:string) as element()* {
if (xmldb:collection-available($collection)) then
(
for $child in xmldb:get-child-collections($collection)
let $path := concat($collection, '/', $child)
let $sPath := concat($subPath, '/', $child)
order by $child
return
<li>{util:unescape-uri($child, "UTF-8")}
<ul>
{ls:ls($path,$sPath)}
</ul>
</li>,
for $child in xmldb:get-child-resources($collection)
let $sPath := concat($subPath, '/', $child)
order by $child
return
<li> {util:unescape-uri($child, "UTF-8")}</li>
)
else ()
};
let $collection := request:get-parameter('coll', '/db/apps/ebner-online/resources/xss/xml')
return
<ul>{ls:ls($collection,"")}</ul>
Rather than util:unescape-uri(), I would suggest using xmldb:encode-uri() and xmldb:decode-uri(). Use the encode version on a collection or document name when creating/storing it. Use the decode version when displaying the collection or document name. See the function documentation for the xmldb module.
As to forcing ä instead of ü, this is an even trickier serialization issue. Both, along with ä, are equivalent representations of the same UTF-8 character. Why not just let the character through as ü?

Resources