2011-09-21 105 views
7

,当我想作一个简单的架构空elment错误验证XML模式

<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> 

验证对XML时,我得到了以下错误在XSD:

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'. 
+0

btw,'xsd:schema'没有在你的上面的代码中关闭,可能是相关的吗? – Piskvor

+0

不,这只是在格式化方面的错误,这段代码适用于我,但从xmlns:tns =“http://xml.netbeans.org/schema/first”中删除“tns”之后,我直到现在才明白原因! – palAlaa

回答

7

如果您改变XSD和xsd:schema e将xmlns="http://xml.netbeans.org/schema/first"它应该工作(它对我来说)

+0

优秀的答案,免费的@jeremyhare。 – Gangnus

13

这将工作,添加'吨:'的类型。

<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在在这种情况下“http://xml.netbeans.org/schema/first”的模式的targetNamespace限定。为了引用它,你需要包含被定义的命名空间。在你的例子中,试图在没有定义的默认命名空间中引用prodtype。