2011-01-31 64 views
0

我有想要寫的模式爲兩個XML示例:這個XML例子可以寫出一個模式嗎?

實施例1:

<attributes>
<attribute type="test" value="yes"/>
</attributes>

實施例2:

<attributes>
<attribute type="test">
<value x="100" y="50">yes</value>
</attribute>
</attributes>

實施例3:

<attributes>
<attribute type="test" value="no">
<value x="100" y="50">yes</value>
</attribute>
</attributes>

它是p可以有一個適用於這些模式? 「價值」是一個屬性,另一個是一個元素?

更新 對,我想我明白我的問題了。

我有這樣的XSD:

<xs:complexType name="Attribute"> 
    <xs:sequence > 
     <xs:element name="value" type="Value" minOccurs="0" maxOccurs="unbounded"> 
     </xs:element> 
    </xs:sequence> 
    <xs:attribute name="type" type="xs:string" use="required"> 
    </xs:attribute> 
    <xs:attribute name="value" type="xs:string" > 
    </xs:attribute> 
</xs:complexType> 

但是,當我嘗試使用JAXB從XSD生成Java類我得到一個錯誤:

[xjc] [ERROR] Property "Value" is already defined. Use &lt;jaxb:property> to resolve this conflict. 
    [xjc] line 275 of file:common.xsd 
    [xjc] [ERROR] The following location is relevant to the above error 
    [xjc] line 286 of file:common/common.xsd 
    [xjc] failure in the XJC task. Use the Ant -verbose switch for more details 

我想這是在JAXB的限制而不是XSD。它會嘗試創建兩個名爲getValue()的方法,這將失敗。

+0

它是什麼讓你覺得它不會是可能的? – 2011-01-31 22:47:28

回答

4

答案是'是',但是如果你想要一個基於value屬性的模式中的if/else語句,那麼答案是'不'。您只需創建包含所有屬性和元素值的xsd作爲可選項,並且文檔將很好地驗證。什麼是xsd驗證會爲您做的是告訴您文檔在模式中指定的規則下是有效的,但它的作用而不是 do是處理文檔內的數據,如value屬性的實際值yes/no

如果您發佈了您當前擁有的模式,並對它應該是什麼樣子有疑問,您將得到更具體的模式反饋。

+3

但是,您可以添加Schematron驗證。 – biziclop 2011-01-31 22:50:07

1

我猜你的問題是,如果我們可以爲你的例子3創建一個模式 - 你基本上有一個名爲value的屬性和一個名爲value的元素。這是可能的