2012-06-29 36 views
1

我有2次XSD的被嵌套:我可以創建一個xml,它指定2個嵌套xsd中的元素而不使用前綴嗎?

DefaultSchema.xsd:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="DefaultSchema" 
    targetNamespace="http://myNamespace.com/DefaultSchema.xsd" 
    elementFormDefault="qualified" 
    xmlns="http://myNamespace.com/DefaultSchema.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
> 
    <xs:complexType name="ZForm"> 
    <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
     <xs:element name="Part" minOccurs="0" maxOccurs="unbounded" type="Part"/> 
    </xs:sequence> 
    <xs:attribute name="Title" use="required" type="xs:string"/> 
    <xs:attribute name="Version" type="xs:int"/> 
    </xs:complexType> 

    <xs:complexType name="Part"> 
    <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
     <xs:element name="Label" type="Label" minOccurs="0"></xs:element> 
    </xs:sequence> 
    <xs:attribute name="Title" use="required" type="xs:string"/> 
    </xs:complexType> 
    <xs:complexType name="Label"> 
    <xs:simpleContent> 
     <xs:extension base="xs:string"> 
     <xs:attribute name="Title" type="xs:string"/> 
     </xs:extension> 
    </xs:simpleContent> 
    </xs:complexType> 
</xs:schema> 

ExportSchema.xsd:(這一個有點包裹1個元件(ZForms)主元件周圍(ZForm在DefaultSchema的)

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="ExportSchema" 
    targetNamespace="http://myNamespace.com/ExportSchema.xsd" 
    elementFormDefault="qualified" 
    xmlns="http://myNamespace.com/DefaultSchema.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:es="http://myNamespace.com/ExportSchema.xsd" 
> 
    <xs:import namespace="http://myNamespace.com/DefaultSchema.xsd" schemaLocation="DefaultSchema.xsd"/> 

    <xs:element name="ZForms" type="es:ZFormType"></xs:element> 

    <xs:complexType name="ZFormType"> 
    <xs:sequence> 
     <xs:element name="ZForm" type="ZForm" maxOccurs="unbounded" /> 
    </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

然後終於)我有一個生成的XML

<?xml version="1.0" encoding="utf-8"?> 
<ZForms xmlns="http://myNamespace.com/ExportSchema.xsd"> 
    <ZForm Version="1" Title="FormTitle"> 
    <Part Title="PartTitle" > 
     <Label Title="LabelTitle" /> 
    </Part> 
    </ZForm> 
</ZForms> 

Visual Studio抱怨它不知道'Part'是什麼。
我希望我不需要使用xml命名空間前綴(..)來使這個XML驗證,因爲ExportSchema.xsd有一個對DefaultSChema.xsd的引用。

有沒有什麼辦法可以使xml結構有效,而無需顯式指定DefaultSchema.xsd?或者這是不行?

回答

1

你可以得到這個工作(沒有命名空間前綴)如果從import更改爲include您的基礎架構:

ExportSchema.xsd

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="ExportSchema" 
    targetNamespace="http://myNamespace.com/DefaultSchema.xsd" 
    elementFormDefault="qualified" 
    xmlns="http://myNamespace.com/DefaultSchema.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
> 
    <xs:include schemaLocation="DefaultSchema.xsd" /> 

    <xs:element name="ZForms" type="ZFormType"></xs:element> 

    <xs:complexType name="ZFormType"> 
     <xs:sequence> 
      <xs:element name="ZForm" type="ZForm" maxOccurs="unbounded" /> 
     </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

注:這也需要改變您的目標命名空間轉換爲DefaultSchema.xsd。

From MSDN on xsd:import

之間的 包括元件和 進口元件是進口元件允許從模式文檔到模式組件的引用與不同的目標命名空間和從包括元素添加的模式組件的差異其他架構文檔具有相同的目標命名空間(或沒有指定的目標命名空間)到包含模式。簡而言之,導入元素允許您使用任何模式的模式組件; include元素允許您將包含模式的所有組件添加到包含模式。

DefaultSchema.xsd(沒有你的版本變更)

的test.xml

<?xml version="1.0" encoding="utf-8"?> 
<ZForms 
    xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" 
    xs:schemaLocation="http://myNamespace.com/DefaultSchema.xsd ExportSchema.xsd" 
    xmlns="http://myNamespace.com/DefaultSchema.xsd"> 
    <ZForm Version="1" Title="FormTitle">  
    <Part Title="PartTitle" >  
     <Label Title="LabelTitle" />  
    </Part> 
    </ZForm> 
</ZForms> 

這種組合似乎工作..

+0

這看起來像是朝着良好方向邁出的一步。任何想法爲什麼VS現在不顯示intellisense?另外,我下載了xmlspy(30天試用版),並試着針對(嵌套的)ExportSchma.xsd驗證xml。它一直在抱怨:「無法找到對此文檔實例中支持的模式類型(DTD,W3C模式)的引用」,現在我沒有找到解決方法。 您是否可以驗證xml? – TweeZz

+0

好吧,我找到了正確的'組合':) 我會更新你的答案,並且標記是正確的,因爲你指出我是問題的核心。謝謝! – TweeZz

0

你的架構文檔DefaultSchema有目標名稱=「http://myNamespace.com/DefaultSchema.xsd」,所以它是說ZForm,Part和Label元素必須在這個namespa CE。但在您的實例中,這些元素不在此名稱空間中。因此,您的實例對此架構無效。

相關問題