2013-02-09 20 views
0

我發現了幾個Q的有關XInclude的,但沒有說具體回答了我關於如何將外部文檔使用簡單的XInclude的

這裏非常基本的問題是幾個XML文檔的,我想REF對方:

<?xml version="1.0" encoding="UTF-8"?> 
<t:person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://test test-schema.xsd" 
    xmlns:t="http://test"> 

    <t:first-name>Wilma</t:first-name> 
    <t:last-name>Flintstone</t:last-name> 
    <t:spouse> 
     <xi:include xmlns:xi="http://www.w3.org/TR/xinclude" href="fred.xml"/> 
    </t:spouse> 

    </t:person> 

<?xml version="1.0" encoding="UTF-8"?> 
<t:person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://test test-schema.xsd" 
xmlns:t="http://test"> 
    <t:first-name>Fred</t:first-name> 
    <t:last-name>Flintstone</t:last-name> 
    <t:spouse> 
     <xi:include xmlns:xi="http://www.w3.org/TR/xinclude" href="wilma.xml"/> 
    </t:spouse> 
</t:person> 

和架構:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://test" 
    targetNamespace="http://test" elementFormDefault="qualified"> 

<xs:element name="person" type="personType"/> 


<xs:complexType name="personType"> 
    <xs:sequence> 
     <xs:element name="first-name" type="xs:string"/> 
     <xs:element name="last-name" type="xs:string"/> 
     <xs:element name="spouse" type="personType"/> 
    </xs:sequence> 
</xs:complexType> 

</xs:schema> 

的XI:include元素快到了爲無效。我一直在四處搜尋,找不到像這樣的簡單例子。 xi:只包含應該在那裏的元素的替身,對嗎?

感謝, BP

回答

1

人誰願意執行這兩個驗證和XInclude處理可能要執行的XInclude第一,然後對其進行驗證,或者先驗證,然後執行XInclude的,或驗證,然後再進行XInclude處理,然後再次驗證。在目前的閱讀技術狀態下,如果沒有人的幫助,軟件無法分辨出哪些是需要的。你知道你想要發生什麼命令,但是你有沒有告訴你的軟件?從您的描述中,聽起來好像您的處理器默認爲先驗證,然後執行XInclude;如果你想要一個非默認的處理序列,你必須告訴你的處理器。你如何做到這一點是依賴於處理器的;閱讀文檔。

1

您對Xinclude有錯誤的命名空間。

對任何一個xml文件運行xmllint --xinclude都不會產生變化 ,因爲它不被識別爲xinclude語句。

命名空間更改爲:xmlns:xi="http://www.w3.org/2001/XInclude"

,它將使在輸出一些變化,但你也對相互遞歸得到一個錯誤 :

$xmllint --xinclude wilma.xml 
fred.xml:8: element include: XInclude error : detected a recursion in wilma.xml 
<?xml version="1.0" encoding="UTF-8"?> 
<t:person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://test" xsi:schemaLocation="http://test test-schema.xsd"> 
    <t:first-name>Wilma</t:first-name> 
    <t:last-name>Flintstone</t:last-name> 
    <t:spouse> 
     <t:person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://test" xsi:schemaLocation="http://test test-schema.xsd"> 
    <t:first-name>Fred</t:first-name> 
    <t:last-name>Flintstone</t:last-name> 
    <t:spouse> 
     <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="wilma.xml"/> 
    </t:spouse> 
</t:person> 
    </t:spouse> 
</t:person> 

的XInclude應該遞歸處理xincludes ,但根據http://www.w3.org/TR/xinclude/#loops

當遞歸處理一個xi:include元素時,它是一個致命的錯誤pro另一個xi:include元素包含已經在包含鏈中處理過的包含位置和xpointer屬性值。

而且,當你嘗試驗證它對抗的模式,除了遞歸錯誤,你會得到:

fred.xml:4: element person: Schemas validity error : Element '{http://test}person': This element is not expected. Expected is ({http://test}first-name). 
wilma.xml fails to validate 

我相信這是因爲您的模式說,配偶ISA personType,但在你的xml, 配偶CONTAINS personType元素:人。

再加上上面說的c-m-sperberg-mcqueen。

如果您希望在進行xinclude擴展之前驗證它,那麼您需要包含 xi:include元素,它是模式中的屬性。

如果你想讓它做的XInclude擴張後確認,該XInclude的處理器將 (除非你告訴它不以某種方式)通常會添加一個xml:base屬性的 包括元素,所以你需要添加xml:base作爲架構中的允許屬性。 (我最初以爲,由於xml命名空間是保留的,xml:attributes 不需要包含在模式中,但事實並非如此。)