开发者

xsd element with the same name

to make xsd for element with same names that only identifyed by attribute value example :-

<a>
  <b n="structure one">
    <c n="inner element 1"/>
    <c n="inner element 2"/>
    <c n="inner element 3"/>
  </b>
  <b n="structure two">
    <c n="inner element 1 for structure two"/>
    <c n="inner element 2 for structure two开发者_JAVA百科"/>
    <c n="inner element 3 for structure two"/>
  </b>
</a>

notice that from the XML i have to mention specific value that belong to the inner element same for structure


Not sure what your specific requirements are, but the following schema validates your document. It says that the root element must be named a, and it can contain any number of b elements, which themselves contain any number of c elements. The b and c elements must contain the attribute with the name n.

<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="b">
          <xs:complexType>
            <xs:sequence>
              <xs:element maxOccurs="unbounded" name="c">
                <xs:complexType>
                  <xs:attribute name="n" type="xs:string" use="required" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="n" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

If you want to constrain the attributes to a specific set of values, you can use a restriction. This schema enumerates the possible values of the n attributes on the b and c elements:

<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="b" type="b"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="b">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="c">
        <xs:complexType>
          <xs:attribute name="n" use="required">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:enumeration value="inner element 1"/>
                <xs:enumeration value="inner element 2"/>
                <xs:enumeration value="inner element 3"/>
                <xs:enumeration value="inner element 1 for structure two"/>
                <xs:enumeration value="inner element 2 for structure two"/>
                <xs:enumeration value="inner element 3 for structure two"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
        </xs:complexType>
      </xs:element>
    </xs:sequence>

    <xs:attribute name="n" use="required">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="structure one"/>
          <xs:enumeration value="structure two"/>
          <xs:enumeration value="structure three"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
  </xs:complexType>

</xs:schema>

You can also constrain the values of the attributes with a regex pattern, like this:

<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="b" type="b"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="b">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="c">
        <xs:complexType>
          <xs:attribute name="n" use="required">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:pattern value="^inner element [0-9]+.*$"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
        </xs:complexType>
      </xs:element>
    </xs:sequence>

    <xs:attribute name="n" use="required">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:pattern value="structure (one|two|three)"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
  </xs:complexType>

</xs:schema>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜