I have an xsd like this
<xsd:complexType name="A"> <xsd:complexContent> <xsd:sequence> <xsd:element name="options"> <xsd:complexType> <xsd:sequence> <xsd:element name="Day"> ... </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="B"> <xsd:complexContent> <xsd:extension base="A"> ...What would go here... </xsd:extension> </xsd:complexContent> </xsd:complexType>
So basically I want class A to have a sequence of options (Day, Week for example) then I want B to inherit from A and have all of A's options and an additional 2 or 3 options like hours, seconds.
The inheritance features in XML Schema allow you to create type hierarchies that capture the data models of the underlying software systems that XML is designed to support.
XML Schema Definition or XSD is a recommendation by the World Wide Web Consortium (W3C) to describe and validate the structure and content of an XML document. It is primarily used to define the elements, attributes and data types the document can contain.
XSD is based and written on XML. XSD defines elements and structures that can appear in the document, while XML does not. XSD ensures that the data is properly interpreted, while XML does not. An XSD document is validated as XML, but the opposite may not always be true.
XML Schema is commonly known as XML Schema Definition (XSD). It is used to describe and validate the structure and the content of XML data. XML schema defines the elements, attributes and data types. Schema element supports Namespaces.
Here's the schema I came up with:
<?xml version="1.0" encoding="utf-8"?> <xs:schema id="inheritance" targetNamespace="http://test.com" elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:test="http://test.com" > <xs:element name="Time"> <xs:complexType> <xs:sequence> <xs:element name="First" type="test:A" /> <xs:element name="Second" type="test:B" /> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="shortOptions"> <xs:sequence> <xs:element name="Day" /> </xs:sequence> </xs:complexType> <xs:complexType name="longOptions"> <xs:complexContent> <xs:extension base="test:shortOptions"> <xs:sequence> <xs:element name="Week" /> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="A"> <xs:sequence> <xs:element name="options" type="test:shortOptions" /> </xs:sequence> </xs:complexType> <xs:complexType name="B"> <xs:sequence> <xs:element name="options" type="test:longOptions" /> </xs:sequence> </xs:complexType> </xs:schema>
Which seems to fit this xml:
<?xml version="1.0" encoding="utf-8" ?> <Time xmlns="http://test.com"> <First> <options> <Day>Today</Day> </options> </First> <Second> <options> <Day>Tomorrow</Day> <Week>This Week</Week> </options> </Second> </Time>
Simply add an <xsd:sequence>
with the required elements:
<xsd:complexType name="B"> <xsd:complexContent> <xsd:extension base="A"> <xsd:sequence> <xsd:element name="Hours"> ... </xsd:element> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With