Skip to content

Instantly share code, notes, and snippets.

@timathom
Last active September 7, 2016 06:34
Show Gist options
  • Save timathom/486b1fcf05d2c69ec176 to your computer and use it in GitHub Desktop.
Save timathom/486b1fcf05d2c69ec176 to your computer and use it in GitHub Desktop.
XForms doc and RESTXQ module for retrieving Turtle RDF data
<?xml-stylesheet href="xsltforms/xsltforms.xsl" type="text/xsl"?><?xsltforms-options debug="no"?><?css-conversion no?><?xml-model href="http://www.oxygenxml.com/1999/xhtml/xhtml-xforms.nvdl" schematypens="http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0"?>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:cwb="http://example.org/cwb/"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<head>
<meta
content="IE=edge"
http-equiv="X-UA-Compatible" />
<title>Cataloger's Workbench Editor</title>
<xf:model
id="rdf-model">
<xf:instance
id="sparql-query">
<cwb:data>
<query xmlns="">
<![CDATA[
DESCRIBE <http://dbpedia.org/resource/Princeton_University_Library> WHERE {?s ?p ?o} LIMIT 1
]]>
</query>
</cwb:data>
</xf:instance>
<xf:instance
id="result">
<cwb:data>
<cwb:result>
<cwb:replace/>
</cwb:result>
</cwb:data>
</xf:instance>
<!-- Submit query to RESTXQ -->
<xf:submission
cdata-section-elements="cwb:replace"
encoding="UTF-8"
id="rq"
instance="result"
method="get"
ref="instance('sparql-query')/*"
replace="text"
resource="/turtle"
serialize="none"
targetref="cwb:result/cwb:replace">
<xf:header
combine="replace">
<xf:name>Accept</xf:name>
<xf:value>text/turtle; q=1.0, application/ld+json; q=0.5</xf:value>
</xf:header>
</xf:submission>
</xf:model>
</head>
<body
prefix="rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns# bf: http://bibframe.org/vocab/">
<div
class="container">
<div
class="header">
<h1
about="http://example.org/cwb"
property="bf:title">Cataloger's Workbench</h1>
<xf:trigger
id="update-resources">
<xf:label>Query</xf:label>
<xf:action
ev:event="DOMActivate">
<xf:send
submission="rq"></xf:send>
</xf:action>
</xf:trigger>
</div>
<xf:group
id="query-output">
<pre>
<xf:output value="instance('result')/cwb:result/cwb:replace"></xf:output>
</pre>
</xf:group>
</div>
</body>
</html>
xquery version "3.1";
(:~
: Module for querying SPARQL endpoints and generating XForms documents
: @author tat2
:)
module namespace cwb-search = "http://example.org/cwb/search";
declare copy-namespaces no-preserve, no-inherit;
declare default element namespace "http://www.w3.org/1999/xhtml";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare namespace cwb = "http://example.org/cwb/";
declare namespace xf = "http://www.w3.org/2002/xforms";
declare namespace ev = "http://www.w3.org/2001/xml-events";
declare namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace rdfs = "http://www.w3.org/2000/01/rdf-schema#";
declare namespace bf = "http://bibframe.org/vocab/";
declare variable $cwb-search:local-path := "/Users/timathom/basex/webapp";
(:~
: This function returns an XForms document containing the results
: of a SPARQL query submitted from another XForms doc
: @param $query querystring from XForms GET submission
: @return response Turtle text; XHTML XForms document written to filesystem
:)
declare %rest:path("/turtle") %rest:GET %rest:query-param("query", "{$query}") %rest:produces("text/turtle") function cwb-search:get-turtle($query as xs:string) {
let $request := <http:request
href="http://dbpedia.org/sparql?query={encode-for-uri($query)}"
method="GET"
override-media-type="text/plain">
<http:header
name="Accept"
value="text/turtle"/>
</http:request>
let $response := http:send-request($request)
let $head := $response[1]
let $body := $response[2]
(: Return search result and output in XForms doc :)
return
if ($head/@status cast as xs:integer lt 400) then
let $test := map {"cdata-section-elements": "Q{http://www.w3.org/1999/xhtml}script"}
let $xslt-pi := processing-instruction xml-stylesheet {'type="text/xsl" href="xsltforms/xsltforms.xsl"'}
let $css := processing-instruction css-conversion {'no'}
let $form := <html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:bf="http://bibframe.org/vocab/"
xmlns:cwb="http://example.org/cwb/"
xmlns:cwb-search="http://example.org/cwb/search"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<head/>
<body>
<script
type="text/turtle">{concat('#<![CDATA[', $body, '#]]>')}</script>
</body>
</html>
let $params := <output:serialization-parameters
xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization">
<output:method
value="html"/>
</output:serialization-parameters>
return
($body,
file:write($cwb-search:local-path || "/static/turtle-test.xhtml", document {$form}, $params))
else
<cwb:error>No results</cwb:error>
};
<!-- Result doc written by BaseX file:write function -->
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:bf="http://bibframe.org/vocab/"
xmlns:cwb="http://example.org/cwb/"
xmlns:cwb-search="http://example.org/cwb/search"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<head>
<meta
content="IE=edge"
http-equiv="X-UA-Compatible" />
<title>Cataloger's Workbench Editor</title>
<xf:model></xf:model>
</head>
<body>
<script type="text/turtle">
@prefix ex: &lt;http://example.org/&gt; .
@prefix test: &lt;http://test.org/&gt; .
ex:test1 a test:Test .
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment