根据XSD验证XML

时间:2017-03-28 21:46:41

标签: java xml xsd schema

  

我尝试验证XML和XML架构,但我收到验证错误消息" Cvc-complex-type.3.2.2:Attribute&#t; tns :submission_type'不允许在元素中出现:提交'。,行' 1',列' 224'。"

     

注意:验证错误是由xml文件中的 tns:submission_type =" testing" 引起的。但是我想在属性中使用命名空间并且它不起作用。有什么建议吗?

  

架构文件(.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     targetNamespace="http://www.example.org/NewXMLSchema" 
     xmlns:tns="http://www.example.org/NewXMLSchema" 
     elementFormDefault="qualified">

    <xsd:element name="submission" type="tns:submission"></xsd:element>
    <xsd:complexType name="submission">
        <xsd:sequence>
            <xsd:element name="header" type="tns:header_type" maxOccurs="1" minOccurs="1"></xsd:element>
            <xsd:element name="detail" type="tns:detail_type" maxOccurs="unbounded" minOccurs="0"></xsd:element>
            <xsd:element name="error" type="tns:error_type" maxOccurs="unbounded" minOccurs="0"></xsd:element>
        </xsd:sequence>
        <xsd:attribute name="submission_type" type="xsd:string"/>
    </xsd:complexType>
    <xsd:complexType name="header_type"></xsd:complexType>
    <xsd:complexType name="detail_type"></xsd:complexType>
    <xsd:complexType name="error_type"></xsd:complexType>
</xsd:schema>
  

Xml文件(.xml)

<?xml version="1.0" encoding="UTF-8"?>
<tns:submission tns:submission_type="testing" 
     xmlns:tns="http://www.example.org/NewXMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.example.org/NewXMLSchema NewXMLSchema.xsd ">
     <tns:header></tns:header>
     <tns:detail></tns:detail>
     <tns:error></tns:error>
</tns:submission>

0 个答案:

没有答案