Search code examples
xmlxsdxmlschema

How to best use an XSD file to store common definitions for multiple schemas


I have multiple XML Schemas that have common definitions. I would like to store those definitions in a common file we'll call it Common.xsd. I have the type guid referenced in my other schemas successfully already.

I don't need, nor want a file type .common to exist however. I haven't yet been able to find a means, such as an attribute, to ensure this isn't allowed and that anything defined in Common.xsd is merely there to be inherited from.

Sample

Common.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    targetNamespace="MyAssembly:Core:IO:Schemas"
    elementFormDefault="qualified"
    xmlns="MyAssembly:Core:IO:Schemas"
    xmlns:mstns="MyAssembly:Core:IO:Schemas"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:simpleType name="guid">
    <xs:annotation>
      <xs:documentation xml:lang="en-us">
        The representation of a GUID, generally the id of an element.
      </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:pattern value="\{[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\}"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

I was hoping that this would be as simple as it is in the case of declaring types within a schema. Basically what I'm talking about here would be an abstract schema for instance:

<xs:schema abstract="true">

</xs:schema>

To clarify I do understand that this is not how it works but I'm looking for the closest way to ensure that a .commmon file is not allowed or being declared.


Solution

  • I don't need, nor want a file type .common to exist however.

    XSD has no concept of control over file types or file creation.


    Your plan to group commonly used types into a Common.xsd file is sound.

    To limit the root elements of valid XML documents, limit the globally defined elements in the XSD; locally defined elements will not be allowed to be used as the root element.

    See also