Is it possible to define a type which can be either a keyref or a number literal? Just like in any typed programming language you can assign either a number literal OR the name of another variable to a numeric variable. I’m making a schema to define drawing api elements (for another programming language) and would like to define a color type that can either be a hexadecimal literal (such as 0xFF0000 for bright red) OR be a reference to a color defined elsewhere. So you could do (in a XML document):
<color key="dialogBorder1">0x222222</color>
<color key="dialogFill1">0xCCCCCC</color>
<!-- later... -->
<windowTheme name="warningWindow">
<border>
<color>0xFF0000</color> <!-- defined literally -->
</border>
<fill>
<solid>
<color>dialogFill1</color> <!-- defined by keyref -->
</solid>
</fill>
</windowTheme>
If it were possible to impose a choice restriction on attributes I could do something like the following, but I am under the impression this is not possible with current (1.0) version of XSD spec.
<!-- I wish: -->
<xs:complexType name="colorType" >
<xs:attrchoice>
<xs:attribute name="value" type="HexLiteral" /> <!-- literal -->
<xs:attribute name="ref" type="xs:string" /> <!-- keyref (defined elsewhere) -->
</xs:attrchoice>
</xs:complexType>
Which would allow either lieral value or keyref ref:
<color value="0xFF0000" /> <!-- OK -->
<color ref="dialogBorder1" /> <!-- OK -->
But not both:
<color value="0xFF0000" ref="colorXYZ" /> <!-- NOT OK -->
The post is somewhat inconsistent in what’s describing. The first XML shows color used without attributes, then the xsd for
colorTypegoes off with some attributes. I assume the XML is what you wanted.So the following works for:
To define a color type that can either be a hexadecimal literal (such as 0xFF0000 for bright red) OR be a reference to a color defined elsewhere
The above uses the same pattern as the
Colortype in XHTML (I am showing this to give you sources of inspiration):The idea here is to use a union. The downside may be that since all that’s not matching the HEX pattern will be matched by the string, invalid HEX syntax (e.g. missing digits) will pass through as references.
Other downsides may be in how well is xsd:union supported by the programming languages that you’re targeting.