我尝试为我的xml文件定义一些xsd方案。
xml结构类似于
<?xml version="1.0" encoding="UTF-8"?>
<product name="abc" xmlns="http://example.org/productMetadata.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.org/productMetadata.xsd productMetadata.xsd">
<metainf />
</product>
(根标签带有一些已定义的属性&#34;名称&#34;以及一些嵌套标签,如示例&#34; metainf&#34;)
我定义xsd的方法看起来像
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.org/productMetadata.xsd" xmlns="http://example.org/productMetadata.xsd">
<xsd:element name="product">
<xsd:complexType>
<xsd:all>
<xsd:element type="xsd:string" name="metainf" />
</xsd:all>
<xsd:attribute type="xsd:string" name="name" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
尽管如此,我还是无法针对xsd验证xml。
根据验证器(我使用java,web-app和eclipse),我收到以下失败消息。
从元素&#39; metainf&#39;开始发现无效内容。其中一个{metainf}&#39;是预期的。
或
Cvc-complex-type.2.4.a:从Element&#39; metainf&#39;开始发现无效内容。其中一个{metainf}&#39;预期。,Line&#39; 5&#39;,Column&#39; 13&#39;。
任何人提示,我的xsd或xml有什么问题。
答案 0 :(得分:2)
只需在elementFormDefault="qualified"
声明中添加xsd:schema
,就像这样:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://example.org/productMetadata.xsd" xmlns="http://example.org/productMetadata.xsd">
根据elementFormDefault
属性的文档:
在此架构的目标命名空间中声明的元素的表单。该值必须为“合格”或“不合格”。默认为“不合格”。
- “ unqualified ”表示目标命名空间中的元素不需要使用命名空间前缀限定。
- “ qualified ”表示必须使用命名空间前缀限定目标命名空间中的元素。