USE xmldb
GO

-- this returns a single XML schema document
-- for the 'http://example.org/LogansRun' namespace 
-- if it occurs in the XML schema collection 'dbo.peoplecoll'
SELECT xml_schema_namespace(
   N'dbo', N'peoplecoll', N'http://example.org/LogansRun')
GO

-- this returns all of the XML schema documents
-- in the peoplecoll XML schema collection
-- multiple schema documents are separated by a space
SELECT xml_schema_namespace(N'dbo', N'peoplecoll')
GO


CREATE TABLE xml_schema_save_tab (
  the_id INTEGER PRIMARY KEY, 
  xml_schema_col XML)
GO

INSERT INTO xml_schema_save_tab VALUES(1,
N'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://example.org/LogansRun"
            targetNamespace="http://example.org/LogansRun" >
   <!-- this schema defines a single data type, personAge,
        and I want to save this comment as well as the schema -->
   <xsd:simpleType name="personAge" >
     <xsd:restriction base="xsd:float" >
       <xsd:maxInclusive value="30" />
       <xsd:minExclusive value="0" />
     </xsd:restriction>
   </xsd:simpleType>
   <xsd:element name="age" type="tns:personAge" />
</xsd:schema>')
GO


-- add schema to collection

CREATE XML SCHEMA COLLECTION mytrees AS
'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://example.org/Trees"
            targetNamespace="http://example.org/Trees" >
   <xsd:simpleType name="treeAge" >
     <xsd:restriction base="xsd:float" >
       <xsd:maxInclusive value="1000" />
       <xsd:minExclusive value="0" />
     </xsd:restriction>
   </xsd:simpleType>
   <xsd:element name="treeage" type="tns:treeAge" />
</xsd:schema>' 
GO

ALTER XML SCHEMA COLLECTION mytrees ADD
'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://example.org/Trees2"
            targetNamespace="http://example.org/Trees2" > 
 <xsd:import namespace="http://example.org/Trees"/>
   <xsd:simpleType name="treeAge2" >
     <xsd:restriction xmlns:t2="http://example.org/Trees"             
                      base="t2:treeAge" >
       <xsd:maxInclusive value="500" />
     </xsd:restriction>
   </xsd:simpleType>
   <xsd:element name="treeage2" type="tns:treeAge2" />
</xsd:schema>'


-- this would fail if the XML schema collection
-- was used to type an XML data type column
DROP XML SCHEMA COLLECTION peoplecoll

-- allow schema collection create
GRANT CREATE XML SCHEMA COLLECTION TO public

-- allow reference in a column
GRANT REFERENCES ON XML SCHEMA COLLECTION::peoplecoll
TO FRED