gpt4 book ai didi

xml - XML 模式中的唯一约束

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

假设我有以下 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>

我想要的是做一个约束,不允许两个相同的 author 值,但上面的那个不能那样工作。我做错了什么?

最佳答案

选择器 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>

关于xml - XML 模式中的唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7717515/

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