问题描述
我创建了以下 XSD(使用 Eclipse):
I created the following XSD (with Eclipse):
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.example.com" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.com">
<element name="Make">
<complexType>
<sequence>
<element name="Scope"></element>
</sequence>
</complexType>
</element>
</schema>
并使用这个简单的 XML 进行验证
and validating with this simple XML
<?xml version="1.0"?>
<Make xmlns="http://www.example.com">
<Scope>
</Scope>
</Make>
给予:
xmllint.exe --noout --schema sources.xsd sources.xml
sources.xml:3: element Scope: Schemas validity error : Element '{http://www.example.com}Scope': This element is not expected. Expected is ( Scope ).
sources.xml fails to validate
在我看来,这必须是正确的:XML 文件位于命名空间 http:///www.example.com(验证器也是这么说的).
In my opinion, this must be correct: the XML file is in the namespace http://www.example.com (what also the validator says).
对于 XSD,我将默认命名空间设置为 XSD 模式(这是 Eclipse 所做的,所以它应该是正确的!)并且我给出了正确的 targetNamespace.我也试过用
And for the XSD I set the default namespace to the XSD schema (this is what Eclipse does, so it should be correct!) and I give the correct targetNamespace. I also tried to use
<element name="tnd:Scope" />
但是,这也不起作用.
这是 xmllint 中的错误还是问题出在哪里?
Is this a bug in xmllint or where is the problem?
问候divB
推荐答案
@dbasemans 的替代方法是将 elementFormDefault
指定为 qualified:
An alternative to @dbasemans answer would be to specify the elementFormDefault
as qualified:
<schema targetNamespace="http://www.example.com"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.com"
elementFormDefault="qualified">
为您的架构命名空间使用 xsd
或 xs
前缀可能被认为是常见的,因此可能希望选择按照 dbaseman.
Using the xsd
or xs
prefix for your schema namespace could be considered as common, so may want to choose to modify your schema as suggested by dbaseman.
这篇关于XSD 验证错误:元素“{http://www.example.com}Scope":不需要此元素.预期是(范围)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!