Rimantas Radžiūnas
Rimantas Radžiūnas

Reputation: 257

XML: can't have multiple elements with provided schema

I'm kinda new to XML and learning XML Schema right now. I've encountered problem where I cannot create more than one car element with provided xml schema.

Right now I have a simple schema like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.cars.lt"
            xmlns="http://www.cars.lt"
            elementFormDefault="qualified"
            xmlns:carsType="http://www.cars.lt">

  <xs:element name="cars" type="carsType:CarType">
  </xs:element>

  <xs:complexType name ="CarType">
    <xs:sequence>
      <xs:element name="car" type="carsType:CarWithBrandAndModel">
        <xs:key name="carKey">
          <xs:selector xpath="car"/>
          <xs:field xpath="@id"/>
        </xs:key>
        <xs:keyref name="NoCarsRef" refer="carKey">
                    <xs:selector xpath="noCar" />
                    <xs:field xpath="@id" />
                </xs:keyref>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

This schema generates only one car element when instead I need to have more than one.

And when I'm trying to create two <car> elements it gives me an error that element cars has invalid child element car in namespace http://www.cars.lt.

What I tried was inserting CarType complex type into cars element, but it gives me the same error.

Upvotes: 1

Views: 36

Answers (1)

kjhughes
kjhughes

Reputation: 111726

The default for maxOccurs is 1. If you want your XSD to allow multiple car elements, add maxOccurs with a value greater than 1 or unbounded:

<xs:element name="car" type="carsType:CarWithBrandAndModel" maxOccurs="unbounded">

Upvotes: 1

Related Questions