【问题标题】:using xml type attribute for derived complex types对派生的复杂类型使用 xml 类型属性
【发布时间】:2011-02-19 02:05:03
【问题描述】:

我正在尝试从 xsd 架构中的基本类型获取派生的复杂类型。

当我这样做时效果很好(灵感来自this):

xml 文件:

     <person
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:type="Employee">
        <name>John</name>
        <height>59</height>
        <jobDescription>manager</jobDescription>
     </person>

xsd 文件:

     <xs:element name="person" type="Person"/>

         <xs:complexType name="Person" abstract="true">
         <xs:sequence>
            <xs:element name= "name" type="xs:string"/>
            <xs:element name= "height" type="xs:double" />
         </xs:sequence>
         </xs:complexType>

         <xs:complexType name="Employee">
         <xs:complexContent>
             <xs:extension base="Person">
                <xs:sequence>
                   <xs:element name="jobDescription" type="xs:string" />
                </xs:sequence>
              </xs:extension>
          </xs:complexContent>
         </xs:complexType>

但是,如果我想在其中包含 person 元素,例如,另一个复杂类型的序列,它就不再起作用了:

xml:

    <staffRecord>
      <company>mycompany</company>
      <dpt>sales</dpt>
      <person
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:type="Employee">
        <name>John</name>
        <height>59</height>
        <jobDescription>manager</jobDescription>
      </person>
    </staffRecord>

xsd 文件:

<xs:element name="staffRecord">
    <xs:complexType>
    <xs:sequence>
        <xs:element name="company" type="xs:string"/>
        <xs:element name="dpt" type="xs:string"/>

        <xs:element name="person" type="Person"/>

            <xs:complexType name="Person" abstract="true">
             <xs:sequence>
                <xs:element name= "name" type="xs:string"/>
                <xs:element name= "height" type="xs:double" />
             </xs:sequence>
            </xs:complexType>

            <xs:complexType name="Employee">
             <xs:complexContent>
                <xs:extension base="Person">
                   <xs:sequence>
                      <xs:element name="jobDescription" type="xs:string" />
                   </xs:sequence>
                 </xs:extension>
             </xs:complexContent>
            </xs:complexType>


    </xs:sequence>
    </xs:complexType>
</xs:element>

当使用 xmllint(在 linux 下)使用该架构验证 xml 时,我会收到以下错误消息:

config.xsd:12: 元素复杂类型: 模式解析器错误:元素 '{http://www.w3.org/2001/XMLSchema}序列': 内容无效。预期是 (注释?,(元素 | 组 | 选择 |序列 |任何)*)。 WXS 架构 config.xsd 编译失败

知道有什么问题吗?

大卫

【问题讨论】:

    标签: xml schema


    【解决方案1】:

    XML 文件的根元素应该在命名空间中定义所有复杂类型。

    http://www.datypic.com/books/defxmlschema/chapter13.html

    【讨论】:

    • 没有必要将类型的定义与元素的定义分开。如果给定的类型只在一个元素中使用,那么它只能在该元素中定义。
    【解决方案2】:

    xsd:sequence 元素只能包含以下子元素列表之一: 最多一个注解,以及任意数量的元素、组、选择、序列、任意,随你的便。

    你的序列元素中有两个 complexType 元素,这是无效的。

    编辑: 详情请见http://www.w3.org/TR/xmlschema-1/#element-sequence

    编辑 2: sequence元素的序列,而不是类型的序列。不存在多态性。声明元素 X 包含序列 A、B 以及任何看起来有点像 C 的东西是不够的,您必须准确说明您想要哪个类 C 元素。

    您的选择包括,

    a) 在根级别(即 xs:schema 下)定义与 Person 和 Employee 类型对应的 element 元素,并在 staffRecord 下用element[@ref] 引用它们

    b) 将两个 complexType 元素包装在两个单独的 element 元素中。

    【讨论】:

    • 是的,我能读懂错误信息……但这无助于解决问题
    • 你问出了什么问题,我的回答是完整的。它确实有助于解决问题,您所要做的就是从允许的元素列表中选择一个元素,然后使用它。我现在更新了我的答案,给出了两种可能的解决方案。
    • 所以,如果有人发布类似“我得到一个'编译错误',怎么了?”,你会回复“它发生是因为你的编译器遇到错误”......非常有用
    • 这取决于。有了这个确切的问题,那么是的。对于像您这样的问题,我将(就像我为您所做的那样)尝试帮助您解释该错误消息,因为您似乎无法理解它。这是一个问答网站,您提出问题并获得这些问题的答案。如果您想获得其他问题的答案,请改为提出该问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    相关资源
    最近更新 更多