0

我有一个如下所示的 xml 输入,并想为其创建一个 XML 架构。但是我创建的架构似乎有问题。是否可以向我展示如何为下面的 xml 创建架构。

-ownerName in ownerDetail 元素对农场元素中的farmOwnerName 的引用

<Country>
  <state>
     <farm farmOwnerName="Jerry" produce="apple">
       <established>1974</established>
       <totalWorker>30</totalWorker>
     </farm>
     <farm farmOwnerName="Ronald" produce="apple" quality="best">
       <established>1969</established>
       <totalWorker>15</totalWorker>
     </farm>
     <farm farmOwnerName="Richard" produce="lemon">
       <established>1970</established>
       <totalWorker>20</totalWorker>
     </farm>
  </state>
  <ownerDetail age="70" ownerName="Ronald">
     <bornIn>Australia</bornIn>
  </ownerDetail>
  <ownerDetail age="50" ownerName="Richard">
     <bornIn>Canada</bornIn>
  </ownerDetail>
</Country>

我编码的架构如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="state" type="xs:string"/>
<xs:element name="established" type="xs:integer"/>
<xs:element name="totalWorker" type="xs:integer"/>
<xs:element name="bornIn" type="xs:string"/>

<xs:element name="Country">

<xs:complexType name="farm">
    <xs:sequence maxOccurs="unbounded">
        <xs:element ref="established"/>
        <xs:element ref="totalWorker"/>
    </xs:sequence>
    <xs:attribute name="farmOwnerName"  use="required"/>
    <xs:attribute name="produce"  use="required"/>
    <xs:attribute name="quality"  use="optional">
        <xs:simpleType>
            <xs:restriction base="xs:token">
                <xs:enumeration value="best"/>
                <xs:enumeration value="normal"/>
                <xs:enumeration value="low"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>

<xs:complexType name="ownerDetail">
  <xs:sequence maxOccurs="unbounded">
      <xs:element ref="bornIn"/>
  </xs:sequence>
  <xs:attribute name="age" use="required"/>
      <xs:attribute name="ownerName" use="required"/>
</xs:complexType>

4

1 回答 1

0

XML Schema的给 xml 应该如下

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
  <xs:element name="Country">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="state">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="farm" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:short" name="established"/>
                    <xs:element type="xs:byte" name="totalWorker"/>
                  </xs:sequence>
                  <xs:attribute type="xs:string" name="farmOwnerName" use="optional"/>
                  <xs:attribute type="xs:string" name="produce" use="optional"/>
                  <xs:attribute type="xs:string" name="quality" use="optional"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="ownerDetail" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="bornIn"/>
            </xs:sequence>
            <xs:attribute type="xs:byte" name="age" use="optional"/>
            <xs:attribute type="xs:string" name="ownerName" use="optional"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
于 2012-11-17T16:50:44.877 回答