使用 XSD 进行复杂的 XML 验证
假设我有以下 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>
Suppose I have the following XML, how should I update my XSD schema (described below as well) to prevent it's validation?
Basically, for question #3, it looks like its a single choice question as there is only one answer in the tag. But then there must be at least one tag inside the tag otherwise there is no other question available to select but the correct one.
<?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>
Here is the 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
answer
元素 (\question\answers\answer
) 的定义应具有 minOccurs = 1。您最初是这样的:将其更改为这样,您将得到您正在寻找的验证:
但是,如果您希望验证
正确
序列是否在没有“不正确”的情况下列出了多个
元素 - 你运气不好。您无法使用 XML 模式执行此类验证。答案
answers
序列中列出的answerThe definition of the
answer
element (\question\answers\answer
), should have minOccurs = 1. You originally had this:Change it to this and you'll get the validation you're looking for:
However, if you're looking to validate that the
correct
sequence has more than oneanswer
listed when there are no "incorrect"answer
elements listed in theanswers
sequence - you're out of luck. You cannot do that type of validation using XML schemas.