什么xsd会让一个元素无限地成为一个子元素?

时间:2010-05-21 20:56:35

标签: xml xsd

如何创建一个xsd来为我提供这种可以无限延续的xml结构?

<?xml version="1.0" encoding="utf-8" ?>
<SampleXml>
    <Items>
        <Item name="SomeName" type="string">
            This would be the value
        </Item>
        <Item name="SecondName" type="string">
            This is the next string
        </Item>
        <Item name="AnotherName" type="list">
            <Items>
                <Item name="SubName" type="string">
                    A string in a sub list
                </Item>
                <Item name="SubSubName" type="list">
                    <Items>
                        <Item name="HowDoI" type="string">
                            How do I keep this going infinately?
                        </Item>
                    </Items>
                </Item>
            </Items>
        </Item>
    </Items>
</SampleXml>

我发现的唯一解决方案是在xsd中重复多次,因为我愿意复制。如下所示。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="SampleXml">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Items">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element maxOccurs="unbounded" name="Item">
                                <xs:complexType mixed="true">
                                    <xs:sequence minOccurs="0">
                                        <xs:element name="Items">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element maxOccurs="unbounded" name="Item">
                                                        <xs:complexType mixed="true">
                                                            <xs:sequence minOccurs="0">
                                                                <xs:element name="Items">
                                                                    <xs:complexType>
                                                                        <xs:sequence>
                                                                            <xs:element name="Item">
                                                                                <xs:complexType>
                                                                                    <xs:simpleContent>
                                                                                        <xs:extension base="xs:string">
                                                                                            <xs:attribute name="name" type="xs:string" use="required" />
                                                                                            <xs:attribute name="type" type="xs:string" use="required" />
                                                                                        </xs:extension>
                                                                                    </xs:simpleContent>
                                                                                </xs:complexType>
                                                                            </xs:element>
                                                                        </xs:sequence>
                                                                    </xs:complexType>
                                                                </xs:element>
                                                            </xs:sequence>
                                                            <xs:attribute name="name" type="xs:string" use="required" />
                                                            <xs:attribute name="type" type="xs:string" use="required" />
                                                        </xs:complexType>
                                                    </xs:element>
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                    <xs:attribute name="name" type="xs:string" use="required" />
                                    <xs:attribute name="type" type="xs:string" use="required" />
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

3 个答案:

答案 0 :(得分:12)

使用<xs:element ref="bla" />递归引用元素。 一个简单的例子:

<xs:element name="recursive">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="recursive" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

允许你写这个:

<recursive>
  <recursive>
    <recursive />
  </recursive>
  <recursive />
  <recursive>
    <recursive>
      <recursive />
    </recursive>
  </recursive>
</recursive>

使用'ref'属性也有助于提高XSD的可读性。这就是我写你的方式:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="SampleXml">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Items" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="Items">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Item" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="Item">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:element ref="Items" minOccurs="0" />
      </xs:sequence>
      <xs:attribute name="name" type="xs:string" use="required" />
      <xs:attribute name="type" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>

</xs:schema>

注意使用'ref'即使不是非常必要(例如当SampleXml引用Items时),也会使XSD不再是嵌套混乱并且更具可读性。

答案 1 :(得分:4)

使用ref属性尝试这样的事情:

<xs:element name="Items">
  <xs:complexType>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element ref="Item" />
    </xs:sequence> 
  </xd:complexType
 </xd:element>

<xs:element name="Item">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="Items" />
    </xs:sequence> 
  </xd:complexType
 </xd:element>

答案 2 :(得分:4)

尝试使用ref属性:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Items">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="Item">
                    <xs:complexType mixed="true">
                        <xs:sequence minOccurs="0">
                            <xs:element ref="Items" />
                        </xs:sequence>
                        <xs:attribute name="name" type="xs:string" use="required" />
                        <xs:attribute name="type" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="SampleXml">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Items" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>