Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define a root element in an XML Document using Schema?

Tags:

xml

xsd

Is this possible? I can't work out how to do it.

like image 857
jax Avatar asked Nov 23 '10 08:11

jax


People also ask

How do you define a root element in XML Schema?

The root element defines the first element of the XML document and defines a reference to the root complexType. The root complexType is the complexType from which all other complexTypes are related to in their ancestry, whether the root is the parent, grand parent, great grand parent, etc.

Which is the root element for any XML Schema document?

The XML document above consists of a root element, "shiporder", that contains a required attribute called "orderid".

What can be defined with an XML document schema?

The XML schema defines the shape, or structure, of an XML document, along with rules for data content and semantics such as what fields an element can contain, which sub elements it can contain and how many items can be present.

Does XML document have root tag?

Android "Valid XML document must have a root tag at line"


1 Answers

The following should work, I'd also suggest the W3 Schools section on schemas.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="rootElement" type="RootElementType"/>

  <xs:complexType name="RootElementType">
    <xs:sequence>
      <xs:element name="child1" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
      <xs:element name="child2" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="user" type="xs:string" use="required"/>
  </xs:complexType>
</xs:schema>

This should be the schema for an XML structure like this:

<rootElement user="Bob">
  <child1>Hello</child1>
  <child1>World</child1>
  <child2>Optional</child2>
</rootElement>
like image 145
daz-fuller Avatar answered Sep 19 '22 05:09

daz-fuller