2011-06-30 40 views
0

在使用XML Schema 1.1時,我想定義一個完全一起出現的屬性組,即屬性組中的所有屬性都存在或不存在。在下面的示例中,我希望<anelement>具有兩個屬性(attr_oneattr_two),或者既不存在任何屬性,也不存在單個屬性。XML Schema 1.1屬性分組約束

<attributeGroup name="attrgroup"> 
    <attribute name="attr_one" /> 
    <attribute name="attr_one" /> 
</attributeGroup> 

<element name="anelement"> 
    <complexType> 
    <attributeGroup ref="attrgroup" /> 
    </complexType> 
</element> 

據我所知,XML Schema 1.0無法指定這些屬性關係(正確?)。在XSD 1.1中指定它們的最佳方式是什麼?我想,我可以使用assert指定的關係,就像下面的內容:

<element name="anelement"> 
    <complexType> 
    <attributeGroup ref="attrgroup" /> 
    <assert test="(@attr_one and @attr_two) or not(@attr_one or @attr_two)" /> 
    </complexType> 
</element> 

但我希望有東西加到1.1,讓我用現有的語言來指定的關係,例如attributeGroup s上的use屬性。在XML Schema 1.1中指定這種關係的最佳方式是什麼?

+0

酷,我不知道的XML Schema 1.1提供了斷言這樣。 – LarsH

+0

您使用的是什麼XML Schema 1.1的實現?撒克遜? – LarsH

+0

是的,1.1。具有完整的XPath斷言。 Xerces-J支持我想要使用的所有1.1部分,因此我現在正在使用它進行驗證。希望其他工具(特別是OXM)很快就會迎頭趕上。 – Bob

回答

1

斷言是要做到這一點的方法。一個更簡單的配方是

test="exists(@attr_one) = exists(@attr_two)" 

,或者你可以做

test="count(@attr_one|@attr_two) != 1" 
+0

謝謝,那是關於我的想法。任何想法如何將你簡單的公式推廣到N個屬性而不會變得更復雜? – Bob