2009-07-08 121 views
3

我正在嘗試使用XML Includes來幫助管理一個需要可供人類和機器使用的大型XML結構。XInclude架構/名稱空間驗證?

但是在嘗試構建可以針對現有模式進行驗證的XML文件時,遇到了無數的問題。這是我想要做的一個簡單的例子。

我的「main.xml」文件不驗證。

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Main.xml - This fails to validate. --> 
<ns1:main xsi:schemaLocation="http://www.example.com/main main.xsd" 
      xmlns:ns1="http://www.example.com/main" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xi="http://www.w3.org/2001/XInclude"> 

    <name>String</name> 
    <xi:include href="child.xml"/> <!-- What I'm trying to do. --> 

</ns1:main> 

「child.xml」文件可以很好地驗證爲獨立文件。

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Child.xml - This validates fine, as a standalone file. --> 
<ns1:child xsi:schemaLocation="http://www.example.com/main main.xsd" 
      xmlns:ns1="http://www.example.com/main" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <name>String</name> 
    <age>String</age> 

</ns1:child> 

這裏是我的架構:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Main.xsd - My Simplified Schema --> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:ns1="http://www.example.com/main" 
      targetNamespace="http://www.example.com/main"> 

    <!-- Main Element (References Child) --> 
    <xs:element name="main"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="name" type="xs:string"/> 
       <xs:element ref="ns1:child"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <!-- Child Element --> 
    <xs:element name="child"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="name" type="xs:string"/> 
       <xs:element name="age" type="xs:string"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 

我的問題是幾乎顯然與命名空間,但我在爲如何解決我的問題的損失。

+0

嘗試驗證時會出現什麼錯誤? – skaffman 2009-07-08 13:39:17

+0

當解析main.xml時,我看到錯誤:「'schemaLocation'屬性引用了一個模式,該模式的目標名稱空間已被用於驗證。」 - 在Altova的XML Spy中。 – Nate 2009-07-08 13:47:11

回答

5

As skaffman已經指出,XML Schema和XInclude不兼容。

xmllint狀態這顯然的驗證錯誤消息:

main.xml:9: element include: Schemas validity error : Element '{http://www.w3.org/2001/XInclude}include': This element is not expected. Expected is ({http://www.example.com/main}child). 
main.xml fails to validate 

引述W3C Recommendation「的XInclude定義了通過將XML模式所產生的擴充信息集沒有關係這樣的擴充信息集可被提供作爲。輸入信息集或這種增強可能會應用於由包含引起的信息集。「

因此,您應該先應用XIncludes構造整個XML文件,然後再驗證此文件。

編輯:您可以使用xmllint和--xinclude來驗證main.xml。

2

我不認爲XML Schema和XInclude規範是保證彼此兼容。單獨的XML處理器可能會允許它,但其他人不會。

作爲一般規則,我會說這兩個不應該一起使用。

P.S.我不確定你爲什麼認爲這是一個名稱空間問題。什麼給你的印象?

0

我不認爲這是由XInclude和模式之間的不兼容造成的;它看起來像包含在驗證之前未被處理。因此該模式不允許在main中包含「include」元素,而只允許「child」元素。如果你可以強制你的XML處理器在驗證之前處理include ...

2

我同意grantwparks - XInclude和XML Schema完全可以一起使用。這些規格是故意相互獨立的。顯然,XInclude的作者希望提供自由讓包含在驗證之前或驗證之後進行處理。

thread on velocityreviews討論這一問題,並幫助我瞭解此事this post on xml.com答案,從中報價:

One of the most common questions about XInclude is how inclusion interacts with validation, XSL transformation, and other processes that may be applied to an XML document. The short answer is that it doesn't. XInclusion is not part of any other XML process. It is a separate step which you may or may not perform when and where it is useful to you.

For example, consider validation against a schema. A document can be validated before inclusion, after inclusion, or both. If you validate the document before the xi:include elements are replaced, then the schema has to declare the xi:include elements just like it would declare any other element. If you validate the document after the xi:include elements are replaced, then the schema has to declare the replacement elements. Inclusion and validation are separate, orthogonal processes that can be performed in any order which is convenient in the local environment

那麼它似乎歸結爲,是讓你的XML工具來處理xi:驗證前包含元素(這是OP需要的示例)。例如,在Eclipse的XML編輯器中,在XML - > XML文件 - >驗證(使用RSA 8.5)下有一個設置「Process XML Inclusions」,需要打開該設置才能讓編輯器處理xi:include before驗證。

Andy