我有以下XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="library.xsd">
<!--<!DOCTYPE library SYSTEM "library.dtd">-->
<authors>
<author aid="a1">Bill Evjen</author>
<author aid="a2">Michael Kay</author>
<author aid="a3">Kevin Goldberg</author>
<author aid="a4">Michael Morrison</author>
</authors>
<books>
<book bookID="b001" author="a2">
<title>XSLT 2.0 and XPath 2.0 Programmer's Reference</title>
<stock>4</stock>
<publisher>John Wiley</publisher>
<year>2009</year>
<use type="advanced" />
<use type="reference" />
</book>
<book bookID="b002" author="a1 a2">
<title>Professional XML (Programmer to Programmer) </title>
<stock>2</stock>
<publisher>John Wiley</publisher>
<year>2007</year>
<use type="professional" />
<use type="advanced" />
<use type="reference" />
</book>
<book bookID="b003" author="a3">
<title>XML: Visual QuickStart Guide</title>
<stock>3</stock>
<publisher>Peachpit Press</publisher>
<year>2008</year>
<use type="introductory" />
<use type="reference" />
</book>
<book bookID="b004" author="a4">
<title>Sams Teach Yourself XML in 24 Hours</title>
<stock>5</stock>
<publisher>SAMS</publisher>
<year>2005</year>
</book>
</books>
</library>
如何在xsd中使用key / keyref,以便book元素中的'author'属性包含author元素'aid'属性的值/值列表?
我已经测试了我在visual studio中创建的以下XSD,但是我收到错误“The Keyref'书籍'找不到引用的密钥或范围内的唯一。”我不知道如何解决这个问题:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element name="author" maxOccurs="unbounded" type="theAuthor"/>
</xs:sequence>
</xs:complexType>
<xs:key name="uniqueAuthorID">
<xs:selector xpath="author"/>
<xs:field xpath="@aid"/>
</xs:key>
</xs:element>
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="theBook" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueBookID">
<xs:selector xpath="book"/>
<xs:field xpath="@bookID"/>
</xs:unique>
<xs:keyref refer="uniqueAuthorID" name="authorKeyRef">
<xs:selector xpath="book"></xs:selector>
<xs:field xpath="@author"></xs:field>
</xs:keyref>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="theAuthor">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="aid" type="xs:string">
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="theBook">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="stock" type="xs:integer"/>
<xs:element name="publisher" type="xs:string"/>
<xs:element name="year" type="xs:string"/>
<xs:element name="use" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="type" type="useType" default="advanced"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="bookID" type="xs:string"/>
<xs:attribute name="author" type="xs:string"/>
</xs:complexType>
<xs:simpleType name="useType">
<xs:restriction base="xs:string">
<xs:enumeration value="advanced" />
<xs:enumeration value="reference" />
<xs:enumeration value="professional" />
<xs:enumeration value="introductory" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
答案 0 :(得分:5)
问题在于约束声明的布局/范围。我们来看看下图:
就像在“花括号”编程世界(Java / C#)中一样,您的元素充当“块”,自动定义声明的名称。在这种情况下,您的uniqueAuthorID
对作者及其下方可见,这就是books
下定义的内容不可见的原因。
所以你需要做的是移动并修改库下的uniqueAuthorID
。
这应该解决您的XSD问题。
您的XML不会像您期望的那样验证:
The key sequence 'a1 a2' in Keyref fails to refer to some key.
即使您将作者正确定义为列表约束简单类型,keyref机制也不会在其组件值中将其分解。要使其工作,您必须将author属性转换为重复元素,然后使用keyref检查每个元素(以防万一,属性不能重复)。