加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

定义一个必须为空且没有属性的XML元素

发布时间:2020-12-16 08:01:24 所属栏目:百科 来源:网络整理
导读:我需要定义一个没有子元素或任何内容的XML元素,并且没有属性。 这是我在做什么 xs:element name="myEmptyElement" type="_Empty"/xs:complexType name="_Empty"/xs:complexType 这似乎工作正常,但我不得不想知道是否有办法做到这一点,而不必声明一个复杂
我需要定义一个没有子元素或任何内容的XML元素,并且没有属性。

这是我在做什么

<xs:element name="myEmptyElement" type="_Empty"/>
<xs:complexType name="_Empty">
</xs:complexType>

这似乎工作正常,但我不得不想知道是否有办法做到这一点,而不必声明一个复杂的类型。另外,如果我有什么问题,请让我知道。

预计有人可能好奇为什么我需要这样的元素:它是一个SOAP操作,不需要任何参数值。

(1)您可以避免定义一个命名的xs:complexType:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="myEmptyElement">
    <xs:complexType/>
  </xs:element>
</xs:schema>

(2)可以使用xs:simpleType而不是xs:complexType:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="myEmptyElement">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:maxLength value="0"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xs:schema>

(3)你可以使用fixed =“”[credit:@Nemo]:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="myEmptyElement" type="xs:string" fixed=""/>
</xs:schema>

(4)但请注意,如果您避免对内容模型发表任何说明:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="myEmptyElement"/>
</xs:schema>

您将允许myEmptyElement中的任何属性和任何内容。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读