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-18-2008, 0:14

    Re: Xml Schema Conundrum

    >> Aside from this, if anyone has discovered any creative ways to resolve an xs:import against a schema stored in a different collection I'd love to hear about it.

    Not likely to happen. Schemas are compiled into types at cataloging so they can be statically analyzed by the query optimizer. It would be nice to say that a column is typed by n-many schemas.

    >> Is it possible to have a typed xml column/variable that is valid only if the content conforms to the 'ns://mydomain.com/importsFoo/' namespace?

    I may be (and probably am) misunderstanding your question, but this seems to work. Remember, typed XML instances are fragments unless they specifically marked as documents. I think the answer is "yes" provided you are treating the instances as fragments.

    use scratch;
    go
    drop table dbo.typedInstances;
    drop xml schema collection dbo.exampleSchema;
    go
    create xml schema collection dbo.exampleSchema as '<xs:schema xmlns:inner="http://schemas.develop.com/inner/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.develop.com/inner/">
      <xs:complexType name="T_someCollection">
        <xs:sequence>
          <xs:element ref="inner:collectionItem" maxOccurs="unbounded"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="T_collectionItem">
        <xs:sequence>
          <xs:element ref="inner:value"/>
          <xs:element ref="inner:text"/>
        </xs:sequence>
      </xs:complexType>
      <xs:element name="value" type="xs:byte"/>
      <xs:element name="text" type="xs:string"/>
      <xs:element name="someCollection" type="inner:T_someCollection"/>
      <xs:element name="collectionItem" type="inner:T_collectionItem"/>
    </xs:schema>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.develop.com/outer/">
      <xs:element name="someString" type="xs:string"/>
      <xs:element name="someDate" type="xs:dateTime"/>
    </xs:schema>
    <xs:schema xmlns:inner="http://schemas.develop.com/inner/" xmlns:outer="http://schemas.develop.com/outer/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:import namespace="http://schemas.develop.com/outer/"/>
      <xs:import namespace="http://schemas.develop.com/inner/"/>
      <xs:complexType name="T_root">
        <xs:sequence>
          <xs:element ref="outer:someString"/>
          <xs:element ref="outer:someDate"/>
          <xs:element ref="inner:someCollection"/>
        </xs:sequence>
      </xs:complexType>
      <xs:element name="root" type="T_root"/>
    </xs:schema>';
    go
    create table dbo.typedInstances(
      id tinyint not null identity(1,1) primary key,
      content xml(dbo.exampleSchema) not null);
    go
    insert into dbo.typedInstances values (
    '<?xml version="1.0" encoding="UTF-8"?>
    <root xmlns:outer="http://schemas.develop.com/outer/" xmlns:inner="http://schemas.develop.com/inner/">
      <outer:someString>Example</outer:someString>
      <outer:someDate>2008-06-17T22:46:00-05:00</outer:someDate>
      <inner:someCollection>
        <inner:collectionItem>
          <inner:value>1</inner:value>
          <inner:text>lawyers</inner:text>
        </inner:collectionItem>
        <inner:collectionItem>
          <inner:value>2</inner:value>
          <inner:text>guns</inner:text>
        </inner:collectionItem>
        <inner:collectionItem>
          <inner:value>3</inner:value>
          <inner:text>moeny</inner:text>
        </inner:collectionItem>       
      </inner:someCollection>
    </root>');
    go
    insert into dbo.typedInstances values (
    '  <someString xmlns="http://schemas.develop.com/outer/">Example</someString>
      <someDate xmlns="http://schemas.develop.com/outer/">2008-06-17T22:46:00-05:00</someDate>');
    go

    insert into dbo.typedInstances values (
    '  <someCollection xmlns="http://schemas.develop.com/inner/">
        <collectionItem>
          <value>1</value>
          <text>lawyers</text>
        </collectionItem>
        <collectionItem>
          <value>2</value>
          <text>guns</text>
        </collectionItem>
        <collectionItem>
          <value>3</value>
          <text>moeny</text>
        </collectionItem>       
      </someCollection>');
    go
    select content from dbo.typedInstances;
    go
     

    Is that what you had in mind?




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