xquery version "3.0";
declare namespace schema="http://exist-db.org/apps/eXide/schema-parser";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
declare namespace json="http://www.json.org";
declare option exist:serialize "method=json media-type=application/json";
declare function schema:parse($schema as element(xs:schema)) {
for $elem in $schema/xs:element
return
schema:scan($elem)
};
declare function schema:scan($nodes as node()*) {
for $node in $nodes
return
typeswitch ($node)
case element(xs:element) return
if ($node/@ref) then
{$node/@ref/string()}
else
element { $node/@name}
{
if ($node/@type) then
schema:scan($node/ancestor::xs:schema/(xs:complexType|xs:simpleType)[@name = $node/@type])
else
schema:scan($node/node())
}
case element(xs:attribute) return
{
$node/@name/string()
}
case element(xs:complexType) return
schema:scan($node/node())
case element(xs:simpleType) return
schema:scan($node/node())
case element(xs:choice) return
{ schema:scan($node/node()) }
case element(xs:sequence) return
{ schema:scan($node/node()) }
case element() return
for $child in $node/* return schema:scan($child)
default return
()
};
let $schema := doc("/db/apps/eXide/resources/schema/schema-for-xslt20.xsd")/*
return
{schema:parse($schema)}