【问题标题】:XSD error: No matching global declaration available for the validation rootXSD 错误:没有可用于验证根的匹配全局声明
【发布时间】:2021-02-26 12:48:46
【问题描述】:

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

person.xml:3:元素 person:架构有效性错误:元素 {http://www.namespace.org/person}person:没有可用于验证根的匹配全局声明。

这是 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>

【问题讨论】:

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


    【解决方案1】:

    错误表明命名空间元素 {http://www.namespace.org/person}person 在给定 XSD 中找不到,因为 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-19
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多