You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多态XML验证求助:Saxon报命名空间不匹配错误

XML Schema验证错误解决方案

定义的Schema元素

根Schema抽象类型

<xs:complexType name="Abstract1" abstract="true">
   <xs:sequence minOccurs="0">
      <xs:element name="absName1" type="xs:string" minOccurs="0"/>
   </xs:sequence>
</xs:complexType>

扩展Schema类型

<xs:complexType name="Extension1">
   <xs:complexContent>
      <xs:extension base="root:Abstract1">
         <xs:sequence>
            <xs:element name="extName1" type="xs:string" minOccurs="0"/>
         </xs:sequence>
      </xs:extension>
   </xs:complexContent>
</xs:complexType>

XML实例文件

<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://example.com"
    xmlns:ext="https://ext.example.com"
    xsi:schemaLocation="https://example.com ../xsd/root.xsd
      https://ext.example.com ../xsd/ext1.xsd">
    <test1
        testTC="Test Case 1.1 - tag injection with same names using type casting, abstract type">
        <extension1 xsi:type="ext:Extension1">
            <absName1>Test Case 1.1 - absName1 from Abstract1 Type inhereted by Extension1</absName1>
            <ext:extName1>Test Case 1.1 - extName1 from Extension1</ext:extName1>
        </extension1>
        <extension1 xsi:type="ext:Extension11">
            <absName1>Test Case 1.1 - absName1 from Abstract1 Type inhereted by Extension11</absName1>
            <ext:extName11>Test Case 1.1 - extName11 from Extension11</ext:extName11>
        </extension1>
    </test1>
</root>

验证错误信息

执行Saxon验证命令:

$ java com.saxonica.Validate --multipleSchemaImports:on -s:tc1.xml -xsd:"../xsd/ext1.xsd;../xsd/root.xsd"

返回错误:

Validation error on line 7 column 92 of tc1.xml:
  FORG0001: In content of element <root>: The content model does not allow element <test1>
  to appear as the first child. The element is in namespace https://example.com but it
  should be in no namespace. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 9 column 14 of tc1.xml:
  FORG0001: In content of element <extension1>: The content model does not allow element
  <absName1> to appear as the first child. The element is in namespace https://example.com
  but it should be in no namespace. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 13 column 14 of tc1.xml:
  FORG0001: In content of element <extension1>: The content model does not allow element
  <absName1> to appear as the first child. The element is in namespace https://example.com
  but it should be in no namespace. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 20 column 17 of tc1.xml:
  FORG0001: In content of element <extension2>: The content model does not allow element
  <nonAbsName2> to appear as the first child. The element is in namespace
  https://example.com but it should be in no namespace. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 24 column 17 of tc1.xml:
  FORG0001: In content of element <extension2>: The content model does not allow element
  <nonAbsName2> to appear as the first child. The element is in namespace
  https://example.com but it should be in no namespace. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Error on line 28 column 8 of tc1.xml:
  XQDY0027  Five validation errors were reported. First error: In content of element <root>:
  The content model does not allow element <test1> to appear as the first child. The element
  is in namespace https://example.com but it should be in no namespace. 
Validation of file tc1.xml completed: errors found

解决方案

错误核心是XML元素的命名空间与Schema定义的元素命名空间不匹配:XML中默认命名空间为https://example.com,但Schema未声明目标命名空间,导致元素被识别为无命名空间。

1. 修正根Schema(root.xsd)

添加目标命名空间、元素限定配置,并补全XML中用到的元素声明:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="https://example.com"
           xmlns:root="https://example.com"
           elementFormDefault="qualified">
  <!-- 抽象类型定义 -->
  <xs:complexType name="Abstract1" abstract="true">
     <xs:sequence minOccurs="0">
        <xs:element name="absName1" type="xs:string" minOccurs="0"/>
     </xs:sequence>
  </xs:complexType>

  <!-- 根元素及子元素声明 -->
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="test1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="extension1" type="root:Abstract1" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="testTC" type="xs:string"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

2. 修正扩展Schema(ext1.xsd)

添加目标命名空间、元素限定配置,并导入根Schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="https://ext.example.com"
           xmlns:ext="https://ext.example.com"
           xmlns:root="https://example.com"
           elementFormDefault="qualified">
  <!-- 导入根Schema -->
  <xs:import namespace="https://example.com" schemaLocation="../xsd/root.xsd"/>

  <!-- Extension1类型定义 -->
  <xs:complexType name="Extension1">
     <xs:complexContent>
        <xs:extension base="root:Abstract1">
           <xs:sequence>
              <xs:element name="extName1" type="xs:string" minOccurs="0"/>
           </xs:sequence>
        </xs:extension>
     </xs:complexContent>
  </xs:complexType>

  <!-- Extension11类型定义(XML中用到) -->
  <xs:complexType name="Extension11">
     <xs:complexContent>
        <xs:extension base="root:Abstract1">
           <xs:sequence>
              <xs:element name="extName11" type="xs:string" minOccurs="0"/>
           </xs:sequence>
        </xs:extension>
     </xs:complexContent>
  </xs:complexType>
</xs:schema>

3. 重新执行验证

保持原验证命令不变,即可通过验证:

java com.saxonica.Validate --multipleSchemaImports:on -s:tc1.xml -xsd:"../xsd/ext1.xsd;../xsd/root.xsd"

内容的提问来源于stack exchange,提问作者FedSic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 22:54:18