2013-06-11 54 views
0

爲了簡單起見,我想在同一個命名空間中有一組文件,因爲它們在概念上是相關的。我有一個主要或中心的xsd,將包含其他模式文件,基本上作爲全局根元素。我的問題是最好的例子說明,但我基本上不能讓我的非中心架構驗證,這是一個命名空間的問題:如何創建同名命名空間設計一致的XSD?

模式1(支持):

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

    <xsd:simpleType name="test"> 
     <xsd:restriction base="xsd:string"> 
     </xsd:restriction> 
    </xsd:simpleType> 

    <xsd:complexType name="PersonType"> 
     <xsd:sequence> 
      <xsd:element name="Name" type="test" /> 
      <xsd:element name="SSN" type="xsd:string" /> 
     </xsd:sequence> 
    </xsd:complexType> 

</xsd:schema> 

模式2(中心):

<?xml version="1.0" encoding="UTF-8"?> 

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://www.company.org" 
      xmlns="http://www.company.org" 
      elementFormDefault="qualified"> 

    <xsd:include schemaLocation="http://www.person.org"/> 

    <xsd:element name="Company"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="Person" type="PersonType" 
          maxOccurs="unbounded"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

模式2很好,模式1不驗證。 「test」沒有命名空間,我不知道如何在不破壞我的意圖的情況下爲我的所有文件使用1個命名空間。

回答

0

目前還不清楚您要問什麼問題,所以我猜想問題是「爲什麼架構文檔1無法驗證,何時架構文檔2確認?」

我無法回答,因爲我無法重現您的結果。這兩個模式文檔都會在您提供的表單中引發錯誤。

Schema文檔1是指,在元件(http://www.company.org,名稱),局部給複合型(http://www.company.org,PersonType)的定義,來命名類型(http://www.person.org,檢驗)。但名稱空間http://www.person.org尚未導入,因此對該名稱空間中組件的引用不合法。

規範type="test"被解釋爲參考(http://www.person.org,test),因爲當「test」被解釋爲QName時,其名稱空間名稱將被視爲默認名稱空間(如果有)。這裏,默認名稱空間(在xsd:schema元素上聲明)是http://www.person.org

如果 - 這是我的錯誤猜測 - 您想引用名稱爲(http://www.company.org,test)的類型,該類型在架構文檔1的第7-10行中聲明,那麼您需要綁定命名空間前綴名稱空間http://www.company.org並使用該前綴。它會工作,例如,(使用默認的命名空間,以避免不必考慮前綴的)名稱的聲明更改爲

<xsd:element name="Name" type="tns:test" 
      xmlns:tns="http://www.company.org"/> 

或:

<xsd:element name="Name" type="test" 
      xmlns="http://www.company.org"/> 

注意,第7-10行聲明的簡單類型具有擴展名(http://www.company.org,test) - 我不知道你說''test'沒有命名空間「是什麼意思,但是你可能想檢查你的假設。

架構文檔2產生一個錯誤,因爲您在第6行的xsd:include中指定的架構位置在取消引用時會生成非XSD架構文檔(它是HTML頁面)的文檔。

我希望這會有所幫助。