简体   繁体   English

XSD:使用属性和其他子元素定义复杂元素

[英]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: 有了这个,我可以定义元素A的属性id

<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> . 实际上它不正确,上面的XSD定义了一个元素<A id="1">hi</A> I don't know how to discard the base attribute of <xs:extension> element. 我不知道如何丢弃<xs:extension>元素的基本属性。

And with the XSD below I could define the inside element <name> : 使用下面的XSD,我可以定义内部元素<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. XML或规定的约束中没有任何内容表示此处需要xs:extension

The following simple XSD, 以下简单的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, 将验证您的XML,

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

as requested. 按照要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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