jax
jax

Reputation: 38643

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

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

Upvotes: 9

Views: 8669

Answers (1)

daz-fuller
daz-fuller

Reputation: 1231

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>

Upvotes: 6

Related Questions