定义一个必须为空且没有属性的 XML 元素 [英] Define an XML element that must be empty and has no attributes
问题描述
我需要定义一个完全没有子元素或任何内容且没有属性的 XML 元素.
I needed to define an XML element that has no sub-elements or any content at all, and has no attributes.
这就是我正在做的:
<xs:element name="myEmptyElement" type="_Empty"/>
<xs:complexType name="_Empty">
</xs:complexType>
这似乎工作正常,但我想知道是否有办法在不必声明复杂类型的情况下做到这一点.另外,如果我所拥有的有任何问题,请告诉我.
This appears to work fine, but I have to wonder if there is a way to do this without having to declare a complex type. Also, if there is anything wrong with what I have, please let me know.
预计有人可能会好奇我为什么需要这样一个元素:它用于不需要任何参数值的 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
:
(2) You could use a xs:simpleType
instead of a 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]:
(3) You could use 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
中的任何属性和任何内容.
You'll be allowing any attributes on and any content in myEmptyElement
.
这篇关于定义一个必须为空且没有属性的 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!