简体   繁体   中英

XSD: define complex element with attribute and other child elements

I want to define an element like this:

<A id='1'>
  <name>jack</name>
</A>

With this below I could define attribute id of element A:

<xs:complexType name="A">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:int" name="id"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

Actually it's not correct, the XSD above defines an element <A id="1">hi</A> . I don't know how to discard the base attribute of <xs:extension> element.

And with the XSD below I could define the inside element <name> :

<xs:element name = "A">
    <xs:complexType>
        <xs:sequence>

            <xs:element name = "name" type="xs:string"/>

        </xs:sequence>
    </xs:complexType>
</xs:element>

Could I combine this two together like this below? I could not get the syntax correct.

    <xs:element name = "A">
        <xs:complexType>
            <xs:sequence>
                <xs:element name = "name" type="xs:string"/>                       
            </xs:sequence>
        </xs:complexType>
        <xs:simpleContent>
          <xs:extension>
            <xs:attribute type="xs:int" name="id"/>
          </xs:extension>
        </xs:simpleContent>
    </xs:element>

Nothing in your XML or stated constraints indicates xs:extension is required here.

The following simple XSD,

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="A">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:int"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

will validate your XML,

<A id='1'>
  <name>jack</name>
</A>

as requested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM