2011-09-21 181 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。