2011-02-18 128 views
1

對於以下xml需要架構。xml具有不同值的相同元素需要架構

<?xml version="1.0" encoding="UTF-8"?> 
<overall_operation> 
    <operation type="list_products"> 
     <ops_description>Listing all the products of a company</ops_description> 
     <module>powesystem</module> 
     <comp_name>APC</comp_name> 
     <prod_price>50K$</prod_price> 
     <manf_date>2001</manf_date> 
     <pool_name>Electrical</pool_name> 
     <fail_retry>2</fail_retry> 
     <storage_type>avialble</storage_type> 
     <storage_check>false</storage_check> 
     <api_type>sync</api_type> 
     <product_name>transformer</product_name> 
    </operation> 
    <operation type="search_product"> 
     <ops_description>Search the products of a company from the repository</ops_description> 
     <module>high-voltage</module> 
     <module>powesystem</module> 
     <comp_name>APC</comp_name> 
     <pool_name>Electrical</pool_name> 
     <fail_retry>2</fail_retry> 
     <storage_type>avialble</storage_type> 
     <storage_check>false</storage_check> 
     <api_type>sync</api_type> 
     <product_name>setup-transformer</product_name> 
    </operation> 
</overall_operation> 

這裏不同元件與操作等list_productssearch_products等。 每個元素都有一些常見的屬性,如ops_descriptionmodule等等。

另外一些對每個元素如prod_pricemanf_date

獨特屬性的我想有一個XML模式來驗證。一些屬性也是可選的。

我試着使用抽象和派生,但無法使其工作。

+0

您是否想爲給定的操作類型定義一組固定的子元素?例如:如果`type =「list_products」`然後``是必需的。 – Filburt 2011-02-18 15:17:31

回答

1

你想要達到的是不可能的xml模式。該定義指出,每個元素或屬性必須獨立進行驗證,而不參考其他元素或屬性。

你可以得到最好的解決辦法是group可選的,但相關的元素:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="overall_operation"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="operation" minOccurs="0" maxOccurs="unbounded"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="ops_description" /> 
          <xs:element name="module" minOccurs="1" maxOccurs="2" /> 
          <xs:element name="comp_name" /> 
          <xs:group ref="price_and_date" minOccurs="0" maxOccurs="1" /> 
          <xs:element name="pool_name" /> 
          <xs:element name="fail_retry" /> 
          <xs:element name="storage_type" /> 
          <xs:element name="storage_check" /> 
          <xs:element name="api_type" /> 
          <xs:element name="product_name" /> 
         </xs:sequence> 
         <xs:attribute name="type" type="xs:string" /> 
        </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <xs:group name="price_and_date"> 
     <xs:sequence> 
      <xs:element name="prod_price" /> 
      <xs:element name="manf_date" /> 
     </xs:sequence> 
    </xs:group> 
</xs:schema> 

使用minOccursmaxOccurs屬性來控制可選元素和組。

相關問題