1

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.

Hawkeye4040
  • 126
  • 10
  • Please explain what you mean by "I don't need, nor want a file type .common to exist however." Are you trying to prevent `Common.xsd` from being imported multiple times (such as [C++ #include guards](https://stackoverflow.com/q/8020113/290085) are used to prevent with C++ headers)? You generally do not have to worry about that. – kjhughes May 01 '21 at 18:06
  • Do you have an example where [`xsd:include` and `xsd:import`](https://stackoverflow.com/q/2357943/290085) are not working for you? – kjhughes May 01 '21 at 18:24
  • Okay I'll try to clarify a little better. I'm using the file common.xsd as described. `xsd:include` and `xsd:import` are working just fine elsewhere. I'm not preventing it from being imported multiple times. Importing it multiple times is why it exists so I can store many definitions such as guid as you see there in various other schemas. I'm basically using the file `common.xsd` as an abstract base class. – Hawkeye4040 May 01 '21 at 18:29
  • What I'm trying to do is prevent this schema from being used to make a `.common` file. If that is attempted I want it to not be allowed if possible, somewhat like a throwing an exception scenario. Is that at all possible? This is fairly new territory for me. – Hawkeye4040 May 01 '21 at 18:31
  • What is a `.common` file? Such a file is neither defined in XML / XSD nor implied by either standard. – kjhughes May 01 '21 at 18:32
  • Nothing at all. I don't want that to exist but I still want to use a schema to store definitions common across various other schemas in the same project so I don't have to re-write them. `Guid` is a good example. I have several different file schemas that I want to use the `guid` definition in. So rather than putting that `guid` definition in say 4 different schema files I put it in one file called `common.xsd` instead. That all works great I don't have to rewrite `guid` 4 times and that's all great but I feel as though I'm using xsd a little improper here. – Hawkeye4040 May 01 '21 at 18:36
  • Reason being is that you could technically declare a `.common` file which is illogical in this scenario and I don't want that to be allowed. Does that even matter. If say, someone develops an extension for this software and uses `.common` files it would almost certainly cause issues in various places in my applications the schemas are used for. I hope that clarifies it a little better. – Hawkeye4040 May 01 '21 at 18:38
  • XSD has nothing to do with file declarations. Your concerns remain unclear. – kjhughes May 01 '21 at 18:40
  • So that leads me to that bottom part there where I proposed the concept of an abstract schema. Just like a class or `complexType` declaration an abstract schema cannot be instantiated it just serves as a container for common definitions to be stored for its inheritors. – Hawkeye4040 May 01 '21 at 18:41
  • Well that's unfortunate that it didn't clear it up for you. I'm trying really hard here I don't quite get what you're confused about here either. – Hawkeye4040 May 01 '21 at 18:42
  • You are talking about xml schemas right? https://www.w3schools.com/XML/schema_howto.asp – Hawkeye4040 May 01 '21 at 18:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231829/discussion-between-kjhughes-and-hawkeye4040). – kjhughes May 01 '21 at 18:46

1 Answers1

1

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

kjhughes
  • 106,133
  • 27
  • 181
  • 240