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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20751782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:57:55  来源:igfitidea点击:

Define an XML element that must be empty and has no attributes

xmlxsd

提问by John S

I needed to define an XML element that has no sub-elements or any content at all, and has no attributes.

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

This is what I am doing:

这就是我正在做的:

<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.

这似乎工作正常,但我想知道是否有办法在不必声明复杂类型的情况下做到这一点。另外,如果我所拥有的有任何问题,请告诉我。

Anticipating that someone might be curious why I would need such an element: It is for a SOAP operation that does not require any parameter values.

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

回答by kjhughes

(1) You could avoid defining a named xs:complexType:

(1) 你可以避免定义一个命名的xs:complexType

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

(2) You could use a xs:simpleTypeinstead of a xs:complexType:

(2) 您可以使用 axs:simpleType代替 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) You could use fixed=""[credit: @Nemo]:

(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) But note that if you avoid saying anything about the content model:

(4) 但请注意,如果您避免提及内容模型:

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

You'll be allowing any attributes on and any content in myEmptyElement.

您将允许myEmptyElement.

回答by Emidio Stani

Another example could be:

另一个例子可能是:

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

or

或者

<xs:element name="myEmptyElement">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value=""/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>

or

或者

<xs:element name="myEmptyElement">
    <xs:complexType>
        <xs:complexContent>
            <xs:restriction base="xs:anyType"/>
        </xs:complexContent>
    </xs:complexType>
</xs:element>