我遇到架构验证问题,这是我的 xml
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='favorites.xsd'>
<Favorites>
<Folder>
<Name>Entertainment</Name>
<Bookmarks>
<Bookmark ID="1"/>
<Bookmark ID="200"/> <!-- This should fail validation -->
</Bookmarks>
</Folder>
</Favorites>
<Bookmarks>
<Bookmark>
<ID>1</ID>
<URL>www.website1.com</URL>
</Bookmark>
<Bookmark>
<ID>2</ID>
<URL>www.website2.com</URL>
</Bookmark>
<Bookmark>
<ID>3</ID>
<URL>www.website3.com</URL>
</Bookmark>
</Bookmarks>
</root>
Bookmark 中的 ID 元素应该是键,Favorites/Folder/Bookmarks/Bookmark ID 属性应该总是引用一个 Bookmark ID。
这是我的xsd:
<?xml version="1.0"?>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="Favorites" type="Favorites"/>
<xs:element name="Bookmarks" type="Bookmarks"/>
</xs:sequence>
</xs:complexType>
<xs:keyref name="bookmarkIDKeyRef" refer="bookmarkIDKey">
<xs:selector xpath="root/Favorites/Folder/Bookmarks/*"/>
<xs:field xpath="@ID"/>
</xs:keyref>
<xs:key name="bookmarkIDKey">
<xs:selector xpath="root/Bookmarks/Bookmark/ID"/>
<xs:field xpath="ID"/>
</xs:key>
</xs:element>
<xs:complexType name="Favorites">
<xs:sequence maxOccurs="unbounded">
<xs:element name="Folder" type="Folder"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Folder">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Bookmarks">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Bookmark">
<xs:complexType>
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Bookmarks">
<xs:sequence maxOccurs ="unbounded">
<xs:element name="Bookmark" type="Bookmark"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Bookmark">
<xs:sequence>
<xs:element name="ID" type="xs:string"/>
<xs:element name="URL" type ="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
xml 格式正确,但对 ID 属性的验证不起作用。