1

I want to validate xml file contain namespace with xsd file.

My xml file:

<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfNumberOfCars xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Api.CarWEB.Services.Cars">
    <NumberOfCars>
         <Number>417</Number>
    </NumberOfCars>
</ArrayOfNumberOfCars>

My xsd file:

    <xsd:element name="ArrayOfNumberOfCars">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element minOccurs="1" maxOccurs="1" ref="NumberOfCars" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="NumberOfCars">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element minOccurs="1" maxOccurs="1" ref="Number" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="Number">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:minLength value="0" />
                <xsd:maxLength value="15" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>

Affer call method validate, I got exception bellow:

org.xml.sax.SAXException: javax.xml.stream.XMLStreamException: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 86; cvc-elt.1: Cannot find the declaration of element 'ArrayOfNumberOfCars'.

Everyone know how to config validate for namespage in xsd file. Please help me.

Thanks

wind
  • 63
  • 1
  • 3
  • 9
  • http://stackoverflow.com/questions/15732/whats-the-best-way-to-validate-an-xml-file-against-an-xsd-file – Wundwin Born Aug 12 '14 at 11:14
  • 1
    One other point: the error message refers to an element "ArrayOfCar" which doesn't appear either in your instance document or your schema. Presumably that was just a typo in preparing your question? – Michael Kay Aug 12 '14 at 14:01

2 Answers2

2

You haven't shown the critical part of your schema, where the targetNamespace is defined.

If the targetNamespace in the schema matches that in your instance document, then you should have no problem, and you don't need to do anything special.

If they don't match, then your instance document is not valid, and the only way to make it valid is to transform it into a different document in the correct namespace (or no namespace, if that's what the schema defines).

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

The right xsd is this..

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema elementFormDefault="qualified"
    targetNamespace="http://schemas.datacontract.org/2004/07/Api.CarWEB.Services.Cars"
    xmlns:prefix="http://schemas.datacontract.org/2004/07/Api.CarWEB.Services.Cars"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="ArrayOfNumberOfCars">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element minOccurs="1" maxOccurs="1" ref="prefix:NumberOfCars" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="NumberOfCars">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element minOccurs="1" maxOccurs="1" ref="prefix:Number" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="Number">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:minLength value="0" />
                <xsd:maxLength value="15" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>

</xsd:schema>

In this way the xsd validation works

Xstian
  • 8,184
  • 10
  • 42
  • 72