2010-12-18 29 views
3

我想使用XInclude將多個包含的XML文件拆分。這種方法我優先於其他方法,因爲包含的XML文件可以獨立成爲單獨驗證的文件。XInclude驗證問題

我有以下示例模式(mybook.xsd):


<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" 
attributeFormDefault="unqualified"> 
<xs:import 
    namespace="http://www.w3.org/XML/1998/namespace" 
    schemaLocation="http://www.w3.org/2001/xml.xsd"/> 
<xs:element name="mybook"> 
    <xs:annotation> 
    <xs:documentation>Comment describing your root element</xs:documentation> 
    </xs:annotation> 
    <xs:complexType> 
    <xs:sequence> 
    <xs:element name="toc"> 
    <xs:complexType> 
     <xs:attributeGroup ref="xml:specialAttrs"/> 
    </xs:complexType> 
    </xs:element> 
    <xs:element ref="part" maxOccurs="unbounded"/> 
    <xs:element name="index"> 
    <xs:complexType> 
     <xs:attributeGroup ref="xml:specialAttrs"/> 
    </xs:complexType> 
    </xs:element> 
    </xs:sequence> 
    </xs:complexType> 
</xs:element> 
<xs:element name="part"> 
    <xs:complexType> 
    <xs:sequence> 
    <xs:element name="chapter" maxOccurs="unbounded"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="page" maxOccurs="unbounded"> 
     <xs:complexType> 
     <xs:simpleContent> 
      <xs:extension base="xs:string"> 
      <xs:attributeGroup ref="xml:specialAttrs"/> 
      </xs:extension> 
     </xs:simpleContent> 
     </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
     <xs:attributeGroup ref="xml:specialAttrs"/> 
    </xs:complexType> 
    </xs:element> 
    </xs:sequence> 
    <xs:attributeGroup ref="xml:specialAttrs"/> 
    <xs:attribute name="chaptername" use="required"/> 
    </xs:complexType> 
</xs:element> 
</xs:schema> 

我做了部分的全局元素,所以我可以先從根元素「部分」的新的XML元素。現在我的XML文件看起來像:

主文件(mybook.xml):


<?xml version="1.0" encoding="UTF-8"?> 
<mybook 
xsi:noNamespaceSchemaLocation="mybook.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xi="http://www.w3.org/2001/XInclude" > 
<toc/> 
<part chaptername="Chapter 1" > 
    <chapter> 
    <page>String</page> 
    <page>String</page> 
    </chapter> 
    <chapter> 
    <page>String</page> 
    <page>String</page> 
    </chapter> 
</part> 
    <xi:include href="part2.xml"/> 
<index/> 
</mybook> 

我的包含文件(part2.xml):


<?xml version="1.0" encoding="UTF-8"?> 
<part chaptername="Chapter 2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="mybook.xsd" > 
<chapter> 
    <page>String</page> 
    <page>String</page> 
</chapter> 
<chapter> 
    <page>String</page> 
    <page>String</page> 
</chapter> 
</part> 

在XMLSPY,現在我可以成功驗證part2.xml。然而驗證mybook.xml的時候,我得到了以下錯誤:

 
File mybook.xml is not valid. 
File part2.xml is not valid. 
    The 'noNamespaceSchemaLocation' attribute references a schema whose target namespace was already used for validation. 
    Error location: part 
    Details 
    The 'noNamespaceSchemaLocation' attribute references a schema whose target namespace was already used for validation. 
    cvc-elt.5.2.1: The element is not valid with respect to the actual type definition '{anonymous}'. 

因爲我failry XML新手我看不到(但試了幾件事情)需要做什麼兼得XML文件成功地驗證對mybook.xsd。

任何人都可以幫助我嗎?在此先感謝

+0

也許,請參閱http://stackoverflow.com/questions/1098116/xinclude-schema-namespace-problem以獲得無法解決您的問題? – Istao 2010-12-18 10:28:03

+0

是的,我已經檢查過這個問題,但無論我嘗試什麼,在驗證過程中都會收到錯誤。我認爲XInclude方法的優點之一是可以有兩個成功驗證xml文件,所以我認爲應該有一種方法來實現這一點。 – eiri 2010-12-18 12:29:22

回答

2

您正在運行禁止使用noNamespaceSchemaLocation的限制。基本上,這可以用來作爲XSD驗證程序提示如何查找您的模式。或多或少,你不允許這個屬性出現多次。你可以參考this

因此,碰到的第一個noNamespaceSchemaLocation發生了什麼,它告訴驗證程序如何找到no命名空間模式,以便可以驗證「mybook」元素。後來,當涉及到「part」元素時,會找到另一個noNamespaceSchemaLocation,但是如果這會導致對已經驗證的「mybook」元素進行其他定義,那麼會發生什麼呢?正是出於這個原因,規範不允許這樣的事情,因此你得到了你記下的錯誤。

看來您需要依靠其他方法來告訴驗證程序如何找到您的架構文檔,而不是使用noNamespaceSchemaLocation。