简体   繁体   English

元素http://www.w3.org/2001/XMLSchema:complexType在此上下文中无效

[英]Element http://www.w3.org/2001/XMLSchema:complexType is invalid in this context

I'm trying to validate this XML file 我正在尝试验证此XML文件

<session>
    <mic id="1" posname="T1" x="0.0" y="0.0" z="0.0" />
</session>

using this XSD file 使用此XSD文件

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

    <xs:element name="session">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="mic" type="micType" minOccurs="1" maxOccurs="4">
                </xs:element>

                <xs:complexType name="micType">
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute type="xs:int" name="id"/>
                            <xs:attribute type="xs:string" name="posname"/>
                            <xs:attribute type="xs:float" name="x"/>
                            <xs:attribute type="xs:float" name="y"/>
                            <xs:attribute type="xs:float" name="z"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType> 
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

but I get this error message: 但我收到此错误消息:

XmlSchema error: Element http://www.w3.org/2001/XMLSchema:complexType is invalid in this context. XmlSchema错误:元素http://www.w3.org/2001/XMLSchema:complexType在此上下文中无效。

If I just have the attribute definition for the mic element the program runs fine. 如果我只有mic元素的属性定义,程序运行正常。 I don't know what I'm doing wrong. 我不知道我做错了什么。 I'm trying to have the XSD validate the data types for the mic element. 我正在尝试让XSD验证麦克风元素的数据类型。 Can anybody please tell me what I'm doing wrong? 谁能告诉我我做错了什么?

名为micType的complexType应定义为全局类型声明,即它应该是xs:schema元素的直接子节点。

What you have is a non-viable hybrid of a local and global declaration of a complex type. 你所拥有的是一种复杂类型的本地和全局声明的不可行的混合体。 Either define a micType globally (as Michael Kay mentioned) or locally: 全局定义micType (如Michael Kay所述)或本地定义:

Global complexType declaration 全局complexType声明

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           attributeFormDefault="unqualified" 
           elementFormDefault="qualified">
  <xs:element name="session">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="mic" type="micType" minOccurs="1" maxOccurs="4"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="micType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:int" name="id"/>
        <xs:attribute type="xs:string" name="posname"/>
        <xs:attribute type="xs:float" name="x"/>
        <xs:attribute type="xs:float" name="y"/>
        <xs:attribute type="xs:float" name="z"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>

Local complexType declaration 本地complexType声明

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           attributeFormDefault="unqualified" 
           elementFormDefault="qualified">
  <xs:element name="session">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="mic" minOccurs="1" maxOccurs="4">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:int" name="id"/>
                <xs:attribute type="xs:string" name="posname"/>
                <xs:attribute type="xs:float" name="x"/>
                <xs:attribute type="xs:float" name="y"/>
                <xs:attribute type="xs:float" name="z"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Either way is ok and will successfully validate your XML. 无论哪种方式都可以,并将成功验证您的XML。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从名称空间&#39;http://www.w3.org/2001/XMLSchema-instance&#39;期望元素&#39;CustomerLeads&#39; - Expecting element 'CustomerLeads' from namespace 'http://www.w3.org/2001/XMLSchema-instance' C#中的XML反序列化错误-InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> 没想到 - XML Deserialization Error in C# - InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> was not expected 使用 DataContractSerializer 时删除 xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; - remove xmlns:i=“http://www.w3.org/2001/XMLSchema-instance” when using DataContractSerializer 具有“ http://www.w3.org/2001/XMLSchema”名称空间的性能影响 - Performance impact of having “http://www.w3.org/2001/XMLSchema” namespace 避免在.Net DataContractSerializer中使用“http://www.w3.org/2001/XMLSchema-instance”命名空间 - Avoiding using the “http://www.w3.org/2001/XMLSchema-instance” namespace with .Net DataContractSerializer 删除 p2:type="&lt;<type> &gt;" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" 来自 xml</type> - Remove p2:type="<<type>>" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" from xml JWT Header 算法:“hs256”与“http://www.w3.org/2001/04/xmldsig-more#hmac-sha256”相同 - JWT Header algorithm: is "hs256" the same as "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256" DNX Core 5.0 JwtSecurityTokenHandler“ IDX10640:不支持算法:&#39;http://www.w3.org/2001/04/xmldsig-more#hmac-sha256&#39;” - DNX Core 5.0 JwtSecurityTokenHandler “IDX10640: Algorithm is not supported: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256'” SignedXml CanonicalizationMethod - http://www.w3.org/2006/12/xml-c14n11 - SignedXml CanonicalizationMethod - http://www.w3.org/2006/12/xml-c14n11 “'http://www.w3.org/XML/1998/namespace:lang' 属性未声明。” - “The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not declared.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM