2014-10-07 74 views
1

我有一個簡單類型的簡單模式定義:XSD定義簡單類型+限制+ anyAttribute

<xs:simpleType name="std_types-bit-type"> 
    <xs:restriction base="xs:int"> 
    <xs:minInclusive value="0"/> 
    <xs:maxInclusive value="1"/> 
    </xs:restriction> 
</xs:simpleType> 

其他地方有:

<xs:element name="comp" type="std_types-bit-type"/> 

一個可以創建這樣的XML元素:

<comp>1</comp> 

我想允許文檔創建者添加任意(對我來說,但對他們有意義)屬性如:

<comp index="2">1</comp> 

既然不能提前知道的所有可能的屬性,xs:anyAttribute適用於只是這種情況。事實上,爲complexTypes定義這個很簡單。問題在於,我無法找到模式元素的神奇組合,它們讓xs:anyAttribute與simpleType/simpleContent限制相關聯。

我無法想象這是不可能的,但它迄今爲止都在毆打我。

回答

0

屬性可以在類型是通過擴展派生時添加;方面可以添加一個類型是由限制派生的。 所以,讓自己兩個定義 ...

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:simpleType name="std_types-bit-base-type"> 
    <xs:restriction base="xs:int"> 
     <xs:minInclusive value="0"/> 
     <xs:maxInclusive value="1"/> 
    </xs:restriction> 
    </xs:simpleType> 

    <xs:complexType name="std_types-bit-type"> 
    <xs:simpleContent> 
     <xs:extension base="std_types-bit-base-type"> 
     <xs:anyAttribute/> 
     </xs:extension> 
    </xs:simpleContent> 
    </xs:complexType> 
</xs:schema> 
+0

呀,我害怕這一點。一個簡單的元素定義爲兩個。由於架構是工具生成的(而且我正在編寫該工具),這意味着更復雜。 xs:anyAttribute也必須存在於擴展和限制(或xmllint樹皮)中。 – 2014-10-07 15:31:47

+0

我回滾了我的編輯,因爲我誤解了你的答案。我的一個解決方案涉及將xs:int(將xs:anyAttribute添加到complexType中),然後限制它。 xs:attribute在xs:complexType中的xs:simpleContent內的xs:restriction內是有效的。重讀你的答案,這次我看到你先限制了基本類型,然後用xs:anyAttribute擴展它。這顯然是一個更清潔的方法。對不起,錯誤... – 2014-10-07 19:29:50