Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

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

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(1) You could avoid defining a named 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: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) 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) But note that if you avoid saying anything about the content model:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...