如何在xsd中定义一个元素,该元素在子元素中具有父元素

时间:2012-12-18 06:43:44

标签: xml xsd

如何定义xsd中具有父元素的元素?

以下是一个例子:

<score seq="1" type="test" numeric="23">
    <option lbound="0" ubound="">
        <score seq="1" type="app" numeric="575">
            <option lbound="" ubound="0">0</option>
        </score>
    </option>
</score>

2 个答案:

答案 0 :(得分:1)

像这样:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="score">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="option">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="score">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="option">
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="xs:unsignedByte">
                            <xs:attribute name="lbound" type="xs:string" use="required" />
                            <xs:attribute name="ubound" type="xs:unsignedByte" use="required" />
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name="seq" type="xs:unsignedByte" use="required" />
                  <xs:attribute name="type" type="xs:string" use="required" />
                  <xs:attribute name="numeric" type="xs:unsignedShort" use="required" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="lbound" type="xs:unsignedByte" use="required" />
            <xs:attribute name="ubound" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="seq" type="xs:unsignedByte" use="required" />
      <xs:attribute name="type" type="xs:string" use="required" />
      <xs:attribute name="numeric" type="xs:unsignedByte" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>

答案 1 :(得分:0)

这是任何嵌套级别的选项......

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="score">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded" ref="option"/>
                <xs:element minOccurs="0" maxOccurs="unbounded" ref="ignore"/>
            </xs:sequence>
            <xs:attribute name="numeric" use="optional" type="xs:integer"/>
            <xs:attribute name="seq" use="required" type="xs:integer"/>
            <xs:attribute name="type" use="required" type="xs:NCName"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="option">
        <xs:complexType mixed="true">
            <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded" ref="score"/>
            </xs:sequence>
            <xs:attribute name="id" use="optional"/>
            <xs:attribute name="lbound" use="optional"/>
            <xs:attribute name="ubound" use="optional"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="ignore"/>
</xs:schema>

这适用于您给出的两个示例:

示例1

<score seq="1" type="test" numeric="23">
    <option lbound="0" ubound="">
        <score seq="1" type="app" numeric="575">
            <option lbound="" ubound="0">0</option>
        </score>
    </option>
</score>

示例2 (来自评论)

<score seq="1" type="IN">
    <option id="1">10</option>
    <ignore/>
</score>