如何扩展AIXM XML Schema以允许下划线开头的任意元素?
关于XSD中扩展AirspaceType以支持下划线开头元素的问题
需求背景
现有AIXM简化的XML Schema定义,希望扩展AirspaceType类型,允许在txtName元素之后添加任意数量、名称以下划线开头的任意元素;同时拒绝名称未以下划线开头的额外元素。
原始Schema定义
<xsd:element name="Ase" type="AirspaceType"/> <xsd:complexType name="AirspaceType"> <xsd:annotation> <xsd:documentation>Airspace</xsd:documentation> </xsd:annotation> <xsd:sequence> <xsd:element name="txtName" type="txtName" minOccurs="0"> <xsd:annotation> <xsd:documentation>Name (as published in AIP)</xsd:documentation> </xsd:annotation> </xsd:element> </xsd:sequence> <xsd:attribute ref="source"/> </xsd:complexType>
符合原始Schema的XML示例
<Ase source="foobar"> <txtName>Some Airspace</txtName> </Ase>
期望的有效扩展XML
<Ase source="foobar"> <txtName>Some Airspace</txtName> <_test>This should be valid</_test> </Ase>
期望的无效XML
<Ase source="foobar"> <txtName>Some Airspace</txtName> <test>This should NOT be valid since the element name doesn't begin with an underscore</test> </Ase>
用户尝试的错误Schema及报错
错误Schema代码
<xsd:element name="Ase" type="AirspaceType"/> <xsd:complexType name="AirspaceType"> <xsd:annotation> <xsd:documentation>Airspace</xsd:documentation> </xsd:annotation> <xsd:sequence> <xsd:element name="txtName" type="txtName" minOccurs="0"> <xsd:annotation> <xsd:documentation>Name (as published in AIP)</xsd:documentation> </xsd:annotation> </xsd:element> </xsd:sequence> <xsd:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"> <xsd:annotation> <xsd:documentation>Unofficial extension</xsd:documentation> </xsd:annotation> <xsd:restriction base="xsd:anyName"> <xsd:pattern value="_.*"/> </xsd:restriction> </xsd:any> <xsd:attribute ref="source"/> </xsd:complexType>
报错信息
element any: Schemas validity error : Element '{http://www.w3.org/2001/XMLSchema}any': This element is not expected. Expected is one of ( {http://www.w3.org/2001/XMLSchema}attribute, {http://www.w3.org/2001/XMLSchema}attributeGroup, {http://www.w3.org/2001/XMLSchema}anyAttribute ).
问题解答
可行性说明
该扩展需求是可行的,但你的实现违反了XML Schema的语法规则,需要调整结构和约束方式。
错误原因分析
- 内容模型结构错误:
xsd:any属于内容模型的一部分(定义元素序列),不能放在xsd:sequence容器之外。XML Schema规定,xsd:complexType的直接子元素中,在内容模型容器(如xsd:sequence、xsd:choice)之后只能是属性相关的定义(xsd:attribute、xsd:attributeGroup、xsd:anyAttribute),这是报错的直接原因。 - 非法子元素使用:
xsd:any元素仅允许xsd:annotation作为子元素,你在其中添加的xsd:restriction是不被Schema语法允许的,这也是导致Schema无效的关键问题。
正确实现方案
根据你使用的XML Schema版本,提供两种实现方式:
方案1:XML Schema 1.1(推荐,简洁高效)
XML Schema 1.1为xsd:any新增了name属性,可直接通过正则表达式限制元素名称。只需将xsd:any放到xsd:sequence的末尾,并指定name="_.*"即可:
<xsd:element name="Ase" type="AirspaceType"/> <xsd:complexType name="AirspaceType"> <xsd:annotation> <xsd:documentation>Airspace</xsd:documentation> </xsd:annotation> <xsd:sequence> <xsd:element name="txtName" type="txtName" minOccurs="0"> <xsd:annotation> <xsd:documentation>Name (as published in AIP)</xsd:documentation> </xsd:annotation> </xsd:element> <!-- 允许任意数量、名称以下划线开头的任意元素 --> <xsd:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded" name="_.*"/> </xsd:sequence> <xsd:attribute ref="source"/> </xsd:complexType>
方案2:XML Schema 1.0兼容实现
若环境仅支持XML Schema 1.0,需通过抽象元素+替换组的方式实现名称约束:
<xsd:element name="Ase" type="AirspaceType"/> <!-- 定义抽象元素,作为扩展元素的父类 --> <xsd:element name="_extension" abstract="true" type="xsd:anyType"/> <!-- 定义名称符合模式的替换元素,可重复使用 --> <xsd:element name="_anyExtension" substitutionGroup="_extension"> <xsd:annotation> <xsd:documentation>Unofficial extension element</xsd:documentation> </xsd:annotation> <xsd:complexType> <xsd:sequence/> <xsd:anyAttribute namespace="##any" processContents="lax"/> </xsd:complexType> <xsd:name> <xsd:pattern value="_.*"/> </xsd:name> </xsd:element> <xsd:complexType name="AirspaceType"> <xsd:annotation> <xsd:documentation>Airspace</xsd:documentation> </xsd:annotation> <xsd:sequence> <xsd:element name="txtName" type="txtName" minOccurs="0"> <xsd:annotation> <xsd:documentation>Name (as published in AIP)</xsd:documentation> </xsd:annotation> </xsd:element> <!-- 允许任意数量的扩展元素 --> <xsd:element ref="_extension" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> <xsd:attribute ref="source"/> </xsd:complexType>
内容的提问来源于stack exchange,提问作者svoop
相关产品推荐
相关产品推荐

