我想创建一个带XSD的XML条件验证。我想如果元素Unit是“uri”,那么元素Value必须包含“http”,否则如果元素Unit是“date”,那么元素Value必须是一个时间戳,依此类推......我从一个简单的验证使用xs:assert并且不起作用。我已经测试了xs:assertion,但它产生了相同的错误。声明包含XSD
xmllint --noout --schema metadata.xsd metadata.xml
metadata.xsd:46: element assert: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}complexType': The content is not valid. Expected is (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))).
WXS schema metadata.xsd failed to compile
。
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="metadata">
<xs:complexType>
<xs:sequence>
<xs:element name="AVU" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Target" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="1088"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Attribute">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="2700"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Value">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="http://.*"/>
<xs:maxLength value="2700"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Unit">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="uri|string|integer|date|float"/>
<xs:maxLength value="250"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:assert test="Unit = 'uri'" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
当心! ''只在XML Schema 1.1中可用。版本1.0中的模式(隐式)。还请检查xmllint是否能够处理XSD v1.1 .... –
potame