简体   繁体   English

如何使用名称空间针对XSD验证XML文件

[英]How to validate xml file against xsd with namespace

I want to validate xml file contain namespace with xsd file. 我想验证xml文件包含带有xsd文件的名称空间。

My xml file: 我的xml文件:

<?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文件:

    <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: Affer调用方法验证后,出现以下异常:

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. 每个人都知道如何配置xsd文件中的名称页验证。 Please help me. 请帮我。

Thanks 谢谢

You haven't shown the critical part of your schema, where the targetNamespace is defined. 您尚未显示定义targetNamespace的架构的关键部分。

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. 如果架构中的targetNamespace与实例文档中的targetNamespace相匹配,则应该没有问题,并且不需要执行任何特殊操作。

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.. 正确的xsd是这个。

<?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 通过这种方式,xsd验证有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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