这是 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."
怎么了?
最佳答案
你必须添加
elementFormDefault="qualified"
在您的架构定义中。不使用相对命名空间也是一个好主意,即使用这样的东西:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:p="http://some-namespace" targetNamespace="http://some-namespace"
elementFormDefault="qualified">
关于java - 我的 XML 模式有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8915411/