假设我有以下XML,我应该如何更新我的XSD架构(也在下面描述)以防止它的验证?
基本上,对于问题#3,它看起来像是一个选择题,因为标签中只有一个答案。但是标签内部必须至少有一个标签,否则没有其他问题可供选择但是正确的。
<?xml version="1.0" encoding="utf-8" ?>
<quiz>
<question><!-- single choice question -->
<text>Question 1</text>
<answers>
<answer>Answer 1</answer>
<answer>Answer 2</answer>
<answer>Answer 4</answer>
<correct>
<answer>Answer 3</answer>
</correct>
</answers>
</question>
<question><!-- multiple choice question -->
<text>Question 2</text>
<answers>
<answer>Answer 1</answer>
<answer>Answer 3</answer>
<correct>
<answer>Answer 2</answer>
<answer>Answer 4</answer>
</correct>
</answers>
</question>
<question><!-- doesn't make sense. help me prevent this validation -->
<text>Question 3</text>
<answers>
<correct>
<answer>Some answer</answer>
</correct>
</answers>
</question>
<question><!-- all answers are correct -->
<text>Question 4</text>
<answers>
<correct>
<answer>Answer 1</answer>
<answer>Answer 2</answer>
<answer>Answer 3</answer>
<answer>Answer 4</answer>
</correct>
</answers>
</question>
</quiz>
这是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="quiz">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="question">
<xs:complexType>
<xs:sequence>
<xs:element name="text" type="xs:string" />
<xs:element name="answers">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded"
name="answer" type="xs:string" />
<xs:element name="correct" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="answer"
type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
答案 0 :(得分:1)
answer
元素(\question\answers\answer
)的定义应该有minOccurs = 1.你最初有这个:
<xs:element minOccurs="0" maxOccurs="unbounded" name="answer" type="xs:string" />
将其更改为此,您将获得正在寻找的验证:
<xs:element minOccurs="1" maxOccurs="unbounded" name="answer" type="xs:string" />
但是,如果您希望验证correct
序列在answer
列出的answer
元素中没有列出answers
多个{{1}},则会{序列 - 你运气不好。您不能使用XML模式进行这种类型的验证。