【问题标题】:XML / XSD 'extension' of an element always has undefined complexType元素的 XML / XSD“扩展”始终具有未定义的 complexType
【发布时间】:2012-04-11 18:52:48
【问题描述】:

我正在尝试编写扩展在元素中定义的 ComplexType 的 XSD 架构。

我正在尝试使用 Notepad++ XMLTools 插件来解决问题,但我总是收到“无法解析架构文件”。没有错误描述,所以我一直在使用位于此处的验证器来获取更多详细信息:

http://www.xmlforasp.net/schemavalidator.aspx

我从中得到的输出是:

状态: Undefined complexType 'http://test.org:BaseClass' 用作复杂类型扩展的基础。

我尝试在 xs:schema 标记中删除 :test 命名空间,我尝试从 ClassHierarchy 中的 ref 中删除 test: 命名空间限定符,并且我尝试将命名空间添加到元素定义中,但我无法让架构通过验证。

任何帮助将不胜感激!谢谢

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    xmlns:test="http://test.org"
    targetNamespace="http://test.org"
    elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="BaseClass">
        <xs:complexType>
            <!-- More referneces in here (Meaning this must be a complex type) but removed out to make a bit simpler -->
            <xs:attribute name="test_name" type="xs:string" use="required"/>
            <xs:attribute name="second_name" type="xs:string"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="SubClass">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="test:BaseClass">
                    <xs:attribute name="min_value" type="xs:float" default="0.0"/>
                    <xs:attribute name="max_value" type="xs:float" default="1.0"/>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>


    <xs:element name="ClassHierarchy">
        <xs:complexType>
            <xs:sequence>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="test:BaseClass" minOccurs="0" maxOccurs="unbounded"/>
                    <xs:element ref="test:SubClass" minOccurs="0" maxOccurs="unbounded"/>
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

XML 文件包含:

<?xml version="1.0"?>
    <ClassHierarchy
    xmlns="http://test.org"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://test.org Test.xsd">
        <BaseClass test_name="Test1" />
        <SubClass test_name="Test2" min_value="0.0" max_value="1.0" />
    </ClassHierarchy>

【问题讨论】:

  • 我发布你的 xsd 并得到“缺少根元素” - 还需要什么来“编译”这个?
  • 抱歉,我没有添加示例 XML 文件。

标签: xml xsd ref complextype


【解决方案1】:

问题是您正在尝试将基本类型值设置为元素。但是“类型”是预期的。你应该让它更像这样:

<xs:element name="BaseClass" type="BaseType" />

<xs:complexType name="BaseType">
      ...
</xs:complexType>

<xs:complexType name="SubType">
   <xs:complexContent>
       <xs:extension base="BaseType">
           ....
       </xs:complexContent>
</xs:complexType>

【讨论】:

  • 啊!这样可行!谢谢。但是,当我尝试命名空间限定 'xmlns:test=' 时,我只能让它与默认命名空间 'xmlns=' 一起工作,即使我命名空间,架构也找不到 BaseType。
  • 不客气 :) 我没有使用过目标模式。虽然我的猜测是它在架构本身中应该是不合格的,并且可以使用 targetNamespace 值被其他文档引用。
猜你喜欢
  • 1970-01-01
  • 2020-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多