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

Joh*_*n S 25 xml xsd

我需要定义一个没有子元素或任何内容的XML元素,并且没有属性.

这就是我在做的事情:

<xs:element name="myEmptyElement" type="_Empty"/>
<xs:complexType name="_Empty">
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)

这似乎工作正常,但我不得不怀疑是否有办法这样做,而不必声明一个复杂的类型.此外,如果我有什么问题,请告诉我.

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

kjh*_*hes 37

(1)您可以避免定义命名xs:complexType:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="myEmptyElement">
    <xs:complexType/>
  </xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

(2)您可以使用a 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>
Run Code Online (Sandbox Code Playgroud)

(3)您可以使用fixed="" [credit:@Nemo ]:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="myEmptyElement" type="xs:string" fixed=""/>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

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

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="myEmptyElement"/>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

您将允许任何属性和任何内容myEmptyElement.