xml - 在复杂类型中嵌套简单类型

标签 xml xsd

下面的代码在验证过程中出现以下错误。这是否意味着我不能在complexType 元素中嵌套simpleType 元素?

错误 - 第 18、17 行:org.xml.sax.SAXParseException;行号:18;列数:17; s4s-elt-must-match.1:“sequence”的内容必须匹配(注释?,(元素|组|选择|序列|任何)*)。发现问题始于:simpleType。

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

    <element name="carType">
        <complexType>
            <sequence>
                <simpleType name="colour">
                    <restriction base="string">
                        <enumeration value="blue" />
                        <enumeration value="yellow" />
                        <enumeration value="green" />
                        <enumeration value="black" />
                        <enumeration value="white" />
                    </restriction>
                </simpleType>
                <simpleType name="body">
                    <restriction base="string">
                        <enumeration value="sedan" />
                        <enumeration value="hatchback" />
                    </restriction>
                </simpleType>
            </sequence>
        </complexType>
    </element>
</schema>

最佳答案

你已经很接近了。只需对 XSD 进行两项小调整即可:

  1. 你在哪里<simpleType name="colour"> ,而是声明一个 colour元素:

        <element name="colour">
          <simpleType>
    
  2. 同样,你有<simpleType name="body"> , 反而 声明 body元素:

        <element name="body">
          <simpleType>
    

总而言之:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.example.org/carType"
        elementFormDefault="qualified">
  <element name="carType">
    <complexType>
      <sequence>
        <element name="colour">
          <simpleType>
            <restriction base="string">
              <enumeration value="blue" />
              <enumeration value="yellow" />
              <enumeration value="green" />
              <enumeration value="black" />
              <enumeration value="white" />
            </restriction>
          </simpleType>
        </element>
        <element name="body">
          <simpleType>
            <restriction base="string">
              <enumeration value="sedan" />
              <enumeration value="hatchback" />
            </restriction>
          </simpleType>
        </element>
      </sequence>
    </complexType>
  </element>
</schema>

关于xml - 在复杂类型中嵌套简单类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27237607/

相关文章:

c# - WCF 如何从 wsdl 和 xsd 生成服务 - Contract First

java - 如何使用相同元素和其他元素映射 XML Wrapper

java - 如何解析通用 XSD 文档

xml - ie9 iframe xml问题

android - 在运行时删除 Surface

android - onClick 时获取其他 View 的值。数据绑定(bind)

c# - 生成与 XSD 中一样具有确切命名空间的 DataContracts

java - 针对 XSD 的 XML 验证 : cvc-complex-type. 2.4.a

xml - Delphi 7 - 使用 OmniXML 库操作 XML 文件

java - 使用 ArrayList 打乱 xml 布局时出错