
[英]Validating XML against XSD containing xsd:import without location
[英]XSD for XML containing XSD
我已经为描述复杂的模块化系统组件功能的XML文档创建了XML Schema。 在该XML文档中,我想包含XML Schema,该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(由开发人员编写的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的评论 ,这是不可能的。 我的解决方案是使用带有##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之间的链接是通过xsi:schemaLocation
或xsi:noNamespaceSchemaLocation
。
试图将XSD物理地包含在XSD中是非常不常规的。 (没有XSD等效于XML DTD的内部子集。)
当然,您始终可以添加类型为xs:anyURI
的属性或元素来表示这种连接,但是这种关联对XML / XSD语义是不透明的。 您还可以使用外部实体来合并任何文件,包括XSD,但是同样,这是文件级的,而不是XML / XSD级的机制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.