2016-12-06 100 views
1

我試圖定義一個.xsd文件,它將限制一個.xml文檔包含某些信息。這是.xsd。如何要求xml文檔包含一個元素的實例?

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
     targetNamespace="http://www.example.org/MicroscopyExperiment" 
     xmlns:tns="http://www.example.org/MicroscopyExperiment"> 
    <element name="MicroscopyExperiment"> 
    <complexType> 
     <sequence> 
     <element name="goal" type="string" minOccurs="1" maxOccurs="1"/> 
     <element name="cellType" type="string"/> 
     <element name="cellDensity" type="string"/> 
     <element name="injury" type="string"/> 
     <element name="drug" type="string"/> 
     <element name="media" type="string"/> 
     <element name="timing" type="string"/> 
     <element name="coating" type="string"/> 
     <element name="plateList"> 
      <complexType> 
      <sequence> 
       <element name="plate" 
         type="tns:plateType" 
         maxOccurs="unbounded" 
         minOccurs="1"/> 
      </sequence> 
      </complexType> 
     </element> 
     </sequence> 
    </complexType> 
    </element> 
    <complexType name="plateType"> 
    <sequence> 
     <element name="goalDiff" type="string"/> 
     <element name="cellTypeDiff" type="string"/> 
     <element name="cellDensityDiff" type="string"/> 
     <element name="injuryDiff" type="string"/> 
     <element name="drugDiff" type="string"/> 
     <element name="mediaDiff" type="string"/> 
     <element name="timingDiff" type="string"/> 
     <element name="coatingDiff" type="string"/> 
    </sequence> 
    </complexType> 
</schema> 

這個.xsd文件在Eclipse Neon.1a Release(4.6.1)中驗證不錯。

然後我創建了第一個.xml文件來驗證這個模式。這是.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE xml> 
<MicroscopyExperiment 
    xmlns:tns="http://www.example.org/MicroscopyExperiment" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.example.org/MicroscopyExperiment MicroscopyExperiment.xsd"> 

</MicroscopyExperiment> 

這也驗證了在Eclipse就好了。我沒有收到有關無法找到一整天都讓我瘋狂的.xsd文件的任何錯誤信息。問題是.xml不應該被驗證。我爲目標元素設置了minOccurs和maxOccurs都爲1,要求它只出現一次。但是,現在.xml文件中沒有任何目標,因此應該無法驗證。

任何提示將不勝感激。

問候,

Dessie

+0

你可以添加你的示例XML文件嗎? –

回答

1

您正在一點與命名空間的一塌糊塗。 在架構中,您包含一個targetNamespace。這意味着 所有全局聲明的元素都屬於該名稱空間。

但是,在您的XML文件中,您使用的MicroscopyExperiment沒有名稱空間前綴。 而且由於您沒有聲明默認命名空間,因此此元素與架構中的元素聲明不匹配。 一個解決這個方法是聲明正確的默認命名空間中的XML文件中:

<MicroscopyExperiment 
    xmlns="http://www.example.org/MicroscopyExperiment" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.example.org/MicroscopyExperiment MicroscopyExperiment.xsd"> 
</MicroscopyExperiment> 

另一種解決方案是將命名空間前綴添加到元素名稱:

<tns:MicroscopyExperiment 
    xmlns:tns="http://www.example.org/MicroscopyExperiment" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.example.org/MicroscopyExperiment MicroscopyExperiment.xsd"> 
</tns:MicroscopyExperiment> 

附錄:注和maxOccurs="1"是默認值,您不需要將它們包含在模式中。 另外,您可能應該將elementFormDefault="qualified"屬性添加到schema元素,否則本地元素(plate)不屬於任何名稱空間,這將導致更多混淆。

+1

嘗試添加tns:前綴到MicroscopyExperiment的解決方案,並解決了我的短期問題。還將納入您的其他一些建議。 – Dessie

相關問題