Imagine I have a basic.xsd
file defining some attribute myAttribute
in some element myElement
as a xsd:string
.
Now I would like to import this basic.xsd
in my special.xsd
and make the definition of myAttribute
stricter, only allowing the value a
or b
. Some other implementer of the anotherSpecial.xsd
might want to restrict the possible values to lorem
and ipsum
.
How can this be done? Can I make the attribute abstract?
Attributes or elements cannot be "redefined"; so you will have to make sure that your attribute is of a simple type that is defined globally; whether the attribute is global or not, it'll not make a difference. Attributes, unlike elements, cannot be marked as abstract.
I'll take it literally that you want to change the domain of values for an attribute associated with an element in basic.xsd, and nothing else. This is how I would do it:
basic.xsd
<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="myElement">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="myAttribute" type="tmyAttribute"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="tmyAttribute">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:schema>
Valid sample XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<myElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" myAttribute="myAttribute1" xmlns="http://tempuri.org/XMLSchema.xsd">myElement1</myElement>
special.xsd
<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:redefine schemaLocation="basic.xsd">
<xsd:simpleType name="tmyAttribute">
<xsd:restriction base="tmyAttribute">
<xsd:enumeration value="a"/>
<xsd:enumeration value="b"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:redefine>
</xsd:schema>
When validating against special.xsd, the above XML is now invalid; change the myAttribute attribute value in the sample XML to a, and it'll work.
If someone using this XSD has an issue with the redefine, as an e.g. tools that bind XSDs to code, then I would look at an XML Schema refactoring tool that can take your special.xsd and automatically converted to its equivalent format, without the xsd:redefine. What you would practically be doing is to keep the "intelectual property" that is in special.xsd separate from basic.xsd; while changes to the basic.xsd could automatically be maintained and made visible to the consumers of the special.xsd without one having to "recode" XSD over and over again...