【发布时间】:2019-03-16 09:53:14
【问题描述】:
我为描述复杂模块化系统组件功能的 XML 文档创建了 XML 模式。在那个 XML 文档中,我想包含 XML Schema,它将被读取和解析以允许配置。
meta-schema.xsd(为简洁起见进行了大量编辑):
<xs:schema targetNamespace="urn:project"
xmlns="urn:project"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Schema" type="SchemaType"/>
<xs:complexType name="SchemaType">
<xs:sequence>
<xs:element type="ConfigurationType" name="Configuration"/>
<xs:any maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ConfigurationType">
<xs:sequence>
<xs:element ref="xs:schema"/> <!-- Does not work -->
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:schema>
所需的 XML(由开发模块的人编写):
<Schema xmlns="urn:project"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:project meta-schema.xsd">
<!-- Snip -->
<Configuration>
<schema>
<element name="enable" type="boolean">
<annotation>
<appinfo>Usage:</appinfo>
<documentation xml:lang="en">
Enable functionality
</documentation>
</annotation>
</element>
</schema>
</Configuration>
</Schema>
这可以用 XSD 表达吗?如果有,怎么做?
编辑:
基于kjhughes's comment,这是不可能的。我的解决方案是使用带有 ##other 命名空间的 any 元素,并带有注释:
<xs:complexType name="ConfigurationType">
<xs:choice>
<xs:element name="hex" type="xs:hexBinary"/>
<xs:element name="base64" type="xs:base64Binary"/>
<!-- If configuration isn't binary, modules should create an XML Schema of the configuration options in order to
facilitate future tooling, when feasible. -->
<xs:any namespace="##other" processContents="lax"/>
</xs:choice>
<xs:anyAttribute/>
</xs:complexType>
启用以下 XML:
<Schema xmlns="urn:project"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:project meta-schema.xsd">
<!-- Snip -->
<Configuration>
<xs:schema targetNamespace="urn:project"
xmlns="urn:project"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="enable" type="xs:boolean">
<xs:annotation>
<xs:appinfo>Usage:</xs:appinfo>
<xs:documentation xml:lang="en">
Enable left radial pulse functionality
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:schema>
</Configuration>
</Schema>
【问题讨论】:
标签: xml xsd xsd-validation xml-validation