XSD:如果存在可选父级,则需要嵌套标记

时间:2014-07-08 09:50:55

标签: xml validation xsd nested

我的XML中有可选的标签,如果存在可选标签,则需要该元素的子标签,如何使用XSD实现此目的(如果在XSD中无法实现,则可以使用其他解决方案) )?

我的XML的一个示例部分:

                <xs:element name="deviceLocation" minOccurs="0" maxOccurs="unbounded" nillable="true">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element type="xs:string" name="xPos"/>
                      <xs:element type="xs:string" name="yPos"/>
                      <xs:element type="xs:string" name="zPos"/>
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>

正如您所看到的,deviceLocation标记是可选的 - 如果此标记存在,那么我需要子标记xPosyPoszPos

编辑:请求的完整架构

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="homeML">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="inhabitantDetails" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="inhabitantID" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="carePlan" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="comment" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
          </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="location" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="locationID" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="locationDescription" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="referenceObjects" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="locationDevice" minOccurs="0" maxOccurs="unbounded" nillable="true">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="lDeviceID" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
                    <xs:element name="deviceDescription" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
                    <xs:element name="deviceLocation" minOccurs="0" maxOccurs="unbounded" nillable="true">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="xPos" type="xs:string" minOccurs="0"/>
                          <xs:element name="yPos" type="xs:string" minOccurs="0"/>
                          <xs:element name="zPos" type="xs:string" minOccurs="0"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="units" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
                    <xs:element name="realTimeInformation" minOccurs="0" maxOccurs="unbounded" nillable="true">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="runID" type="xs:string" minOccurs="0"/>
                          <xs:element name="sampleRate" type="xs:string" minOccurs="0"/>
                          <xs:element name="startTimeStamp" type="xs:dateTime" minOccurs="0"/>
                          <xs:element name="endTimeStamp" type="xs:dateTime" minOccurs="0"/>
                          <xs:element name="data" type="xs:string" minOccurs="0"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="event" minOccurs="0" maxOccurs="unbounded" nillable="true">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="lEventID" type="xs:string" minOccurs="0"/>
                          <xs:element name="timeStamp" type="xs:dateTime" minOccurs="0"/>
                          <xs:element name="data" type="xs:string" minOccurs="0"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="mobileDevice" minOccurs="0" maxOccurs="unbounded" nillable="true">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="mDeviceID" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="deviceDescription" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="units" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="devicePlacement" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="quantisationResolution" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="realTimeInformation" minOccurs="0" maxOccurs="unbounded" nillable="true">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="runID" type="xs:string" minOccurs="0"/>
                    <xs:element name="sampleRate" type="xs:string" minOccurs="0"/>
                    <xs:element name="startTimeStamp" type="xs:dateTime" minOccurs="0"/>
                    <xs:element name="endTimeStamp" type="xs:dateTime" minOccurs="0"/>
                    <xs:element name="data" type="xs:string" minOccurs="0"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="event" minOccurs="0" maxOccurs="unbounded" nillable="true">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="mEventID" type="xs:string" minOccurs="0"/>
                    <xs:element name="timeStamp" type="xs:dateTime" minOccurs="0"/>
                    <xs:element name="data" type="xs:string" minOccurs="0"/>
                    <xs:element name="support" type="xs:string" minOccurs="0"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="annotationDetails" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="annotationID" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
              <xs:element name="experimentType" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
              <xs:element name="lDevice" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="mDevice" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
              <xs:element name="startTimeStamp" type="xs:dateTime" minOccurs="1" maxOccurs="unbounded"/>
              <xs:element name="endTimeStamp" type="xs:dateTime" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

1 个答案:

答案 0 :(得分:1)

如果您声明这样的(子)元素:

<xs:element name="n" minOccurs="0" type="someType"/>

然后元素是可选的,但如果它发生,那么它必须符合someType类型。因此,如果someType定义了具有必需元素的内容模型,那么n是可选的,但是当存在时,n的子元素是必需的。

顺便说一下,它的元素是可选的,而不是标签。元素通常有两个标记,即开始标记和结束标记。