xml - 如何在 XML 架构中指定唯一值

标签 xml xpath xsd

我无法让 xs:unique 说明符在 XML 文件中工作。我似乎无法计算出有效的 XPath。对于这个问题中的代码量,我深表歉意,但我将非常感谢任何能在下面指出我做错了什么的人。无论我做什么,我都无法获取元素中的@ref 属性来报告我复制值的错误(每个 ref 必须是唯一的)。

如能提供任何帮助或信息提示,我们将不胜感激。

祝你好运

这是我的模式:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Artworks"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:aw="http://www.fourthwish.co.uk/data/Artworks.xsd"
xmlns="http://www.fourthwish.co.uk/data/Artworks.xsd"
targetNamespace="http://www.fourthwish.co.uk/data/Artworks.xsd"
elementFormDefault="qualified"
>
<xs:element name="artworks">
    <xs:complexType>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="artwork" type="ArtworkType">
                <xs:unique name="uniqueRef">
                    <xs:selector xpath="artwork"/>
                    <xs:field xpath="@ref"/>
                </xs:unique>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:complexType name="ArtworkType">
    <xs:sequence>
        <xs:element name="title" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="ref" type="xs:nonNegativeInteger"/>
</xs:complexType>
</xs:schema>

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<artworks
xmlns="http://www.fourthwish.co.uk/data/Artworks.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.fourthwish.co.uk/data/Artworks.xsd Artworks.xsd"
>
<artwork ref="1">
    <title>Title String</title>
</artwork>
<artwork ref="1">
    <title>Title String</title>
</artwork>
</artworks>

为什么我没有收到重复引用值的错误消息?啊啊啊!我已经阅读了互联网上的所有内容。请帮助某人。

最佳答案

使用这个:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Artworks"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:aw="http://www.fourthwish.co.uk/data/Artworks.xsd"
xmlns="http://www.fourthwish.co.uk/data/Artworks.xsd"
targetNamespace="http://www.fourthwish.co.uk/data/Artworks.xsd"
elementFormDefault="qualified"
>
  <xs:element name="artworks">
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="artwork" type="ArtworkType"/>
      </xs:sequence>
    </xs:complexType>

    <xs:unique name="uniqueRef">
      <xs:selector xpath="aw:artwork"/>
      <xs:field xpath="@ref"/>
    </xs:unique>

  </xs:element>

  <xs:complexType name="ArtworkType">
    <xs:sequence>
      <xs:element name="title" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="ref" type="xs:nonNegativeInteger"/>
  </xs:complexType>
</xs:schema>

关于xml - 如何在 XML 架构中指定唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7835600/

相关文章:

android - 这是我第一次使用 android studio 并出现错误?不知道好像哪里出了问题?

java - 撒克逊 XSLT。扩展函数只调用一次

xml - 如何使用 xml starlet 选择 xml 值

xml - 你总是必须有一个带有 xml/xsd 的根节点吗?

xsd - 出现错误 无法加载架构 abc.xsd。 IllegalArgumentException : The resource path def. xsd 已规范化为无效的 null

ios - 使用self调用方法会导致错误

xml - 如何使用 XSLT 根据节点属性拆分 XML 文件?

ruby - 谁能帮我使用 Ruby 抓取亚马逊产品 API?

php - 如何从 XML 标签中取出元素存储到 PHP 数组中?

xml - 如何将简单的重复数据从 excel 导出为 xml?