我在XML模式比特新手。我将不胜感激,如果有人帮助我理解为什么我的XML不被与模式验证:

下面是我的架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/testSchema" xmlns="http://www.example.org/testSchema">
  <xs:element name="Employee">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Name">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="FirstName" />
              <xs:element name="LastName" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

下面是我测试的XML:

<?xml version="1.0" encoding="UTF-8"?>
<Employee xmlns="http://www.example.org/testSchema">
 <Name>
  <FirstName>John</FirstName>
  <LastName>Smith</LastName>
 </Name>
</Employee>

我被Eclipse的XML编辑器获得以下错误/验证器:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'Name'. One of '{Name}' is expected.

我不明白什么是错的这个模式还是我的XML。

有帮助吗?

解决方案

所有的u必须做的是添加将elementFormDefault =“合格”和U将被罚款。理解这种行为,请阅读“您是否合格?”部分@ http://msdn.microsoft.com/en-us/library/ ms950796.aspx

其他提示

只需添加将elementFormDefault = “合格” 到架构attribues。

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"         
       targetNamespace="http://www.example.org/testSchema"
       elementFormDefault="qualified"
       xmlns="http://www.example.org/testSchema">

和你原来工作的意愿

 <?xml version="1.0" encoding="utf-8"?>
   <Employee xmlns="http://www.example.org/testSchema">
     <Name>
      <FirstName>John</FirstName>
      <LastName>Smith</LastName>
   </Name>
 </Employee>

喜欢你身上看起来会没有指定如何验证FirstNameLastName元素;给那些type="xsd:string"(其中xsd需要被映射到XML模式数据类型名称空间,当然)的元件的规格和所有应该很好。

但你最好不要嵌套这些元素如此之深。把他们都在同一水平和使用ref="Name"到它们放在一起,而不是链接;它使你的架构更加灵活和可用的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top