2013-03-24 38 views
1

我爲一個項目製作了一個簡單的UI定義語言,現在想創建一個模式,以便於驗證。不幸的是,我的XSD技能是相當生鏽,我發現自己試圖讓我甚至不確定的東西是可能的。如何定義一個可以包含* IDREF或字符串枚舉的屬性?

UI由可以相互定位的「塊」組成。爲了簡化最常見的用例,我希望引用屬性能夠包含任何字符串parent,previousnext。爲了儘可能靈活,我也希望它能夠指向任何帶有ID的元素。

換句話說,我想下面是有效的:

<ui> 
    <block id="foo"/> 
    <block/> 
    <block anchor="previous"/> 
    <block anchor="#foo"/> 
</ui> 

我怎樣才能形容這XSD?

回答

1

事實證明,XSD包含一個功能,它完全做到了這一點 - 將兩種或更多種類型相結合 - 而我完全錯過了它。 A union創建一個類型,其詞彙空間覆蓋所有成員類型的詞彙空間(換句話說,它可以包含與其任何子類型匹配的值)。

由於IDREF s不能包含前導#(它是對ID的直接引用,而不是URL的片段標識符),所以以下模式將驗證示例XML。有趣的是AnchorTypeTreeReferenceType

<schema targetNamespace="urn:x-ample:ui" elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:ui="urn:x-ample:ui"> 
    <element name="ui" type="ui:UIType"/> 

    <complexType name="UIType"> 
     <sequence> 
      <element minOccurs="1" maxOccurs="unbounded" name="block" type="ui:BlockType"/> 
     </sequence> 
    </complexType> 

    <complexType name="BlockType"> 
     <attribute use="optional" name="id" type="ID"/> 
     <attribute name="anchor" type="ui:AnchorType"/> 
    </complexType> 

    <simpleType name="AnchorType"> 
     <union memberTypes="ui:TreeReferenceType IDREF"/> 
    </simpleType> 

    <simpleType name="TreeReferenceType"> 
     <restriction base="string"> 
      <enumeration value="parent"/> 
      <enumeration value="previous"/> 
      <enumeration value="next"/> 
     </restriction> 
    </simpleType> 
</schema> 
相關問題