Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD element with both attributes and child elements

Tags:

attributes

xsd

I want to know the correct syntax for defining an element containing both attributes and child elements (if possible). For example:

<component type="A" binding="B">   <operation name="X">     <input type="C" />   </operation>    <event name="Y">     <output type="D" />   </event> </component> 

As you can see, both elements component, operation and event have both attributes and child element. Is it possible to define this in XSD? How?

Thank you very much!

like image 265
Ste83 Avatar asked Mar 30 '12 16:03

Ste83


People also ask

What XML element that contains other elements and or attributes?

A complex element is an XML element that contains other elements and/or attributes.

Which schema type can contain sub elements and attributes?

An XML schema describes the coarse shape of the XML document, what fields an element can contain, which sub elements it can contain etc, it can also describe the values that can be placed into any element or attribute.

What is minOccurs and maxOccurs in XSD?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.

What is mixed true in XSD?

Use mixed=true "mixed" attribute allow to have character data between elements.


2 Answers

This is one possible way to define an XSD matching your XML; when learning XSD, you could enroll the help of a tool that infers the XSD for you, starting from one or more XML sample files.

<?xml version="1.0" encoding="utf-8"?> <!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)--> <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">   <xsd:element name="component">     <xsd:complexType>       <xsd:sequence>         <xsd:element name="operation">           <xsd:complexType>             <xsd:sequence>               <xsd:element name="input">                 <xsd:complexType>                   <xsd:attribute name="type" type="xsd:string" use="required" />                 </xsd:complexType>               </xsd:element>             </xsd:sequence>             <xsd:attribute name="name" type="xsd:string" use="required" />           </xsd:complexType>         </xsd:element>         <xsd:element name="event">           <xsd:complexType>             <xsd:sequence>               <xsd:element name="output">                 <xsd:complexType>                   <xsd:attribute name="type" type="xsd:string" use="required" />                 </xsd:complexType>               </xsd:element>             </xsd:sequence>             <xsd:attribute name="name" type="xsd:string" use="required" />           </xsd:complexType>         </xsd:element>       </xsd:sequence>       <xsd:attribute name="type" type="xsd:string" use="required" />       <xsd:attribute name="binding" type="xsd:string" use="required" />     </xsd:complexType>   </xsd:element> </xsd:schema> 

As you walkthrough this, you may start tweaking min/maxOccurs, use (required/optional), reusing definitions, etc. A generated XSD is a good starting point, but typically ends up being edited one way or another...

like image 51
Petru Gardea Avatar answered Nov 14 '22 13:11

Petru Gardea


I am giving below, a solution that works:

<xs:simpleType name="inputTypeType">         <xs:restriction base="xs:string" /> </xs:simpleType>  <xs:complexType name="inputType">     <xs:attribute name="type" type="inputTypeType"/>             </xs:complexType>  <xs:simpleType name="operationNameType">         <xs:restriction base="xs:string" /> </xs:simpleType>  <xs:complexType name="operationType">   <xs:sequence>     <xs:element name="input"   type="inputType" />   </xs:sequence>     <xs:attribute name="name" type="operationNameType"/> </xs:complexType>    <xs:simpleType name="outputTypeType">         <xs:restriction base="xs:string" /> </xs:simpleType>  <xs:complexType name="outputType">     <xs:attribute name="type" type="outputTypeType"/>            </xs:complexType>  <xs:simpleType name="eventNameType">         <xs:restriction base="xs:string" /> </xs:simpleType>  <xs:complexType name="eventType">   <xs:sequence>     <xs:element name="output"   type="outputType" />   </xs:sequence>     <xs:attribute name="name" type="eventNameType"/> </xs:complexType>      <xs:simpleType name="typeType">         <xs:restriction base="xs:string" /> </xs:simpleType>  <xs:simpleType name="bindingType">         <xs:restriction base="xs:string" /> </xs:simpleType>     <xs:complexType name="componentType">     <xs:sequence>                 <xs:element name="operation"   type="operationType" />         <xs:element name="event" type="eventType" />     </xs:sequence>     <xs:attribute name="type" type="typeType"/>     <xs:attribute name="binding" type="bindingType"/>        </xs:complexType>   <xs:element name="component" type="componentType" /> 

like image 21
Murthy KVM Avatar answered Nov 14 '22 13:11

Murthy KVM