[MarkLogic Dev General] Xml schema validation problem - reg.,

Mary Holstege mary.holstege at marklogic.com
Fri May 22 08:27:07 PDT 2009


On Fri, 22 May 2009 01:09:44 -0700, Santhosh Raj <santhosh.raj at tcs.com>  
wrote:

> Hi all,
>
>         I have created a library.xsd file and a library.xml file which
> will use that library.xsd file.
>
> I have loaded the library.xsd file into my schema database.
>
> Then I loaded library.xml file into my Documents database  In  Marklogic
> server.
>
> When I fire the following query:
>
> declare namespace bk = "http://www.harmony.com";
> doc("library.xml")//bk:book[1]/descendant::element(*,xs:string)
>
> It gives the correct answer.  i.e It returns the descendant element of
> first book whose element type is xs:string.
>
> This implies that the xml is refering to that xsd.So it is populating the
> the element matching the datatype xs:string which is specified in the  
> xsd.

What happens is this: when you perform a type-aware operation (fn:data,
type test, etc.) we attempt to determine what the type of the node is.
You can think of this as the type-assignment part of schema assessment,
without the validation part.  Note that this is done on-demand: we do
not persist type information in the database. As such, your query above
is a relatively expensive operation: you fetch every descendant node of
every book, figure out the type for that node, and then filter out the
ones that match.

> But when i try to update the xml using xdmp:node-insert-child() it is not
> validating the content with the xsd. Even though if i add an extra  
> element which is not present in the xsd it is adding to the xml.  What  
> is the
> problem? Is there any option to specify the xml to validate against xsd.
>
> When i try it , it is still adding to the xml. But actually it should  
> not.

It is doing what it is supposed to: schema-validity is not required of
XML stored in the database.  You don't even have to have a schema,
and being able to store invalid XML allows you to use XQuery to
clean up the bad XML.

> declare namespace bk = "http://www.harmony.com";
> for $b in doc("library.xml")//bk:books
> return
> xdmp:node-insert-child($b,<bk:book>
>     <bk:title>Arts</bk:title>
>     <bk:price>200</bk:price>
>     <bk:author>Santhosh </bk:author>
>   </bk:book>)
>
> In this example    <bk:author>Santhosh </bk:author>  Element is not
> present in the xsd, but still it is adding to the xml.
>
> What should be done to achieve this.(validating the xml against the xsd)

You need to use the XQuery validate {} expression.
For the query you have above, it would look something like:

declare namespace bk = "http://www.harmony.com";
for $b in doc("library.xml")//bk:books
return
  xdmp:node-insert-child($b, validate strict { <bk:book>
      <bk:title>Arts</bk:title>
      <bk:price>200</bk:price>
      <bk:author>Santhosh </bk:author>
    </bk:book> } )

In the current version of ML (4.0) it does not yet do full checking,  
however,
although it will in 4.1.  In particular, it does not check the complex type
constraints (e.g. which children are required, etc.)

//Mary

Mary.Holstege at marklogic.com
Principal Engineer
Mark Logic Corporation


More information about the General mailing list