Created
June 19, 2017 17:27
-
-
Save tvnpraveen/6306aee46ea33eac44ca0f5fae57eabb to your computer and use it in GitHub Desktop.
XSD to sample XML generator. Does not have attribute support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "1.0-ml"; | |
import module namespace functx = "http://www.functx.com" at "/MarkLogic/functx/functx-1.0-nodoc-2007-01.xqy"; | |
declare namespace xsd="http://www.w3.org/2001/XMLSchema"; | |
(: Provide the physical location where all the XSDs exist in $fileLoc :) | |
declare variable $fileLoc := "/reporting/xsd/”; | |
(: Provide the main XSD file :) | |
declare variable $mainXSDFile := "test.xsd"; | |
declare variable $elementMap := map:map(); | |
(: pretty print stylesheet :) | |
declare variable $pp-ss := | |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<xsl:output method="xml" indent="yes" encoding="UTF-8"/> | |
<xsl:template match="@*|element( )"> | |
<xsl:copy> | |
<xsl:apply-templates select="@*|element( )|text()[ string-length(normalize-space(string(.))) gt 0 ]"/> | |
</xsl:copy> | |
</xsl:template> | |
</xsl:stylesheet>; | |
declare function local:pretty-print($orig as element()) as element() { | |
xdmp:unquote( | |
fn:replace( | |
xdmp:quote( | |
xdmp:xslt-eval($pp-ss, $orig)/element() | |
), | |
"	", | |
"        " | |
) | |
)/element() | |
}; | |
declare function local:get-timezone() { | |
functx:timezone-from-duration(xs:dayTimeDuration(fn:implicit-timezone())) | |
}; | |
declare function local:getRandomString($name) { | |
if(fn:contains(fn:lower-case($name), "identifier") or | |
fn:contains(fn:lower-case($name), "number")) then | |
xs:string(xdmp:random(1000000)) | |
else if(fn:contains(fn:lower-case($name), "email")) then | |
"test@test.com" | |
else if(fn:contains(fn:lower-case($name), "name")) then | |
"test name" | |
else if(fn:contains(fn:lower-case($name), "referencetypecode")) then | |
xs:string(xdmp:random(9)) | |
else "random" | |
}; | |
declare function local:getRandomId() { | |
xdmp:random(100000) | |
}; | |
declare function local:getDate() { | |
fn:concat(fn:format-date(fn:current-date(),"[Y0001]-[M01]-[D01]"),"T00:00:00",local:get-timezone()) | |
}; | |
declare function local:getDateTime() { | |
fn:current-dateTime() | |
}; | |
declare function local:getFileNameFromLocation($schemaLocation) { | |
fn:tokenize($schemaLocation, "/")[fn:last()] | |
}; | |
declare function local:getBoolean($minoccurs) { | |
if($minoccurs eq "0") then | |
fn:false() | |
else | |
fn:true() | |
}; | |
declare function local:scanXsdFile($fileName){ | |
let $elementName := xdmp:unquote(xdmp:filesystem-file(fn:concat($fileLoc,$fileName)))/xsd:schema/xsd:element/@name/fn:string() | |
return | |
local:scanXsdFile($fileName, $elementName) | |
}; | |
declare function local:scanXsdFile($fileName, $elementName){ | |
let $schema := xdmp:unquote(xdmp:filesystem-file(fn:concat($fileLoc,$fileName)))/xsd:schema | |
let $ns := $schema/@targetNamespace/fn:string() | |
return | |
if($elementName) then | |
element {fn:QName($ns,$elementName)} { | |
local:parseXSD($schema, $ns) | |
} | |
else( | |
local:parseXSD($schema, $ns) | |
) | |
}; | |
declare function local:parseXSD($schema, $ns){ | |
let $extension := fn:tokenize($schema//xsd:extension/@base/fn:string(),":")[2] | |
return | |
if($extension) then | |
local:scanXsdFile(fn:concat($extension,".xsd"),()) | |
else() | |
, | |
for $element in $schema//xsd:sequence/xsd:element | |
let $name := $element/@name/fn:string() | |
let $type := fn:tokenize($element/@type/fn:string(),":")[2] | |
let $minoccurs := $element/@minOccurs/fn:string() | |
let $maxoccurs := $element/@maxOccurs/fn:string() | |
return | |
switch ($type) | |
case "string" | |
return element {fn:QName($ns,$name)} {local:getRandomString($name)} | |
case "integer" | |
return element {fn:QName($ns,$name)} {local:getRandomId()} | |
case "decimal" | |
return element {fn:QName($ns,$name)} {local:getRandomId()} | |
case "double" | |
return element {fn:QName($ns,$name)} {local:getRandomId()} | |
case "date" | |
return element {fn:QName($ns,$name)} {local:getDate()} | |
case "dateTime" | |
return element {fn:QName($ns,$name)} {local:getDateTime()} | |
case "boolean" | |
return element {fn:QName($ns,$name)} {local:getBoolean($minoccurs)} | |
default | |
return | |
if(fn:not(map:get($elementMap,fn:concat($type,$name)))) then | |
let $_ := map:put($elementMap, fn:concat($type,$name), $type) | |
return | |
local:scanXsdFile(fn:concat($type,".xsd"), $name) | |
else() | |
}; | |
(:##################### END OF FUNCTIONS #########################:) | |
local:pretty-print(local:scanXsdFile($mainXSDFile)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment