加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在XML模式中指定唯一值

发布时间:2020-12-16 23:11:01 所属栏目:百科 来源:网络整理
导读:我无法让xs:unique说明符在 XML文件中工作.我似乎无法找到一个有效的XPath.我对这个问题中的代码数量表示道歉,但我非常感谢任何可以在下面指出我做错的人.无论我做什么,我都无法在元素中获取@ref属性来报告错误,因为我复制了值(每个ref必须是唯一的). 非常
我无法让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>

为什么我的重复参考值没有出错? Arrrggghhh!我已经阅读了互联网上的所有内容.请帮助别人.

解决方法

用这个:

<?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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读