Skip to content

Instantly share code, notes, and snippets.

@ableasdale
Last active August 29, 2015 14:16
Show Gist options
  • Save ableasdale/fb8dfbc44d8bfd0e5247 to your computer and use it in GitHub Desktop.
Save ableasdale/fb8dfbc44d8bfd0e5247 to your computer and use it in GitHub Desktop.
Basic XSD Viewer
xquery version "1.0-ml";
declare variable $filename := xdmp:get-request-field("file");
declare variable $dir-base as xs:string := "C:\Program Files\MarkLogic\Config\";
declare function local:bootstrap-shim($title, $content){
xdmp:set-response-content-type("text/html; charset=utf-8"),
("<!DOCTYPE html>",
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{$title}</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">{" "}</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js">{" "}</script>
</head>
<body role="document">
{$content}
</body>
</html>)};
declare function local:process($x) {
(element h2 {fn:data($x/@name)},
element p {$x//xs:documentation/text()},
element pre { element code { xdmp:quote($x) } }
)};
declare function local:schema-file($file as xs:string) {
element div {
attribute class {"container"},
element div {
attribute class {"well"}, "Viewing contents of: ", element strong {$file}
},
for $x in xdmp:document-get(concat($dir-base, $file))/node()/node()
return if (fn:exists($x/@name))
then (local:process($x))
else ()
},
element footer { attribute class {"footer"},
element div { attribute class {"container"},
element p { element a {attribute href {"/"}, "Back to listing >"}}
}
}
};
declare function local:get-directory() {
element div {
attribute class {"container"},
element div { attribute class {"page-header"},
element h2 {"Directory listing for: "},
element h3 {$dir-base},
element table { attribute class {"table table-striped table-bordered"},
element thead { element tr {
for $x in ("Filename", "Pathname", "Type", "Content Length", "Last Modified")
return element th {$x}
}
},
element tbody {
for $d in xdmp:filesystem-directory($dir-base)/dir:entry
return element tr {
element td {element a { attribute href {concat("?file=", $d/dir:filename/text())}, $d/dir:filename/text()}},
element td {$d/dir:pathname/text()},
element td {$d/dir:type/text()},
element td {element span { attribute class {"badge"}, $d/dir:content-length/text()}},
element td {$d/dir:last-modified/text()}
}
}
}}
}
};
(: main :)
if (not(empty($filename)))
then (local:bootstrap-shim(concat("File Listing: ",$filename), local:schema-file($filename)))
else (local:bootstrap-shim("Directory Listing", local:get-directory()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment