locked
XML Schema: substitute "imports" with inline schema? RRS feed

  • Question

  • I got XML Schema "Business.xsd" importing another schema "DataTypes.xsd".
    Is it possible to place 2nd schema inside the 1st one? I know that schema can be placed inside XML file, but what about placing it inside another schema? I want to avoid using 2 files.

    Probably you may suggest to "mix" all 2nd schema elements right inside the 1st one - i.e. have just ONE single schema.
    But I can't do that. Here is how the 2nd schema looks like:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns="urn:schemas-microsoft-com:datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:schemas-microsoft-com:datatypes">
     <xs:attribute name="dt" default="string">
      <xs:simpleType>
       <xs:restriction base="xs:string">
        <xs:enumeration value="Date"/>
        <xs:enumeration value="Fixed.14.4"/>
        <xs:enumeration value="Time"/>
        <xs:enumeration value="number"/>
            <xs:enumeration value="int"/>
            <xs:enumeration value="string"/>
          </xs:restriction>
      </xs:simpleType>
     </xs:attribute>
    </xs:schema>

    This schema is imported into the 1st one, like this:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dt="urn:schemas-microsoft-com:datatypes" elementFormDefault="qualified">
      <xs:import namespace="urn:schemas-microsoft-com:datatypes" schemaLocation="DataTypes.xsd"/>
      <xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
    ...

    Besides, the 2nd schema ("DataTypes.xsd") stuff isn't used in the 1st ("Business.xsd") schema directly. It is used only inside my XML files, like this:

    <Field name="Amount" dt:dt="Fixed.14.4">123,05</Field>
    <Field name="DeliveryDate" dt:dt="Date">20091231</Field>

    Of course, to make this possible, I made the root element of my XML file look like this:

    <RootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Business.xsd" xmlns:dt="urn:schemas-microsoft-com:datatypes">

    Thus - using two XSD-files - I make it possible to use "old" (XDR) data types in my XML files. For me this is better because SQL Server has "native" support of these types.

    But having these two schemas "bound" (1st imports 2nd), I got problems with the CREATE XML SCHEMA COLLECTION command at SQL Server (2005): I have to pass (to this command) both schemas as strings (not files!), and it seems that their "binding" is lost (because the "imports" element of the 1st schema points to file, namely - to "DataTypes.xsd", see above). That's why I'm trying to avoid using two different files.

    Thank you.
    I hope explained clearly :-)

    Tuesday, December 29, 2009 5:18 PM

Answers

  • I am not aware of any way defined in the W3C XML schema specification to include a second schema into a first one. You could not even move the element/attribute/type definitions from the second into the first schema if target namespace differ (which seems to be the case in your sample as you use xs:import and not xs:include).
    As for the SQL Server schema collection part of your question, I don't have much experience with schemas in SQL server, you might want to ask in an SQL Server group, as this group is more about Microsoft's .NET XML classes (i.e. System.Xml, System.Xml.Schema).
    MVP XML My blog
    Tuesday, December 29, 2009 5:39 PM

All replies

  • Misprint. The last code sample should look like this:

    <RootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Business.xsd" xmlns:dt="urn:schemas-microsoft-com:datatypes">

    Tuesday, December 29, 2009 5:21 PM
  • I am not aware of any way defined in the W3C XML schema specification to include a second schema into a first one. You could not even move the element/attribute/type definitions from the second into the first schema if target namespace differ (which seems to be the case in your sample as you use xs:import and not xs:include).
    As for the SQL Server schema collection part of your question, I don't have much experience with schemas in SQL server, you might want to ask in an SQL Server group, as this group is more about Microsoft's .NET XML classes (i.e. System.Xml, System.Xml.Schema).
    MVP XML My blog
    Tuesday, December 29, 2009 5:39 PM
  • Hi,

     

    I am writing to check the status of the issue on your side.  Martin's post is really helpful.  Would you mind letting us know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, January 5, 2010 6:57 AM
    Moderator