XmlSchemaValidationException元素不能包含空格

时间:2018-05-18 13:06:52

标签: xml xsd xsd-validation

当我尝试验证XML方案时,我得到以下异常:

System.Xml.Schema.XmlSchemaValidationException: 'The element cannot contain white space. Content model is empty.'

我正在阅读相同的错误,但我似乎无法在这些帖子中找到解决方案。

谁能告诉我我做错了什么以及为什么?

XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="urn://Test.Deploy" elementFormDefault="qualified" xmlns="urn://Test.Deploy" xmlns:mstns="urn://Test.Deploy" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="configuration">
    <xs:attribute name="id" type="xs:string" use="required"/>
    <xs:attribute name="description" type="xs:string" use="required"/>
    <xs:attribute name="deployDir" type="xs:anyURI" use="optional"  default="xs:"/>
  </xs:complexType>

  <xs:complexType name="service">
    <xs:attribute name="name" type="xs:string" use="required" />
    <xs:attribute name="order" type="xs:string" use="optional" />
    <xs:attribute name="version" type="xs:string" use="optional" />
  </xs:complexType>

  <xs:complexType name="services">
    <xs:sequence>
      <xs:element name="service" type="service" minOccurs="0" nillable="true" />
    </xs:sequence>
    <xs:attribute name="environment" use="required" />
    <xs:attribute name="version" use="required" />
  </xs:complexType>

  <xs:element name="configuration" type="configuration"/>

  <xs:element name="services" type="services" />

</xs:schema>

XML:

<?xml version="1.0" encoding="utf-8"?> 
<configuration xmlns="urn://Test.Deploy" id="Test deploy" description="Test deploy server" deployDir="../Deploy">
  <services environment="Development" version="0.1796.0">
    <service name="Foo" order="0" />
    <service name="Bar" />
  </services>
</configuration>

1 个答案:

答案 0 :(得分:1)

您可以将XSD更改为:

<xs:schema targetNamespace="urn://Test.Deploy" elementFormDefault="qualified" xmlns="urn://Test.Deploy" xmlns:mstns="urn://Test.Deploy" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="configuration">
    <xs:sequence>
      <xs:element ref="services" minOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:string" use="required"/>
    <xs:attribute name="description" type="xs:string" use="required"/>
    <xs:attribute name="deployDir" type="xs:anyURI" use="optional"  default="xs:"/>
  </xs:complexType>

  <xs:complexType name="service">
    <xs:attribute name="name" type="xs:string" use="required" />
    <xs:attribute name="order" type="xs:string" use="optional" />
    <xs:attribute name="version" type="xs:string" use="optional" />
  </xs:complexType>

  <xs:complexType name="services">
    <xs:sequence>
      <xs:element name="service" type="service" minOccurs="0" maxOccurs="unbounded" nillable="true" />
    </xs:sequence>
    <xs:attribute name="environment" use="required" />
    <xs:attribute name="version" use="required" />
  </xs:complexType>

  <xs:element name="configuration" type="configuration"/>
  <xs:element name="services" type="services" />
</xs:schema>

您错过了servicesconfigurationmaxOccurs="unbounded"services的引用。