使用 XSD 进行复杂的 XML 验证

发布于 2024-10-18 14:15:25 字数 3141 浏览 7 评论 0原文

假设我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤云独去闲 2024-10-25 14:15:25

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" />

但是,如果您希望验证正确序列是否在没有“不正确”的情况下列出了多个答案 answers 序列中列出的answer 元素 - 你运气不好。您无法使用 XML 模式执行此类验证。

The definition of the answer element (\question\answers\answer), should have minOccurs = 1. You originally had this:

<xs:element minOccurs="0" maxOccurs="unbounded" name="answer" type="xs:string" />

Change it to this and you'll get the validation you're looking for:

<xs:element minOccurs="1" maxOccurs="unbounded" name="answer" type="xs:string" />

However, if you're looking to validate that the correct sequence has more than one answer listed when there are no "incorrect" answer elements listed in the answers sequence - you're out of luck. You cannot do that type of validation using XML schemas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文