XML Schema Choice

时间:2014-04-09 20:43:57

标签: xml xsd xsd-1.0

有什么方法可以从这个XML制作XML Schema 1.0?

<products>
         <product category="ABCD"> <!-- Mandatory element -->
                <name> PC </name> <!--  mandatory element -->
                <costs>-10000.00</costs> <!--mandatory element -->
                <price>110000.00</price> <!-- mandatory element -->
         </product>
         <product category="ABCDSetup">
                <description> Skladanie PC</description> <!-- mandatory element -->
                <price>10000.00</price>     <!-- Price of the service, either price or costs must be included -->
         </product>
</products>

我有这个但是错了,因为如果标记<name>出现,标记<costs><price>都必须出现

<xsd:sequence>
            <xsd:choice>
                    <xsd:element name="name">...</xsd:element>
                    <xsd:element name="description">...</xsd:element>   
            </xsd:choice>

            <xsd:choice minOccurs="1" maxOccurs="2">
                    <xsd:element name="price">...</xsd:element>
                    <xsd:element name="costs">...</xsd:element>
            </xsd:choice>
</xsd:sequence>

1 个答案:

答案 0 :(得分:1)

是的,可以使用嵌套的选择节点。

第一个选择是名称,成本和价

第二种选择是描述,可选择成本或价格。

<?xml version="1.0" encoding="utf-16"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="products">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="product">
          <xs:complexType>
            <xs:sequence>
              <xs:choice>
                <xs:sequence>
                  <xs:element name="name" type="xs:string" />
                  <xs:element name="costs" type="xs:decimal" />
                  <xs:element name="price" type="xs:decimal" />
                </xs:sequence>
                <xs:sequence>
                  <xs:element name="description" type="xs:string" />
                  <xs:choice>
                    <xs:element name="costs" type="xs:decimal" />
                    <xs:element name="price" type="xs:decimal" />
                  </xs:choice>
                </xs:sequence>
              </xs:choice>
            </xs:sequence>
            <xs:attribute name="category" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>