【问题标题】:add attribute to xml element inside sequence in complextype向复杂类型中序列内的xml元素添加属性
【发布时间】:2017-02-06 22:43:03
【问题描述】:

我有一个 xsd:

<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

我想将属性“Attribute1”添加到“firstname”元素:

<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string">
          <xs:attribute name="Attribute1" type="xs:string/>
      </xs:element>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

执行此操作时,我收到一条错误消息:“此上下文不支持 'http://www.w3.org/2001/XMLSchema:attribute' 元素。”

将属性分配给元素“名字”的正确方法是什么?

【问题讨论】:

    标签: xml xsd schema


    【解决方案1】:

    缺少一些中间嵌套元素。元素firstname 必须定义一个匿名复杂类型、一个简单的字符串内容(更准确地说,该类型是从xs:string 扩展派生的),并添加了属性。

    <xs:element name="employee">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="firstname">
            <xs:complexType>
              <xs:simpleContent>
                <xs:extension base="xs:string">
                  <xs:attribute name="Attribute1" type="xs:string"/>
                </xs:extension>
              </xs:simpleContent>
            </xs:complexType>
          </xs:element>
          <xs:element name="lastname" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    

    在原始定义中,如果元素firstname 的类型为xs:string,则不允许任何属性。

    【讨论】:

      猜你喜欢
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多