Hi Tim,

> I need to validate against the schema form of the DTD, i.e.
> Archive-1.0.xsd.  I could replace the DTD reference with an
> XML schema reference in the XML file prior to loading it into
> ML, but I prefer to perform all content manipulation in MarkLogic.

To summarize, no need to do this. Just leave the DTD declaration, and it is not 
necessary to add a reference a schema either. Adding schema location attributes 
to your content would make things less flexible..

> Does this sound feasible?  Can anyone provide a code snippet
> of performing validation as part of a precommit trigger?

You need to take several steps to make this work:

1) Add your schema to the database that is configured as the schemas database 
of your documents database. There are plenty ways to do this..

2) Create and insert a module in some (modules) database that will do the 
actual validation. It could be something like this:

        xquery version "1.0-ml";

        declare namespace trgr="http://marklogic.com/xdmp/triggers";;

        declare variable $trgr:uri as xs:string external;
        declare variable $trgr:trigger as node() external;

        validate strict {
                doc($trgr:uri)
        },
        "ok"

3) Create pre-commit triggers that calls the above module. You can use 
something like the following, but make sure to execute this against the 
database that is configured as the triggers database of your documents database 
(I wrapped the code below in a xdmp:eval, to make it execute against the 
triggers database; hence the external variables):

                        xquery version "1.0-ml";
                        import module namespace 
trgr="http://marklogic.com/xdmp/triggers"; at "/MarkLogic/triggers.xqy";
                        declare variable $trigger-create-name as xs:string 
external;
                        declare variable $trigger-create-description as 
xs:string external;
                        declare variable $trigger-modify-name as xs:string 
external;
                        declare variable $trigger-modify-description as 
xs:string external;
                        declare variable $trigger-module-uri as xs:string 
external;
                        try {
                                (: trigger for new documents insert :)
                                let $create-trigger :=
                                        trgr:create-trigger(
                                                $trigger-create-name,
                                                $trigger-create-description,
                                                trgr:trigger-data-event(
                                                       
trgr:directory-scope("/", "infinity"),
                                                       
trgr:document-content("create"),
                                                       trgr:pre-commit()
                                                ),
                                                trgr:trigger-module(
                                                        
xdmp:database("Modules"),
                                                        "/",
                                                        $trigger-module-uri),
                                                (:enable immediately:) true(),
                                                xdmp:default-permissions()
                                        )
                                (: trigger for documents updates :)
                                let $modify-trigger :=
                                        trgr:create-trigger(
                                                $trigger-modify-name,
                                                $trigger-modify-description,
                                                trgr:trigger-data-event(
                                                       
trgr:directory-scope("/", "infinity"),
                                                       
trgr:document-content("modify"),
                                                       trgr:pre-commit()
                                                ),
                                                trgr:trigger-module(
                                                        
xdmp:database("Modules"),
                                                        "/",
                                                        $trigger-module-uri),
                                                (:enable immediately:) true(),
                                                xdmp:default-permissions()
                                        )
                                return
                                        "Triggers succesfully installed.."
                        } catch ($exception) {
                                ()
                        }

4) Add schema mappings to your application server. Use the admin interface, go 
the configuration of your app server, and open the schemas page. Add a schema 
mapping in which you leave the namespace uri blank, and add the uri to the 
schema you added in step 1..

You can skip step 4 if you add import schema lines in the validating module, 
but this is more flexible. Add the following lines if you don't need the 
flexibility:

        declare default element namespace "";
        import schema "" at "/my-schema.xsd";


You can do document inserts from anywhere now, these will always be validated. 
Note that invalid documents will throw exceptions that cannot be caught as 
usual. A failing pre-commit trigger is supposed to make the whole transaction 
fail. You can put xdmp:eval around xdmp:document-insert statements, that will 
allow capturing these exceptions in a try catch, but use this with care..

Kind regards,
Geert



Drs. G.P.H. Josten
Consultant


http://www.daidalos.nl/
Daidalos BV
Source of Innovation
Hoekeindsehof 1-4
2665 JZ Bleiswijk
Tel.: +31 (0) 10 850 1200
Fax: +31 (0) 10 850 1199
http://www.daidalos.nl/
KvK 27164984
De informatie - verzonden in of met dit emailbericht - is afkomstig van 
Daidalos BV en is uitsluitend bestemd voor de geadresseerde. Indien u dit 
bericht onbedoeld hebt ontvangen, verzoeken wij u het te verwijderen. Aan dit 
bericht kunnen geen rechten worden ontleend.



_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

Reply via email to