我已将user / url声明为主键。但我的验证器仍然接受相同的url值。我错过了什么?
还有其他一些指定主键约束的方法吗?或者我对xsd的理解不足?
由于
我的xml:
<?xml version="1.0"?>
<user_list xmlns="http://www.cse532.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.cse532.com schema.xsd">
<user>
<name>Bob</name>
<url>http://www.cse532.com/bob</url>
<work_history>
<company>Stony Brook</company>
<from_date>2013-08-01</from_date>
<to_date>2013-12-31</to_date>
</work_history>
<work_history>
<company>Microsoft</company>
<from_date>2014-01-01</from_date>
<to_date>2014-12-31</to_date>
</work_history>
<skills>
<skill>Programming</skill>
</skills>
<endorsements>
<url>http://www.cse532.com/alice</url>
<skill>Programming</skill>
</endorsements>
<endorsements>
<url>http://www.cse532.com/alice</url>
<skill>Instructor</skill>
</endorsements>
<endorsements>
<url>http://www.cse532.com/carol</url>
<skill>Programming</skill>
</endorsements>
</user>
<user>
<name>Bob</name>
<url>http://www.cse532.com/bob</url>
<work_history>
<company>Stony Brook</company>
<from_date>2013-08-01</from_date>
<to_date>2013-12-31</to_date>
</work_history>
<work_history>
<company>Microsoft</company>
<from_date>2014-01-01</from_date>
<to_date>2014-12-31</to_date>
</work_history>
<skills>
<skill>Programming</skill>
</skills>
<endorsements>
<url>http://www.cse532.com/alice</url>
<skill>Programming</skill>
</endorsements>
<endorsements>
<url>http://www.cse532.com/alice</url>
<skill>Instructor</skill>
</endorsements>
<endorsements>
<url>http://www.cse532.com/carol</url>
<skill>Programming</skill>
</endorsements>
</user>
我的xsd:
<!-- Definition of simple elements. -->
<xs:element name="name" type="xs:string"/>
<xs:element name="url" type="xs:string"/>
<xs:element name="company" type="xs:string"/>
<xs:element name="from_date" type="xs:string"/>
<xs:element name="to_date" type="xs:string"/>
<xs:element name="skill" type="xs:string"/>
<!-- Definition of complex elements. -->
<xs:element name="work_history">
<xs:complexType>
<xs:sequence>
<xs:element ref="company"/>
<xs:element ref="from_date"/>
<xs:element ref="to_date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="skills">
<xs:complexType>
<xs:sequence>
<xs:element ref="skill"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="endorsements">
<xs:complexType>
<xs:sequence>
<xs:element ref="url"/>
<xs:element ref="skill"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="user">
<xs:complexType>
<xs:sequence>
<xs:element ref="name" maxOccurs="1"/>
<xs:element ref="url"/>
<xs:element ref="work_history" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="skills" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="endorsements" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- "user_list" is the root element of this schema. -->
<xs:element name="user_list">
<xs:complexType>
<xs:sequence>
<xs:element ref="user" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!-- Primary key and foreign key definitions. -->
<xs:key name="PKuser_url">
<xs:selector xpath="user"/>
<xs:field xpath="url"/>
</xs:key>
<xs:key name="PKwork_history">
<xs:selector xpath="user/work_history"/>
<xs:field xpath="company"/>
<xs:field xpath="from_date"/>
</xs:key>
<xs:key name="PKskills">
<xs:selector xpath="user/skills"/>
<xs:field xpath="skill"/>
</xs:key>
<xs:key name="PKendorsements">
<xs:selector xpath="user/endorsements"/>
<xs:field xpath="url"/>
<xs:field xpath="skill"/>
</xs:key>
<xs:keyref name="FKurl" refer="PKuser_url">
<xs:selector xpath="user/endorsements"/>
<xs:field xpath="url"/>
</xs:keyref>
<xs:keyref name="FKskill" refer="PKskills">
<xs:selector xpath="user/endorsements"/>
<xs:field xpath="skill"/>
</xs:keyref>
</xs:element>
</xs:schema>
答案 0 :(得分:1)
您的实例文档有一个默认命名空间,因此约束中的XPath表达式(xpath =“user”,xpath =“url”)无效,因为它们选择了无命名空间中的元素。