XSD 用于包含 XSD 的 XML [英] XSD for XML containing XSD

查看:28
本文介绍了XSD 用于包含 XSD 的 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为描述复杂模块化系统组件功能的 XML 文档创建了 XML 模式.在那个 XML 文档中,我想包含 XML Schema,它将被读取和解析以允许配置.

I have created XML Schema for an XML document that describes functionality of components of a complex modular system. Within that XML document, I want to include XML Schema which will be read and parsed to allow for configuration.

meta-schema.xsd(为简洁起见进行了大量编辑):

meta-schema.xsd (heavily edited for brevity):

<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(由开发模块的人编写):

The desired XML (written by folks developing modules):

<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 表达吗?如果是这样,如何?

Is this possible to express in XSD? If so, how?

根据kjhughes 的评论,这是不可能的.我的解决方案是使用带有 ##other 命名空间的 any 元素,并带有注释:

Based on kjhughes's comment, this is not possible. My solution was to use an any element with an ##other namespace, with a comment:

<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:schemaLocationxsi:noNamespaceSchemaLocation.

The link between an XML document instance and its associated XSD is established via xsi:schemaLocation or xsi:noNamespaceSchemaLocation.

尝试将 XSD 物理地包含在 XSD 中是非常非常规的.(没有与 XML DTD 的内部子集等效的 XSD.)

It would be highly unconventional to attempt to physically include the XSD within the XSD. (There is no XSD equivalent to XML DTD's internal subset.)

当然,您总是可以添加 xs:anyURI 类型的属性或元素来表达这种连接,但是这种关联对于 XML/XSD 语义来说是不透明的.您还可以使用 外部实体 来合并任何文件,包括XSD,但同样,这是一种文件级机制,而不是 XML/XSD 级机制.

You can, of course, always add an attribute or element of type xs:anyURI to express such a connection, but that association will be opaque to XML/XSD semantics. You could also use external entities to incorporate any file, including an XSD, but, again, this is a file-level, not an XML/XSD-level mechanism.

这篇关于XSD 用于包含 XSD 的 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆