xml - XSD 错误 : No matching global declaration available for the validation root

标签 xml xsd xml-namespaces xsd-validation xml-validation

当我尝试验证 XML 文件时,出现以下错误:

person.xml:3: element person: Schemas validity error : Element {http://www.namespace.org/person}person: No matching global declaration available for the validation root.

这是 person.xml 文件的内容:

<?xml version="1.0"?>
<p:person xmlns:p="http://www.namespace.org/person">
  <firstName>name</firstName>
  <surName>surname</surName>
  <secondName>n2</secondName>
</p:person>

这是 person.xsd 文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:p="http://www.namespace.org/person"   
           version="1.0">

    <xs:element name="person">
        <xs:complexType>
            <xs:group ref="credential"/>
        </xs:complexType>
    </xs:element>

    <xs:group name="credential">
        <xs:sequence>
            <xs:element name="firstName" type="xs:string"/>
            <xs:element name="surName" type="xs:string"/>
            <xs:element name="secondName" type="xs:string"/>
        </xs:sequence>
    </xs:group>     
</xs:schema>

最佳答案

该错误表明在给定的 XSD 中找不到命名空间元素 {http://www.namespace.org/person}person,因为 p元素不在 http://www.namespace.org/person 命名空间中。通过将 targetNamespace="http://www.namespace.org/person" 添加到 XSD 的 xs:schema 根元素来更正问题。接下来,调整对 credential 组的引用以也使用该命名空间。

总而言之,以下 XML 对以下 XSD 有效:

XML

<?xml version="1.0"?>
<p:person xmlns:p="http://www.namespace.org/person">
  <firstName>name</firstName>
  <surName>surname</surName>
  <secondName>n2</secondName>
</p:person>

XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:p="http://www.namespace.org/person"
  targetNamespace="http://www.namespace.org/person"
  version="1.0">
  
  <xs:element name="person">
    <xs:complexType>
      <xs:group ref="p:credential"/>
    </xs:complexType>
  </xs:element>
  
  <xs:group name="credential">
    <xs:sequence>
      <xs:element name="firstName" type="xs:string"/>
      <xs:element name="surName" type="xs:string"/>
      <xs:element name="secondName" type="xs:string"/>
    </xs:sequence>
  </xs:group>  

</xs:schema>

关于xml - XSD 错误 : No matching global declaration available for the validation root,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64839082/

相关文章:

wpf - Powershell 中的自定义 XAML 命名空间

c# - 如何解析节点名称中包含无效字符的XML?

c# - 如何在 XML soap 消息中发送 "Raw XML"; C#、.Net 网络服务

java - 如何检索从 Web 服务发送的值

xml - 在 XSD 中定义逗号分隔值

java - 为什么此代码会抛出 MalformedURLException?

jsf - javax.faces.view.facelets.FaceletException : Error Parsing/my. xhtml:错误跟踪 [行:42] 元素 "f"的前缀 "f:facet"未绑定(bind)

javascript - 将图像从 XML 显示为 HTML

xml - 批量转换 xslt xml

c++ - XSD 生成的解析器中的 gSOAP 替代方案