【问题标题】:XSD for XML containing XSD用于包含 XSD 的 XML 的 XSD
【发布时间】: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


    【解决方案1】:

    XML 文档实例与其关联的 XSD 之间的链接是通过 xsi:schemaLocationxsi:noNamespaceSchemaLocation 建立的。

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

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

    【讨论】:

    • 虽然显然不是我希望的答案,但感谢您清楚地解释为什么这是不可能的。
    猜你喜欢
    • 2012-07-28
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2012-06-11
    • 2021-12-23
    • 2013-09-02
    • 2013-03-19
    相关资源
    最近更新 更多