【发布时间】:2015-04-12 07:09:57
【问题描述】:
我正在尝试创建一个 XSD 来验证 XML 文件,并且有几个元素需要具有属性但没有文本。
例如这应该被认为是有效的:
<DEPRECATED value="blah"/>
这些是无效的:
<DEPRECATED>blah</DEPRECATED>
<DEPRECATED value="blah">bad text</DEPRECATED>
我尝试按照here 的描述声明一个复杂的空元素,但要么我对示例的解释不正确,要么示例本身有误(请参阅下面的错误):
<xs:element name="DEPRECATED" type="stringValue" minOccurs="0" maxOccurs="1"/>
<xs:complexType name="stringValue">
<xs:complexContent>
<xs:restriction base="xs:integer">
<xs:attribute name="value" type="xs:string"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
错误:类型“stringValue”的复杂类型定义表示错误。使用时,基类型必须是 complexType。 'integer' 是一个简单类型。
我也尝试过这样定义 complexType:
<xs:complexType name="stringValue">
<xs:attribute name="value" type="xs:string"/>
</xs:complexType>
但这认为上面的无效示例是有效的。 [编辑:更正,上述确实有效。]
我也尝试过类似问题的答案(Define an XML element that must be empty and has no attributes、Prevent Empty Elements in XML via XSD),但没有运气。
如何验证元素是否具有属性,但没有文本?
【问题讨论】:
-
Argh - 答案就在问题中。上面定义的 complexType 可以工作,我的 XML 文件有问题。
标签: xml xsd xsd-validation