W3C XML Schema混合元素扩展:是否需显式声明mixed=true?
W3C XML Schema混合元素扩展的规范疑问与验证结果解析
核心疑问:在W3C XML Schema中扩展混合元素类型时,扩展元素是否必须显式声明mixed="true",还是该属性会从被扩展的混合元素类型隐式继承?
1. 基础测试
Schema定义(foo.xsd)
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="mixedElement" mixed="true"> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="elem"/> </xs:choice> </xs:complexType> <xs:element name="root"> <xs:complexType> <xs:complexContent> <xs:extension base="mixedElement"/> </xs:complexContent> </xs:complexType> </xs:element> </xs:schema>
这里root元素扩展了显式设置mixed="true"的mixedElement类型,但自身未声明mixed属性。
XML实例(foo.xml)
<?xml version="1.0" encoding="utf-8"?> <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="foo.xsd">abc<elem/>xyz</root>
该XML的root元素包含混合内容。
验证结果
- 使用
xmllint验证:
结果判定xmllint --noout --schema foo.xsd foo.xmlfoo.xml有效。 - 使用Python的xmlschema库验证:
提示混合内容不被允许。File "/usr/lib/python3/dist-packages/xmlschema/validators/schemas.py", line 1678, in validate raise error xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating <Element 'root' at 0x7f3592775260> with XsdGroup(model='sequence', occurs=[1, 1]): Reason: character data between child elements not allowed
修改后的验证
若为root元素的complexType添加mixed="true"声明:
<xs:element name="root"> <xs:complexType mixed="true"> <!-- added mixed="true" --> <xs:complexContent> <xs:extension base="mixedElement"/> </xs:complexContent> </xs:complexType> </xs:element>
两种验证器均判定foo.xml有效。
2. 扩展内容测试
Schema定义(bar.xsd)
在扩展中添加新元素elem2:
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="mixedElement" mixed="true"> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="elem"/> </xs:choice> </xs:complexType> <xs:element name="root"> <xs:complexType> <xs:complexContent> <xs:extension base="mixedElement"> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="elem2"/> </xs:choice> </xs:extension> </xs:complexContent> </xs:complexType> </xs:element> </xs:schema>
XML实例(bar.xml)
<?xml version="1.0" encoding="utf-8"?> <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="bar.xsd">abc<elem/>xyz<elem2>123</elem2>abc</root>
验证结果
两种验证器均判定Schema无效:
xmllint提示:bar.xsd:12: element complexType: Schemas parser error : local complex type: The content type of both, the type and its base type, must either 'mixed' or 'element-only'. WXS schema bar.xsd failed to compile- Python xmlschema库提示:
xmlschema.validators.exceptions.XMLSchemaParseError: base has a different content type (mixed=True) and the extension group is not empty:
规范符合性说明
依据W3C XML Schema 1.0规范:
- 基础测试场景:当扩展一个混合类型且未添加新的内容模型时,扩展后的类型应隐式继承
mixed="true"属性。因此xmllint的验证结果符合规范,Python xmlschema库的报错属于实现偏差。 - 扩展内容测试场景:扩展混合类型时,如果添加了新的内容组,必须显式在扩展的complexType中声明
mixed="true",否则会因基础类型为混合、扩展内容组为元素-only的内容类型冲突导致Schema编译失败。此时两个验证器的报错均符合规范。
内容的提问来源于stack exchange,提问作者max66
相关产品推荐
相关产品推荐

