定义一个必须为空并且没有属性的XML元素
问题描述:
我需要定义一个根本没有子元素或任何内容且没有属性的XML元素。定义一个必须为空并且没有属性的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=""
[credi T:@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
。
答
另一个例子是:
<xs:complexType name="empty">
<xs:sequence/>
</xs:complexType>
<xs:element name="myEmptyElement" type="empty>
或
<xs:element name="myEmptyElement">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
或
<xs:element name="myEmptyElement">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:anyType"/>
</xs:complexContent>
</xs:complexType>
</xs:element>
这是一个SOAP Web服务,我用的soapUI测试。当soapUI自动生成请求时,它为#1产生' ',但为#2&#3产生'? web:getServerInfo>'。 –
我想知道#2是否会成为一种使用'xs:simpleType'的方式,但具有讽刺意味的是它比使用'xs:complexType'更加冗长。我也不喜欢它说内容是一个字符串,尽管是一个空字符串。 –
我猜像' '太过分了。所以,我会和#1一起去。由于我只有一个这样的元素,因此不值得定义一个命名类型。 –