2011-03-30 49 views
-1

嘿,我在指定文檔範圍的定義屬性時遇到困難。我使用屬性內部的屬性使用ref屬性,我相信它應該可以工作。但是當我嘗試驗證我的測試XML文件時,我只是得到一個錯誤,說明屬性沒有被定義。XSD:文檔範圍的屬性

如果我用屬性定義替換ref-屬性的內聯聲明或甚至attributeGroup,那麼它就神奇地起作用。 Visual Studio的自動完成建議我再次使用某個前綴導入名稱空間,並希望在屬性上使用該前綴,儘管名稱空間本身已作爲默認名稱空間導入(並且是唯一使用的名稱空間)。

我已經煮了我的XML和我的XSD文件都差不多,所以這是留給:

<?xml version="1.0" encoding="utf-8"?> 
<foo xmlns="http://example.com/test" attr="xy" /> 
<!-- Visual Studio autocompletes to this, which works too: 
<foo xmlns="http://example.com/test" a:attr="xy" xmlns:a="http://example.com/test" /> 
--> 

這是方案:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns="http://example.com/test" targetNamespace="http://example.com/test" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 

    <xs:attribute name="attr" type="xs:string" /> 

    <xs:attributeGroup name="attrGroup"> 
    <xs:attribute name="attr" type="xs:string" /> 
    </xs:attributeGroup> 

    <xs:element name="foo"> 
    <xs:complexType> 
     <xs:attribute ref="attr" /> 
     <!-- These two examples work: 
     <xs:attributeGroup ref="attrGroup" /> 
     <xs:attribute name="attr" type="xs:string" /> 
     --> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

什麼是這個原因行爲和我能做些什麼來解決它?

回答

3

ref="attr"是頂層屬性聲明與name="attr",其聲稱,在該模式文檔的目標命名空間的屬性的參考。這通常不是你想要的,這就是爲什麼頂級屬性聲明很少被看到的原因。最好引用包含名爲"attr"的本地屬性的屬性組,該屬性組(因爲attributeFormDefault隱式地不合格)不在命名空間中。

+0

啊,謝謝。所以每個默認屬性都不在任何名稱空間中? – poke 2011-03-30 19:32:45