1

使用不同的元素验证两种 XML XSD 类型。

产品的这个 XML。

<oils hostDbID="6" hostDetailId="289" model="1l dans 5w30 c3"
  host="norauto" trademark="oil-dans" id="noil-237789" 
  product-type="oils">
  <url>http://www.oils.es/product/_289.html</url>
  <img>http://medias.es/images_prod/oil_5_w_30_c3_1l.jpg</img>
  <price>11.95</price>
  <currency>€&lt;/currency>
  <manufacture>autoOil</manufacture>
  <viscosity>5W30</viscosity>
  <liters>1 L</liters>
  <composition>sint</composition>
  <norm type="Acea">C2</norm>
  <norm type="API">SM/CF</norm>
  <norm type="VW">505.00</norm>
  <norm type="Mercedes-Benz">229.31</norm>
  <norm type="BMW">LONGLIFE 04</norm>
  <content>
    Norma C3 A3/B3/B4 LL.
  </content>
</oils>

这是 XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="tire" type="tire" />
  <xs:element name="oils" type="oil" />

  <xs:complexType name="product">
    <xs:sequence>
      <xs:element type="xs:anyURI" name="url" />
      <xs:element type="xs:double" name="price" />
      <xs:element type="xs:float" name="ecorate" 
        minOccurs="0" maxOccurs="unbounded" />
      <xs:element type="xs:string" name="currency" />
      <xs:element type="xs:boolean" name="vat" />
      <xs:element type="xs:anyURI" name="img" />
      <xs:element type="xs:string" name="content" />
      <xs:element name="specs" maxOccurs="unbounded"
        minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="spec" maxOccurs="unbounded"
              minOccurs="0">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:string">
                    <xs:attribute type="xs:string" name="name"
                      use="optional" />
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="features" maxOccurs="unbounded"
        minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="feature" maxOccurs="unbounded"
              minOccurs="0">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:string">
                    <xs:attribute type="xs:string" name="name"
                      use="optional" />
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute type="xs:int" name="hostDbID" />
    <xs:attribute type="xs:string" name="hostDetailId" />
    <xs:attribute type="xs:string" name="host" />
    <xs:attribute type="xs:string" name="id" />
    <xs:attribute type="xs:string" name="product-type" />
  </xs:complexType>

  <xs:complexType name="tire">
    <xs:complexContent>
      <xs:extension base="product">
        <xs:attribute type="xs:string" name="speed" />
        <xs:attribute type="xs:int" name="load" />
        <xs:attribute type="xs:int" name="wheels" />
        <xs:attribute type="xs:int" name="height" />
        <xs:attribute type="xs:int" name="width" />
        <xs:attribute type="xs:int" name="season" />
        <xs:attribute type="xs:string" name="model" />
        <xs:attribute type="xs:string" name="trademark" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="oil">
    <xs:complexContent>
      <xs:extension base="product">
      <xs:attribute type="xs:string" name="model"/>
      <xs:attribute type="xs:string" name="trademark"/>
      </xs:extension>
    </xs:complexContent>

    <xs:sequence>
      <xs:element type="xs:string" name="manufacture"/>
      <xs:element type="xs:string" name="viscosity"/>
      <xs:element type="xs:string" name="liters"/>
      <xs:element type="xs:string" name="composition"/>
      <xs:element name="norm" 
          maxOccurs="unbounded" minOccurs="0">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute type="xs:string" 
                name="type" use="optional"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
        </xs:element>
      </xs:sequence>

  </xs:complexType>
<xs:element name="products">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="tire" maxOccurs="unbounded"
        minOccurs="0" type="tire" />
      <xs:element name="oils" maxOccurs="unbounded"
        minOccurs="0" type="oil" />
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

问题是

    <xs:sequence>
      <xs:element type="xs:string" name="manufacture"/>
      <xs:element type="xs:string" name="viscosity"/>
      <xs:element type="xs:string" name="liters"/>
      <xs:element type="xs:string" name="composition"/>
      <xs:element name="norm" maxOccurs="unbounded" 
          minOccurs="0">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute type="xs:string" 
                name="type" use="optional"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
        </xs:element>
      </xs:sequence>

我用过xmllint。我得到的错误是这样的:

Element '{http://www.w3.org/2001/XMLSchema}element': The content is not valid. 

“油”的 complexType 中元素的内容不正确,没有有效的元素。如何在产品油中添加更多项目的扩展?谢谢。

解决方案:

<xs:complexType name="fullOil">
  <xs:complexContent>
    <xs:extension base="oil">
      <xs:sequence>
        <xs:element type="xs:string" 
                    name="manufacture"/>
        <xs:element type="xs:string" name="viscosity"/>
        <xs:element type="xs:string" name="liters"/>
        <xs:element type="xs:string" name="composition"/>
        <xs:element name="norm" 
                    maxOccurs="unbounded" 
                    minOccurs="0">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:string" 
                              name="type" 
                              use="optional"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
4

0 回答 0