Note that there are some explanatory texts on larger screens.

plurals
  1. PODefinition of custom types as restriction within XML schema
    primarykey
    data
    text
    <p>I am to writing a generator for source code (multiple languages). The classes, i.e. basic data containers, should be specified as XML files. To automatically validate and parse these XML files, I am defining an XSD schema. This should be a valid file:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;class&gt; &lt;customType name="vector3D"&gt; &lt;variable name="x" type="int"/&gt; &lt;variable name="y" type="int"/&gt; &lt;variable name="z" type="int"/&gt; &lt;/customType&gt; &lt;variable name="identifier" type="string"/&gt; &lt;variable name="direction" type="vector3D"/&gt; &lt;/class&gt; </code></pre> <p>I defined my root element <code>class</code>, and the elements <code>customType</code> and <code>variable</code> as:</p> <pre><code>&lt;xsd:complexType name="Class"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="customType" type="CustomType" minOccurs="0" maxOccurs="unbounded"/&gt; &lt;xsd:element name="variable" type="Variable" minOccurs="0" maxOccurs="unbounded"/&gt; &lt;/xsd:sequence&gt; &lt;xsd:complexType&gt; &lt;xsd:complexType name="CustomType"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="variable" type="Variable" minOccurs="0" maxOccurs="unbounded"/&gt; &lt;/xsd:sequence&gt; &lt;xsd:attribute name="name" type="xsd:string" use="required"/&gt; &lt;/xsd:complexType&gt; &lt;xsd:complexType name="Variable"&gt; &lt;xsd:attribute name="name" type="xsd:string" use="required"/&gt; &lt;xsd:attribute name="type" type="ValidType" use="required"/&gt; &lt;/xsd:complexType&gt; </code></pre> <p>However, I am struggling heavily trying to allow a limited set of base types <strong>and</strong> the names defined in <code>customType</code> tags. Defining my set of base types was easy:</p> <pre><code>&lt;xsd:simpleType name="ValidType"&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:enumeration value="bool"/&gt; &lt;xsd:enumeration value="int"/&gt; &lt;xsd:enumeration value="string"/&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; </code></pre> <p>But is there any way I can also allow identifiers defined in the <code>name</code> attribute of <code>customType</code> tags, or do I have to allow any <code>xsd:string</code> and check for validity within my generator?</p> <p><strong>Edit</strong></p> <p>If I understood <a href="http://www.w3.org/TR/xmlschema11-1/#Simple_Type_Definitions" rel="nofollow">3.16.2 of the W3C XML Schema Definition Language (XSD) Recommendation</a> correctly, what I want cannot be done using XSD (because restrictions are limited to <code>minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern | assertion | explicitTimezone</code>, which do not support this kind of dynamic restriction), and I have to do this after validation of the XSD schema manually.</p> <p>Can anyone confirm that this is correct?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload