2012-04-09 95 views
1

這是我的問題。在節點中,我希望有一個屬性或一個子節點(來自一個組)。例如:屬性或子節點的XML模式

<set_variable name="bob" value="3/> 

<set_variable name="bob"> 
    <.../> 
</set_variable> 

我發現做到這一點的唯一方法是:

<xs:element name="set_variable" type="setVariableType"/> 

<xs:complexType name="setVariableType"> 
    <xs:sequence minOccurs="0" maxOccurs="1"> 
     <xs:group ref="arithmeticGroup"/> 
    </xs:sequence> 
    <xs:attribute name="name" type="xs:string" use="required"/> 
    <xs:attribute name="value" type="xs:integer" use="optional"/> 
</xs:complexType> 

<xs:group name="arithmeticGroup"> 
    .... 
</xs:group> 

與解決方案的問題在於,它允許兩個禁止方式:

<set_variable name="bob"/> <!-- Should at least have an attribute or a subnode --> 

and

<set_variable name="bob" value="18"> <!-- Should not have both attribute and a subnode --> 
    <.../> 
</set_variable> 

任何人都有想法嗎?

回答

2

不幸的是,這對於XSD 1.0是不可能的;您必須在XSD 1.0之上使用Schematron斷言,或者依靠XSD 1.1 assertionstype alternatives(僅在4月5日以W3C rec形式僞造)和兼容處理器(無空閒,很少可用)。

+0

謝謝,即使在XSD 1.0中沒有解決方案... – Zaphod 2012-04-09 13:54:26

+0

您好Petru,我也在尋找這個。您能否指出我在XSD 1.1規範中的這一點成爲可能(即元素和屬性之間的選擇)? (或者,也許是工作組的在線討論,他們在哪裏談論它?)我也想看看在舊的XSD 1.0規範中哪裏是不可能的 - 但我認爲這不太可能只是一個地方指向!此外,只是順便說一句,你認爲XSD 1.1可能會被廣泛採用嗎?非常感謝您給出的任何答案! – 13ren 2012-09-07 13:19:54

+1

@ 13ren,我已經更新了與XSD 1.1斷言鏈接的答案。無法實現XSD 1.0所要求的事實與如何在1.0中定義內容模型有關。沒有涉及屬性的內容模型選擇的概念(只能選擇處理粒子)。 XSD 1.1現在支持它 - 參見[Type Alternatives](http://www.w3.org/TR/xmlschema11-1/#cTypeAlternative)。至於如何廣泛採用,這主要取決於主要參與者進入的速度。微軟在這個領域並沒有表現出任何努力... ... – 2012-09-07 14:17:06