简体   繁体   中英

How to validate xml file against xsd with namespace

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

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM