op:generate-view( $schemaName as xs:string, $viewName as xs:string, [$columnDeclarations as map:map*] ) as element()
This method generates a view that encapsulates a query. Insert the generated XML into the schemas database and then use the op.fromView op:from-view accessor to use the generated view.
xquery version "1.0-ml"; import module namespace op="http://marklogic.com/optic" at "/MarkLogic/optic.xqy"; op:from-view("main", "employees") => op:where(cts:collection-query("/division/manufacturing")) => op:select(("EmployeeID", "FirstName", "LastName")) => op:order-by("EmployeeID") => op:generate-view("main", "manufacturingEmployees")
xquery version "1.0-ml"; import module namespace op="http://marklogic.com/optic" at "/MarkLogic/optic.xqy"; let $plan1 := op:from-literals(( map:entry("rowId", 1) => map:with("colorIdShape", 1) => map:with("desc", "ball"), map:entry("rowId", 2) => map:with("colorIdShape", 2) => map:with("desc", "square"), map:entry("rowId", 3) => map:with("colorIdShape", 1) => map:with("desc", "box"), map:entry("rowId", 4) => map:with("colorIdShape", 1) => map:with("desc", "oval"), map:entry("rowId", 5) => map:with("colorIdShape", 5) => map:with("desc", "circle") )) let $plan2 := op:from-literals(( map:entry("colorId", 1) => map:with("colorDesc", "red"), map:entry("colorId", 2) => map:with("colorDesc", "blue"), map:entry("colorId", 3) => map:with("colorDesc", "black"), map:entry("colorId", 4) => map:with("colorDesc", "yellow") )) let $output := $plan1 => op:join-inner($plan2, op:on(op:col("colorIdShape"), op:col("colorId"))) => op:select(("rowId", op:as("description", op:col("desc")), "colorId", "colorDesc")) => op:order-by("rowId") return $output => op:generate-view('ThreeParams', 'ThreeColsSelected',( map:entry("name", "rowId") => map:with("type", "integer") => map:with("nullable", fn:true()) => map:with("invalidValues", 'reject'), map:entry("name", "description") => map:with("type", "string") => map:with("invalidValues", 'reject'), map:entry("name", "colorId") => map:with("type", "integer") => map:with("nullable", fn:false()) => map:with("invalidValues", 'skip') ))