2016-12-06 46 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

相关问题