具有多个字段的XML Schema Key

时间:2009-08-28 18:59:41

标签: xml schema xsd

我有一个包含问题结构的XML文件格式:

<question id="q101">
  <text>Do you like the color red?</text>
  <answer>yes</answer>
  <answer>no</answer>
</question>
<question id="q102">
  <text>What is your favorite color?</text>
  <answer>red</answer>
  <answer>blue</answer>
  <answer>white</answer>
  <answer>yellow</answer>
</question>

我也有来自多个用户的相同文件回复。

<user id="bob">
  <response questionIdRef="q101">yes</response>
  <response questionIdRef="q102">white</response>
</user>
<user id="jane">
  <response questionIdRef="q101">no</response>
  <response questionIdRef="q102">blue</response>
</user>

我已经在xml中为questionId:

定义了一个键和一个keyref元素
<xsd:key name="questionId">
  <xsd:selector xpath=".//question" />
  <xsd:field xpath="@id" />
</xsd:key>
<xsd:keyref name="responseQuestionIdKeyRef" refer="questionId">
  <xsd:selector xpath=".//response" />
  <xsd:field xpath="@questionIdRef" />
</xsd:keyref>

我现在想要做的是现在让模式验证用户对某个问题的响应的值实际上是引用问题中提供的答案。我尝试使用以下密钥和keyref执行此操作,但它只能识别第一个答案,所有其他答案都未被识别为有效:

<xsd:key name="answerValue">
  <xsd:selector xpath=".//question" />
  <xsd:field xpath="@id" />
  <xsd:field xpath=".//answer/value" />
</xsd:key>
<xsd:keyref name="validAnswer" refer="answerValue">
  <xsd:selector xpath=".//response" />
  <xsd:field xpath="@questionIdRef" />
  <xsd:field xpath="." />
</xsd:keyref>

我得到的确切错误是:

  

字段'answer'最多只能有一个值。

我应该注意到我正在使用C#XML验证器。

为了完整性,下面是我所指的完整模式和xml实例: 架构:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="survey">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="user" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="response" maxOccurs="unbounded">
                <xsd:complexType>
                  <xsd:simpleContent>
                    <xsd:extension base="xsd:string">
                      <xsd:attribute name="questionIdRef" type="xsd:string" use="required" />
                    </xsd:extension>
                  </xsd:simpleContent>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="id" type="xsd:string" use="required" />
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="question" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="text" type="xsd:string" />
              <xsd:element name="answer" maxOccurs="unbounded" type="xsd:string"/>
            </xsd:sequence>
            <xsd:attribute name="id" type="xsd:string" use="required" />
          </xsd:complexType>
          <xsd:unique name="uniqueAnswer">
            <xsd:selector xpath=".//answer" />
            <xsd:field xpath="@value" />
          </xsd:unique>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
    <!--

    <xsd:key name="questionId">
      <xsd:selector xpath=".//question" />
      <xsd:field xpath="@id" />
    </xsd:key>
    <xsd:keyref name="responseQuestionIdKeyRef" refer="questionId">
      <xsd:selector xpath=".//response" />
      <xsd:field xpath="@questionIdRef" />
    </xsd:keyref>

    -->
    <xsd:key name="answerValue">
      <xsd:selector xpath=".//question" />
      <xsd:field xpath="@id" />
      <xsd:field xpath=".//answer" />
    </xsd:key>
    <xsd:keyref name="validAnswer" refer="answerValue">
      <xsd:selector xpath=".//response" />
      <xsd:field xpath="@questionIdRef" />
      <xsd:field xpath="." />
    </xsd:keyref>
    <xsd:unique name="uniqueUserId">
      <xsd:selector xpath=".//user" />
      <xsd:field xpath="@id" />
    </xsd:unique>
  </xsd:element>
</xsd:schema>

示例XML实例:

<?xml version="1.0" encoding="utf-8"?>
<survey>
  <user id="bob">
    <response questionIdRef="q101">yes</response>
    <response questionIdRef="q102">white</response>
  </user>
  <user id="jane">
    <response questionIdRef="q101">no</response>
    <response questionIdRef="q102">blue</response>
  </user>
  <question id="q101">
    <text>Do you like the color red?</text>
    <answer>yes</answer>
    <answer>no</answer>
  </question>
  <question id="q102">
    <text>What is your favorite color?</text>
    <answer>red</answer>
    <answer>blue</answer>
    <answer>white</answer>
    <answer>yellow</answer>
  </question>
</survey>

2 个答案:

答案 0 :(得分:6)

好吧,我发现这个问题(XSD key/keyref: hierarchical key structure)与我的问题有关。这个问题也没有一个可以接受的答案。

这似乎是XML Schema技术的限制。我决定重新排序Schema,将用户的回答置于问题之下。然后,响应将引用用户。

以下是示例XML实例:

<?xml version="1.0" encoding="utf-8"?>
<survey>
  <user id="bob" />
  <user id="jane" />
  <question id="q101">
    <text>Do you like the color red?</text>
    <answer>yes</answer>
    <answer>no</answer>
    <response userIdRef="bob">yes</response>
    <response userIdRef="jane">no</response>
  </question>
  <question id="q102">
    <text>What is your favorite color?</text>
    <answer>red</answer>
    <answer>blue</answer>
    <answer>white</answer>
    <answer>yellow</answer>
    <response userIdRef="bob">white</response>
    <response userIdRef="jane">blue</response>
  </question>
</survey>

这是我使用的架构:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="survey">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="user" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:attribute name="id" type="xsd:string" use="required" />
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="question" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="text" type="xsd:string" />
              <xsd:element name="answer" maxOccurs="unbounded" type="xsd:string"/>
              <xsd:element name="response" maxOccurs="unbounded">
                <xsd:complexType>
                  <xsd:simpleContent>
                    <xsd:extension base="xsd:string">
                      <xsd:attribute name="userIdRef" type="xsd:string" use="required" />
                    </xsd:extension>
                  </xsd:simpleContent>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="id" type="xsd:string" use="required" />
          </xsd:complexType>
          <xsd:key name="validAnswerKey">
            <xsd:selector xpath=".//answer" />
            <xsd:field xpath="." />
          </xsd:key>
          <xsd:keyref name="responseValidAnswerKeyRef" refer="validAnswerKey">
            <xsd:selector xpath=".//response" />
            <xsd:field xpath="." />
          </xsd:keyref>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:key name="userIdKey">
      <xsd:selector xpath=".//user" />
      <xsd:field xpath="@id" />
    </xsd:key>
    <xsd:keyref name="userResponse" refer="userIdKey">
      <xsd:selector xpath=".//response" />
      <xsd:field xpath="@userIdRef" />
    </xsd:keyref>
  </xsd:element>
</xsd:schema>

答案 1 :(得分:0)

在您的架构中,使得answer元素受到枚举的限制,因此在您的架构中它看起来更像是:

<xsd:simpleType name="answer">
    <xsd:restriction>
        <xsd:enumeration>red</xsd:enumeration>
        <xsd:enumeration>blue</xsd:enumeration>
        <xsd:enumeration>green</xsd:enumeration>
        <xsd:enumeration>yellow</xsd:enumeration>
    </xsd:restriction>
</xsd:simpleType>

枚举限制是给定元素的一组可接受的值。