无法以正确的方式在XML模式中使用键引用引用

时间:2013-10-31 17:20:54

标签: xml xpath xsd

我有一个xml结构:

<Quiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="file:/C:/Users/Snedden27/Documents/RIT/Knowledge%20representation/xml/schema/Quizzer%20Project/quiz.xsd" QID="q1" CourseID="IST 01"> //here QID is the ID of the quiz
    <QuizTaker RITeMAIL="" Name="Type in your Name here"/>
    <Instruction>Thisis nicequiz</Instruction>
    <Questions Objective="Yes" TotalMarks="10">
        <Question QNumber="1" QIDref="q1">         //I am trying to reference QID here
            <Q>WHat is your name?</Q> 
            <Option OptionNum="1" OptionValue="Leon"/>
            <Option OptionNum="2" OptionValue="Snedden"/>
            <Answer>1</Answer>
        </Question>
        <Question QIDref="q1" QNumber="2" >      //I am trying to reference QID here i.e ID of quiz
            <Q>Whats your dog name?</Q>
            <Answer></Answer>
        </Question>
    </Questions>
</Quiz>

但由于某种原因,我收到一个错误:不是关键qKEy的值,这是我的架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"   >
    <xs:element name="Quiz">

        <xs:complexType>
            <xs:sequence>
                <xs:element  name="QuizTaker">
                    <xs:complexType>
                        <xs:attributeGroup ref="student_Att_Group"></xs:attributeGroup>        
                    </xs:complexType>
                </xs:element>
                <xs:element name="Instruction" type="xs:string" />
                <xs:element name="Questions" >
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Question" minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="Q" type="xs:string"></xs:element>
                                        <xs:element name="Option"  minOccurs="0" maxOccurs="unbounded">
                                           <xs:complexType>

                                                <xs:attribute name="OptionNum" use="required" type="xs:nonNegativeInteger" ></xs:attribute>
                                                <xs:attribute name="OptionValue" use="required"  type="xs:string"></xs:attribute>

                                            </xs:complexType>
                                        </xs:element>
                                        <xs:element name="Answer" type="xs:string"></xs:element>
                                    </xs:sequence>
                                    <xs:attributeGroup ref="Q_details_Att_Group"></xs:attributeGroup>
                                </xs:complexType>

                            </xs:element>    
                        </xs:sequence>
                        <xs:attributeGroup ref="question_Att_Group"></xs:attributeGroup>
                        <xs:attribute name="TotalMarks" use="required" type="xs:nonNegativeInteger"></xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>



           <xs:attributeGroup ref="Quiz_details_Att_Group" />

        </xs:complexType>

        <xs:unique name="optionId"> 
            <xs:selector xpath="./Questions/Question/Option"/> 
            <xs:field xpath="@OptionNum"/> 
        </xs:unique> 

       <!----------------- gave the entire schema but this is what matters I guess,so highlighting---------------------------->
        <xs:key name="QId">
            <xs:selector xpath="." />
            <xs:field xpath="@QID" />
        </xs:key>

        <xs:key name="QKey">
            <xs:selector xpath="./Questions/Question"/>
            <xs:field xpath="@Qno"/>
            <xs:field xpath="@QIDref"/>
        </xs:key>



        <xs:keyref name="QKeyRef" refer="QId">
            <xs:selector xpath="./Questions/Question"/>
            <xs:field xpath="@QIDref" />
        </xs:keyref>



    </xs:element>


    <xs:attributeGroup name="Q_details_Att_Group">
        <xs:attribute name="QNumber" use="required" ></xs:attribute>
        <xs:attribute  name="QIDref"  use="required"></xs:attribute>
    </xs:attributeGroup>



    <xs:attributeGroup name="Quiz_details_Att_Group">
        <xs:attribute name="QID" use="required" type="xs:ID" ></xs:attribute>
        <xs:attribute name="CourseID" use="required"></xs:attribute>
    </xs:attributeGroup>

    <xs:attributeGroup name="student_Att_Group">
        <xs:attribute name="Name" use="optional" default="Type in your Name here"></xs:attribute>
        <xs:attribute name="RITeMAIL" use="required" ></xs:attribute>
    </xs:attributeGroup>


    <xs:attributeGroup name="question_Att_Group">
        <xs:attribute name="Objective" use="required" >
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="Yes"></xs:enumeration>
                    <xs:enumeration value="No"></xs:enumeration>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="Marks" type="xs:nonNegativeInteger"/>
    </xs:attributeGroup>
</xs:schema>

1 个答案:

答案 0 :(得分:0)

正如Marcus Rickert指出你需要在下面将@Qno更改为@QNumber

    <xs:key name="QKey">
        <xs:selector xpath="./Questions/Question"/>
        <xs:field xpath="@Qno"/>
        <xs:field xpath="@QIDref"/>
    </xs:key>

所以看起来这个

<xs:key name="QKey">
  <xs:selector xpath="./Questions/Question"/>
  <xs:field xpath="@QNumber"/>
  <xs:field xpath="@QIDref"/>
</xs:key>