验证xml架构时出错

时间:2011-09-21 05:36:32

标签: xml schema

我想为空元素制作一个简单的模式

 <product orderid="4"/>

我像这样创建了我的XSD:

<?xml version="1.0" encoding="UTF-8"?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/first"
xmlns:tns="http://xml.netbeans.org/schema/first"
elementFormDefault="qualified">

<xsd:element name="product" type="prodtype"/>
<xsd:complexType name="prodtype">
        <xsd:attribute name="prodid" type="xsd:integer"/>
</xsd:complexType>

</xsd:schema>

在针对XSD验证XML时出现以下错误:

 Error resolving component 'prodtype'. It was detected that 'prodtype' has no namespace, but components with no target namespace are not referenceable from schema document 'file:/D:/Teacher%20assistant%202011/First%20term/web%20services/test%20programs/BpelModule1/src/product.xsd'. If 'prodtype' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'prodtype' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:/D:/Teacher%20assistant%202011/First%20term/web%20services/test%20programs/BpelModule1/src/product.xsd'.

2 个答案:

答案 0 :(得分:13)

这将有效,将'tns:'添加到类型中。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://xml.netbeans.org/schema/first"
    xmlns:tns="http://xml.netbeans.org/schema/first"
    elementFormDefault="qualified">    
    <xsd:element name="product" type="tns:prodtype"/>
    <xsd:complexType name="prodtype">
        <xsd:attribute name="prodid" type="xsd:integer"/>
    </xsd:complexType>    
</xsd:schema>

prodtype在架构的targetNamespace中定义,在本例中为“http://xml.netbeans.org/schema/first”。为了引用它,您需要包含定义的命名空间。在您的示例中,尝试引用未定义的默认命名空间中的prodtype。

答案 1 :(得分:7)

如果您更改XSD并将xmlns="http://xml.netbeans.org/schema/first"放入xsd:schema元素中,它应该可以正常工作(它适用于我)