gpt4 book ai didi

xml - xsd 唯一约束不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 02:01:06 25 4
gpt4 key购买 nike

我有一个根 Inserts 标签,一系列 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/Insert , 意思是在那里面 <Inserts>元素,另一个 <Inserts>元素是预期的,并且只有其中一个 <Insert>元素。

<xs:unique>但是,约束已经在 <Inserts> 内元素,这就是为什么你只需要找到 <Insert>元素:

<xs:unique name="unique-isbn">
<xs:selector xpath="Insert"/>
<xs:field xpath="@name"/>
</xs:unique>

第二个问题是 XPath 没有在 Xml 中用 xmlns 定义的默认命名空间的概念。属性。 Insert您在 XPath 中引用的元素不是 Insert来自 XSD 的默认命名空间的元素,而是一个 Insert没有命名空间 URI 的元素。

要解决这个问题,请为您的命名空间添加一个命名空间前缀(作为默认命名空间的替代)到您的 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输出

There is a duplicate key sequence 'BaseInsert' for the 'http://www.osames.org/osamesorm:unique-isbn' key or unique identity constraint. Line: 1 Column:357

关于xml - xsd 唯一约束不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22353302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com