我建立了基於該UML的XSD文件和XML 代碼(鏈接,下面的代碼),但我不能驗證XML文件。如何正確構建XSD文件?
<?xml version="1.0" encoding="ISO-8859-15"?>
<Library
xsi:noNamespaceSchemaLocation="lib.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<authors>
<author id="author1" name="Einstein"/>
</authors>
<publications>
<Book year="1942" title="7 Kingdoms" author="Einstein"/>
<Magazine year="2010" number="203" title="The News"/>
<Book year="1956" title="The Fall" author="author1"/>
</publications>
</Library>
這裏是XSD文件,我從UML和XML代碼建:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="library" type="Library">
<!-- Begin KEY -->
<xsd:key name="Author">
<xsd:selector xpath="./authors/Author"/>
<xsd:field xpath="@id"/>
</xsd:key>
<xsd:key name="Magazine">
<xsd:selector xpath="./publications/Magazine"/>
<xsd:field xpath="@id"/>
</xsd:key>
<xsd:key name="Book">
<xsd:selector xpath="./publications/Book"/>
<xsd:field xpath="@id"/>
</xsd:key>
<xsd:key name="Publication">
<xsd:selector xpath="./publications/Magazine | ./publications/Book"/>
<xsd:field xpath="@id"/>
</xsd:key>
<!-- END KEY -->
<!-- Begin KEYREF -->
<xsd:keyref name="Book.author" refer="Author">
<xsd:selector xpath="./publications/Book/author"/>
<xsd:field xpath="@ref"/>
</xsd:keyref>
<!-- END KEYREF -->
</xsd:element>
<xsd:complexType name="Author">
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="name" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="Publication">
<xsd:attribute name="title" type="xsd:string"/>
<xsd:attribute name="year" type="xsd:integer"/>
</xsd:complexType>
<xsd:complexType name="Magazine">
<xsd:complexContent>
<xsd:extension base="Publication">
<xsd:attribute name="number" type="xsd:integer"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="Book">
<xsd:complexContent>
<xsd:extension base="Publication"/>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="Library">
<xsd:sequence>
<xsd:element name="book" type="Book" maxOccurs="unbounded"/>
<xsd:element name="magazine" type="Magazine" maxOccurs="unbounded"/>
<xsd:element name="author" type="Author" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
我有,當我試圖問題驗證XML文件是因爲它沒有找到元素「庫」的聲明。
我不知道該keyref NAME =「Book.author」創建足以書之間和作者建立的聯繫,我相信這個問題來源於此。
我是否需要在中添加代碼complexType name =「Book」創建鏈接並因此驗證XML文件?
不知足發表評論,但它是區分大小寫? –
@CecilWard是的,案件確實很重要。圖書館與圖書館是兩回事 – Dijkgraaf
自從我做了很多XSLT之後,很長時間了,所以我很生氣。 –