xml – xsd唯一约束不起作用
我有一个根插入标记,一系列Inserts标记,每个标记都有一个“name”属性.
我无法获得在线验证器,以发现有重复的“名称”值. 我们一直在努力奋斗……天.谢谢你找到答案. XSD: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.osames.org/osamesorm" targetNamespace="http://www.osames.org/osamesorm" elementFormDefault="qualified"> <xs:element name="Inserts"> <xs:complexType> <xs:sequence> <xs:element name="Insert" maxOccurs="unbounded"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="name" type="xs:string" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> <xs:unique name="unique-isbn"> <xs:selector xpath="Inserts/Insert"/> <xs:field xpath="@name"/> </xs:unique> </xs:element> </xs:schema> XML: <?xml version="1.0" encoding="UTF-8"?> <Inserts xmlns="http://www.osames.org/osamesorm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osames.org/osamesorm ./xml_schemas/verysimple.xsd"> <Insert name="BaseInsert">INSERT INTO {0} ({1}) values ({2});</Insert> <Insert name="BaseInsert">INSERT INTO {0} ({1}) values ({2});</Insert> </Inserts> 解决方法
架构中存在两个问题:
第一个是您的选择器XPath不正确,基于您定义它的位置. < xs:unique>元素位于< Inserts>内元素,但你的XPath读取插入/插入,这意味着在< Inserts> element,another< Inserts>元素是预期的,并且只在其中< Insert>元件. < xs:unique>但是,约束已经在< Inserts>中了.元素,这就是为什么你只需要找到< Insert>元件: <xs:unique name="unique-isbn"> <xs:selector xpath="Insert"/> <xs:field xpath="@name"/> </xs:unique> 第二个问题是XPath没有使用xmlns属性在Xml中定义的默认命名空间的概念.您在XPath中引用的Insert元素不是XSD默认命名空间中的Insert元素,而是没有命名空间URI的Insert元素. 要处理这个问题,请为您的命名空间添加名称空间前缀(作为默认名称空间的替代方法)到XSD文件: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.osames.org/osamesorm" targetNamespace="http://www.osames.org/osamesorm" xmlns:o="http://www.osames.org/osamesorm" elementFormDefault="qualified"> 然后,在XPath中使用该命名空间前缀: <xs:unique name="unique-isbn"> <xs:selector xpath="o:Insert"/> <xs:field xpath="@name"/> </xs:unique> 随着这些变化,this validator输出
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |