甚至可能吗?
例:
<root children="2">
<child />
<child />
</root>
答案 0 :(得分:6)
XSD 1.1允许您表达这种约束:
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="child" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="children" type="xs:integer"/>
</xs:complexType>
<xs:assert test="@children = count(child)"/>
</xs:element>
XSD 1.1目前在Saxon和Xerces中实施。
答案 1 :(得分:5)
W3C Schema 1.0无法根据实例文档约束属性值。
Schematron是验证文档是否符合此类自定义验证方案的绝佳工具。
例如:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<pattern>
<rule context="root[@children]">
<assert
id="children-value"
test="@children=count(child)"
flag="error">
The root/@children value must be equal to the number of child elements.
</assert>
</rule>
</pattern>
</schema>