2009-09-16 67 views
0

我想基於xml屬性值指定基礎子元素的結構。例如:使用屬性指定XSD中的元素結構

<param type="uniform"> 
    <high>10</high> 
    <low>0</low> 
</param> 

<param2 type="normal"> 
    <mean>5</mean> 
    <stdev>2.5</mean> 
<param2> 

有沒有一種方法來驗證這種類型的結構使用XSD?

回答

1

不幸的是,這是一個缺乏XSD的領域 - 您無法基於屬性或元素中的值來控制結構。 XSD嚴格控制結構。

這樣的事情,你不得不使用其他的XML驗證技術,所以我建議你可能想看看的Schematron:

Schematron是一種可以定義這種依賴關係的方法(「如果此屬性的值爲XYZ,則.......」)。

馬克

+0

這是我懷疑的情況。感謝您驗證我的想法。 – 2009-09-16 20:28:57

1

您可以使用抽象類做類似的事情。

<xs:complexType name="basePqrameterType" abstract="true"/> 

其次是具體的(具體的)類型定義:

<xs:complexType name="Param_uniform"> 
    <xs:complexContent> 
     <xs:extension base="baseParameterType"> 
      <xs:attribute name="type" use="required" fixed="uniform"/> 
      ...<!--other specific restrictions for type uniform--> 
     </xs:extension> 
     </xs:complexContent> 
</xs:complexType> 

<xs:complexType name="Param_normal"> 
    <xs:complexContent> 
     <xs:extension base="baseParameterType"> 
      <xs:attribute name="type" use="required" fixed="normal"/> 
      ...<!--other specific restrictions for type normal--> 
     </xs:extension> 
     </xs:complexContent> 
</xs:complexType> 

您的XML看起來就像這樣:

<Param xsi:type="Param_normal" type="normal"/> 
<Param xsi:type="Param_uniform" type="uniform"/> 

因此,有可能具有相同名稱的元素但是由於不同類型的定義而限制它們,但是你不能通過使用屬性值來「選擇」這些類型。它必須使用'xsi:type'表示法。