2011-02-18 71 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属性来控制可选元素和组。

相关问题