我試圖定義一個.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
你可以添加你的示例XML文件嗎? –