我的XML架构出了什么问题?

时间:2012-01-18 18:32:48

标签: java xml xsd

这是my.xsd中的架构:

<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' 
  xmlns:p='some-namespace' targetNamespace='some-namespace'>
  <xs:element name='root' type='p:main'/>
  <xs:complexType name='main'>
    <xs:sequence>
      <xs:element name='alpha' type='xs:string' />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

这是我正在验证的XML文档:

<root xmlns='some-namespace' 
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
  xsi:schemaLocation='some-namespace my.xsd'>
  <alpha>xxx</alpha>
</root>

SAX解析器说:

"Invalid content was found starting with element 'alpha'. One of 
'{alpha}' is expected."

怎么了?

1 个答案:

答案 0 :(得分:4)

你必须添加

elementFormDefault="qualified"
您的架构定义中的

。不使用相对命名空间也是一个好主意,即使用类似这样的东西:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:p="http://some-namespace" targetNamespace="http://some-namespace"
elementFormDefault="qualified">
相关问题