XML Schema中的唯一约束
发布时间:2020-12-16 07:40:55 所属栏目:百科 来源:网络整理
导读:假设我有以下 XML文件: authors authora1/author authora2/author lastmodified2010/lastmodified/authors 和XML模式片段: xs:element name="authors" maxOccurs="1" xs:complexType xs:sequence xs:element name="author" maxOccurs="unbounded" type="xs
假设我有以下
XML文件:
<authors> <author>a1</author> <author>a2</author> <lastmodified>2010</lastmodified> </authors> 和XML模式片段: <xs:element name="authors" maxOccurs="1"> <xs:complexType> <xs:sequence> <xs:element name="author" maxOccurs="unbounded" type="xs:string"> </xs:element> <xs:element name="lastmodified" type="xs:date" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:unique name="uniqueAuthor"> <xs:selector xpath="."/> <xs:field xpath="author"/> </xs:unique> </xs:element> 我想要的是制定一个不允许两个相同的作者值的约束,但上面的一个不会这样工作.我究竟做错了什么?
选择器XPath选择必须是唯一的节点(在这种情况下,它应该选择作者节点).字段XPath选择“使其独特”(在这种情况下,使用)将导致其类型值,在这种情况下,标签之间的文本被视为字符串,被使用).文件
<?xml version="1.0" encoding="UTF-8"?> <authors> <author>a1</author> <author>a2</author> <lastmodified>2010-01-01</lastmodified> </authors> 应该对以下模式有效: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="authors"> <xs:complexType> <xs:sequence> <xs:element name="author" maxOccurs="unbounded" type="xs:string"/> <xs:element name="lastmodified" type="xs:date" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:unique name="uniqueAuthor"> <xs:selector xpath="author"/> <xs:field xpath="."/> </xs:unique> </xs:element> </xs:schema> 而这个不应该: <?xml version="1.0" encoding="UTF-8"?> <authors> <author>a1</author> <author>a1</author> <lastmodified>2010-01-01</lastmodified> </authors> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |