2016-08-30 99 views
0

我建立了基於該UMLXSD文件和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文件,我從UMLXML代碼建:

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

+1

不知足發表評論,但它是區分大小寫? –

+0

@CecilWard是的,案件確實很重要。圖書館與圖書館是兩回事 – Dijkgraaf

+0

自從我做了很多XSLT之後,很長時間了,所以我很生氣。 –

回答

0

您的模式完全不

目前您的模式將代表的是XML的XML相匹配。

<library> 
    <book title="title_0" year="100" /> 
    <magazine title="title_0" year="100" number="100" /> 
    <author id="id_0" name="name_1" /> 
</library> 

原因,它不會驗證對XML

  1. 由於CecilWard注意到,您的架構定義庫以小寫,但你的XML樣本具有它作爲標題的情況下(庫)。

  2. 此外,您的作者節點沒有被定義爲作者節點下的重複項目。

  3. 您尚未定義可包含書籍或雜誌的選擇節點。無界限應該在於那本書或雜誌,如果它們與您的示例XML混合在一起。

我剛剛生成了一個XML的模式,它將根據該XML文件進行驗證。

它不具有任何你所定義的keyref的,但你可以在添加這些回。

<?xml version="1.0" encoding="utf-16"?> 
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" 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 maxOccurs="unbounded" name="author"> 
       <xs:complexType> 
        <xs:attribute name="id" type="xs:string" use="required" /> 
        <xs:attribute name="name" type="xs:string" use="required" /> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="publications"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:choice maxOccurs="unbounded"> 
       <xs:element name="Book"> 
        <xs:complexType> 
        <xs:attribute name="year" type="xs:integer" use="required" /> 
        <xs:attribute name="title" type="xs:string" use="required" /> 
        <xs:attribute name="author" type="xs:string" use="required" /> 
        </xs:complexType> 
       </xs:element> 
       <xs:element name="Magazine"> 
        <xs:complexType> 
        <xs:attribute name="year" type="xs:integer" use="required" /> 
        <xs:attribute name="number" type="xs:integer" use="required" /> 
        <xs:attribute name="title" type="xs:string" use="required" /> 
        </xs:complexType> 
       </xs:element> 
       </xs:choice> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 
+0

在某些情況下,我堅持使用全小寫標識符。 –

+0

@CecilWard這樣做很好,但是XML和XSD都必須匹配。在OP案例中,他們沒有,因此OP正在得到的錯誤。 – Dijkgraaf

+0

對不起,不清楚 - 我的意思是我會堅持小寫,以避免區分大小寫錯誤的可能性。非常正確 –