我正在尝试使用xsd架构确定一个正确的方法来定义一个元素,该元素可能包含任何一组子元素,包括可能的重复子元素。
例如,给定以下XML文档:
<?xml version="1.0" encoding="utf-8" ?>
<EncounterDefinitions
xmlns="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Encounters.xsd">
<Encounter name="Sample Encounter">
<Task>
<Weapon/>
<Weapon/>
<Information/>
</Task>
<Task>
<Tool count="3"/>
<Ritual/>
</Task>
</Encounter>
</EncounterDefinitions>
到目前为止,我已经成功地推出了XSD。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Encounters"
targetNamespace="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Encounters.xsd"
elementFormDefault="qualified"
xmlns="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Encounters.xsd"
xmlns:mstns="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Encounters.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<!-- attribute types -->
<xs:attribute name="name" type="xs:string" />
<!-- complex types -->
<xs:element name="Task">
<xs:complexType>
<xs:sequence>
<!-- This type here should be any of a set of Elements -->
<xs:element name="Objective" type="Objective"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Encounter">
<xs:complexType>
<xs:sequence>
<xs:element ref="Task"/>
</xs:sequence>
<xs:attribute ref="name" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="EncounterDefinitions">
<xs:complexType>
<xs:sequence>
<xs:element ref="Encounter"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
答案 0 :(得分:1)
如果对孩子的顺序或每个孩子的出现次数没有限制,请使用重复选择:
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Objective" type="Objective"/>
<xs:element name="Weapon" type="Weapon"/>
<xs:element name="Information" type="Information"/>
</xs:sequence>
</xs:complexType>