我无法使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>