dirkpitt125
dirkpitt125

Reputation: 11

XML XSD Validation Error

I have the following XSD

<xs:schema targetNamespace="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010"
           xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Product">
    <xs:complexType>
      <xs:all>
        <xs:element name="ProductColor" minOccurs="0" nillable="true" />
        <xs:element name="ProductID" minOccurs="1" maxOccurs="1" type="xs:unsignedByte" />
        <xs:element name="ProductName"  minOccurs="1" maxOccurs="1" type="xs:string" />
        <xs:element name="ProductNumber"  minOccurs="1" maxOccurs="1" type="xs:string" />
        <xs:element name="ProductPrice"  minOccurs="1" maxOccurs="1" type="xs:decimal" />
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

I am validating the following XML with that XSD

<Product xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <ProductID>1</ProductID>
    <ProductName>Adjustable Race</ProductName>
    <ProductNumber>AR-5381</ProductNumber>
    <ProductPrice>5.48</ProductPrice>
</Product>

It is giving a validation error.

The element 'Product' in namespace 'http://microsoft.com/schemas/VisualStudio/TeamTest/2010' has invalid child element 'ProductID' in namespace 'http://microsoft.com/schemas/VisualStudio/TeamTest/2010'. List of possible elements expected: 'ProductID, ProductName, ProductPrice, ProductNumber, ProductColor'.

Is the XML not formatted correctly?

Upvotes: 1

Views: 176

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66783

With your current schema, it is expecting each of the Product child elements not to be bound to a namespace.

If your intent was that all of the Product element children should be in the targetNamespace like the Product element, you need to indicate that they should be qualified. The default behavior is that they are unqualified.

You could specify on each element with the default="qualified" attribute:

<xs:element name="ProductColor" default="qualified" minOccurs="0" nillable="true" />

Or you can specify it globally on the xs:schema element with the elementFormDefault="qualified" attribute:

<xs:schema targetNamespace="http://microsoft.com/schemas/VisualStudio/TeamTest/2010"
           xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010"
           xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           elementFormDefault="qualified">

    <xs:element name="Product">
        <xs:complexType>
            <xs:all>
                <xs:element name="ProductColor" minOccurs="0" nillable="true" />
                <xs:element name="ProductID" minOccurs="1" maxOccurs="1" type="xs:unsignedByte" />
                <xs:element name="ProductName" minOccurs="1" maxOccurs="1" type="xs:string" />
                <xs:element name="ProductNumber" minOccurs="1" maxOccurs="1" type="xs:string" />
                <xs:element name="ProductPrice" minOccurs="1" maxOccurs="1" type="xs:decimal" />
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>

Upvotes: 1

Related Questions