如何要求xml文档包含元素的唯一实例?

时间:2016-12-06 19:56:58

标签: xml xsd

我试图定义一个.xsd文件,该文件将限制.xml文档包含某些信息。这是.xsd。

<?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不应该验证。我将goal元素的minOccurs和maxOccurs都设置为1,要求它只出现一次。但是,目前.xml文件中没有目标,因此无法验证。

任何提示都会非常感激。

此致

Dessie

1 个答案:

答案 0 :(得分:1)

你正在使命名空间搞得一团糟。 在架构中,您包含targetNamespace。这意味着 所有全局声明的元素都属于该命名空间。

但是,在XML文件中,使用不带名称空间前缀的MicroscopyExperiment。 并且因为您也没有声明默认命名空间,所以此元素与Schema中的元素声明不匹配。 解决此问题的一种方法是在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>

ADDENDUM :请注意,minOccurs="1"maxOccurs="1"是默认设置,您不需要将它们包含在架构中。 此外,您可能应该将elementFormDefault="qualified"属性添加到schema元素, 否则本地元素(plate)不属于任何名称空间,这将导致更多的混淆。