XML 使用 xmllint 进行模式验证:抱怨像 <foobar/> 这样的空标签
XML schema validation with xmllint: complains about empty tags like <foobar/>
我在使用 xmllint
根据 XSD 模式验证 XML 文件时遇到问题:xmllint compains with a validity error that a tag like <foobar/>
is尽管 foobar
在 XSD 模式中定义如下:
<xs:element name="foobar" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:positiveInteger">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
比较:
根据 xmllint,<foobar>123</foobar>
有效。如果我从 XML 文件中完全删除 foobar
标记,xmllint 也不会抱怨。
问题:
那么,拒绝 <foobar/>
有什么意义?
谢谢!
P.S.: 实际错误信息:
myfile.xml:135298: element foobar: Schemas validity error : Element '{http://www.foobaz.com/namespace}foobar': '' is not a valid value of the local atomic type.
P.P.S.: xmllint 版本 20901
数字或正常值可以是正整数或空字符串。
下面是一个可能的解决方案,否则你应该使用nillable="true"
。
<xs:simpleType name="positive-integer-or-empty">
<xs:annotation>
<xs:documentation>The number-or-normal values can be either a positive integer or an empty string. This is used for the content of the ensemble element.</xs:documentation>
</xs:annotation>
<xs:union memberTypes="positive-integer-restricted">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
<xs:simpleType name="positive-integer-restricted">
<xs:restriction base="xs:positiveInteger">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
你在schema中说元素的值必须是1到9999范围内的整数,但元素的实际值是空内容。我不太明白为什么你的架构不允许这个值有任何问题。
如果要允许此范围内的整数或空内容,有两种可能的方法:
(a) 定义一个联合类型,其成员类型为 (i) 1 到 9999 范围内的整数,以及 (ii) 具有 facet length=0
或
的字符串
(b) 定义一个列表类型,其项类型为1到9999范围内的整数,列表类型有minLength=0
、maxLength=1
.
您也可以使用 nillable="true"
,但是 <foobar/>
不是有效内容,它必须是 <foobar xsi:nil="true"/>
,这(在我看来)完全违背了目的。
我在使用 xmllint
根据 XSD 模式验证 XML 文件时遇到问题:xmllint compains with a validity error that a tag like <foobar/>
is尽管 foobar
在 XSD 模式中定义如下:
<xs:element name="foobar" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:positiveInteger">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
比较:
根据 xmllint,<foobar>123</foobar>
有效。如果我从 XML 文件中完全删除 foobar
标记,xmllint 也不会抱怨。
问题:
那么,拒绝 <foobar/>
有什么意义?
谢谢!
P.S.: 实际错误信息:
myfile.xml:135298: element foobar: Schemas validity error : Element '{http://www.foobaz.com/namespace}foobar': '' is not a valid value of the local atomic type.
P.P.S.: xmllint 版本 20901
数字或正常值可以是正整数或空字符串。
下面是一个可能的解决方案,否则你应该使用nillable="true"
。
<xs:simpleType name="positive-integer-or-empty">
<xs:annotation>
<xs:documentation>The number-or-normal values can be either a positive integer or an empty string. This is used for the content of the ensemble element.</xs:documentation>
</xs:annotation>
<xs:union memberTypes="positive-integer-restricted">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
<xs:simpleType name="positive-integer-restricted">
<xs:restriction base="xs:positiveInteger">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
你在schema中说元素的值必须是1到9999范围内的整数,但元素的实际值是空内容。我不太明白为什么你的架构不允许这个值有任何问题。
如果要允许此范围内的整数或空内容,有两种可能的方法:
(a) 定义一个联合类型,其成员类型为 (i) 1 到 9999 范围内的整数,以及 (ii) 具有 facet length=0
或
(b) 定义一个列表类型,其项类型为1到9999范围内的整数,列表类型有minLength=0
、maxLength=1
.
您也可以使用 nillable="true"
,但是 <foobar/>
不是有效内容,它必须是 <foobar xsi:nil="true"/>
,这(在我看来)完全违背了目的。