我要创建包含以下内容的XML架构:
<xs:complexType name="Record">
<!--required elements-->
<xs:element name="RecordTag" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="RecordSize" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="RecordSection" type="xs:string" minOccurs="1" maxOccurs="1" />
<!--optional elements-->
<xs:element name="RecordName" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="RecordType" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="RecordValue" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="RecordDefault" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="RecordComment" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="RecordURL" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="Condition" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="Master" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="RecordCurrent" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="RecordId" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:complexType>
从评论中可以看出,我希望前三个元素是必需的,其余的是可选的。架构应该允许元素以任何顺序出现。
现在,如果我使用
<xs:sequence>
指示符,则命令将被强制执行,这是我不希望的。如果我使用
<xs:all>
指示符,那么模式要求显示所有元素,即使minOccurs
值设置为0
。有没有其他指标可以用来完成我的任务?
谢谢!
最佳答案
为了便于说明,让我们假设下面的xsd;它与post中提供的xsd没有什么不同,只是语法正确(通过引入xs:all合成器)和为了轻松创建示例xml,我还添加了一个dummyRecord
元素。
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Record" type="Record"/>
<xs:complexType name="Record">
<xs:all>
<!--required elements-->
<xs:element name="RecordTag" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="RecordSize" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="RecordSection" type="xs:string" minOccurs="1" maxOccurs="1"/>
<!--optional elements-->
<xs:element name="RecordName" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="RecordType" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="RecordValue" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="RecordDefault" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="RecordComment" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="RecordURL" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Condition" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Master" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="RecordCurrent" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="RecordId" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:all>
</xs:complexType>
</xs:schema>
具有此示例XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RecordId>RecordId1</RecordId>
<RecordCurrent>RecordCurrent1</RecordCurrent>
<Master>Master1</Master>
<Condition>Condition1</Condition>
<RecordURL>RecordURL1</RecordURL>
<RecordSection>RecordSection1</RecordSection>
<RecordSize>RecordSize1</RecordSize>
<!--
<RecordTag>RecordTag1</RecordTag>
-->
</Record>
您将收到此错误消息(使用.NET时):
元素“record”的内容不完整。可能元素列表
应为:“recordTag”。
薛西斯会说:
cvc complex type.2.4.b:元素“record”的内容不是
完成。需要{recordtag}之一。
以上所述只是一个实际的论点,即问题的原陈述如下所述是错误的。
如果我使用指示符,那么模式需要
要显示的元素,即使minOccurs值设置为0。
这里提供的ask在xsd 1.0中完全可以通过
all
合成器实现。规范中没有任何内容可以说明其他情况;我冒昧地猜测,可能是在支持xsd的xml处理器中实现了错误。关于xml - 如何创建需要某些元素,允许其他元素并且与订单无关的架构?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3294740/