THE SQL Server Blog Spot on the Web
Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help

Re: Xml Schema Conundrum

  •  06-25-2008, 13:11

    Re: Xml Schema Conundrum

    Thanks for the example and for attempting to answer my question, I've been posting it all over the place and have had minimal response, granted it's a small bit of business in Sql Server. My question might have been a bit vague, I'd hoped that by keeping it simple it might spark more conversation.

    So, to refine the issue using your example (thanks again for that); let's say that we've implemented the above example and have those structures in place. I am storing document fragments, what I want to do is have a column that will only store fragments conforming to the 'http://schemas.develop.com/outer/' namespace. Given the example above, with the type of restriction I'm seeking, only the 2nd / middle insert would succeed. The first and last would fail because they do not exclusively implement the  'http://schemas.develop.com/outer/' namespace.

    Does that help make my question more concise?

    Can that degree of control be achieved in any way using a typed xml declaration, does it require a constraint, or is there a best practices recommendation that covers this concept that I haven't found yet?

    At this point I'm fairly resigned to using a check constraint to provide the additional validation necessary, there certainly are worse options.

    While working with the example and testing a check constraint using a UDF I've noticed a limitation when using the exist() method, it only works on schemas that have a single root node. Last time I checked a schema could legally have multiple root nodes as the example above does in the inner and outer namespaces. However any usage of exist() with any of the schemas having multiple roots appears to fail, sometimes silently, in all contexts.

    When I tried to create a check constraint that would validate the outer schema by specifying .exist('/someString') the create function statement would fail with:
    XQuery [exist()]: There is no element named 'someString'

    Same thing for someDate or any other element from inner or outer, only /root works since its schema has only one root element.

    i did some more testing with the schema bound column and select statements and found the same is true outside of a function and remains true even if I remove the schema collection binding from the column (turn it into an untyped xml column). I'm guessing that's because the xml content still specifies the namespace and sql server is smart enough to associate the namespace in the schema colelction with the namespace declared in the element.

    When I insert some instances of <someString /> into the untyped column without the xmlns attribute .exist('/someString') miracuously returns the expected value (1).

    I don't recall seeing this behavior specifically documented anywhere, but I would certianly believe it if someone told me it's buried in the documentation somewhere.

    In my scenario I'm also writing the schema and have the option of providing namespaces with a single root element for validation, but I could see where this behavior could be frustrating if you had to discover it the hard way working with an immutable schema - you'd pretty much have to revert to string parsing or use the CLR to validate xml against a schema with multiple root elements.

    Thanks very much for addressing the issue, if you had not I'd probably have discovered the .exist() issue the had way.

    ~Steve

     

View Complete Thread
Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement