Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xsd and inheritance

Tags:

xml

xsd

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.

like image 233
arinte Avatar asked Oct 15 '08 14:10

arinte


People also ask

Does XML have inheritance?

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.

What is an XSD used for?

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.

What is difference between XSD and XML?

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.

Is XSD and schema same?

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.


2 Answers

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> 
like image 137
Richard Nienaber Avatar answered Sep 17 '22 13:09

Richard Nienaber


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> 
like image 30
csgero Avatar answered Sep 21 '22 13:09

csgero