当前位置: 首页 > 知识库问答 >
问题:

多态xml的验证问题

裴彦
2023-03-14

我们在根模式中定义了一些抽象元素,例如:

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

...然后扩展如下:

<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命令行验证xml时,我得到以下错误消息:

$ 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/root模式-ext模式

任何帮助将不胜感激:)

共有1个答案

卞安邦
2023-03-14

你眼前的问题可以通过添加

elementFormDefault="qualified"

到XSD的xs:schema元素。要获得更深入的解释,请参阅elementFormDefault在XSD中做了什么?

还有其他验证问题。如果他们的解决方案对你来说不明显,发布一个新问题,最好是一个完整的独立于非现场资源的MCVE。

 类似资料:
  • 我对XML非常陌生,我面临着验证问题,如果我的XML和XSD文件结构良好,我也希望得到一些反馈。 在根据XSD验证XML时,我经常遇到错误- 下面的XML XSD 如果你发现其中任何一个有任何错误,请随时指出。它们都有从图案到最大长度的限制。 谢谢!!!!

  • 主要内容:形式良好的 XML 文档,验证 XML 文档,XML DTD,XML Schema,一个通用的 XML 验证器拥有正确语法的 XML 被称为"形式良好"的 XML。 通过 DTD 验证的XML是"合法"的 XML。 形式良好的 XML 文档 "形式良好"的 XML 文档拥有正确的语法。 在前面的章节描述的语法规则: XML 文档必须有一个根元素 XML元素都必须有一个关闭标签 XML 标签对大小写敏感 XML 元素必须被正确的嵌套 XML 属性值必须加引号 <?xml version=

  • 我是XML新手。这是一个家庭作业。我已经尽了最大努力,但我无法让实例文档进行验证。我总是犯错误 “找不到元素'站点'的声明。 我想我可能错误地声明了一些名称空间或者schemaLocations。有人能给我指一下正确的方向吗? 作业步骤: > <块引用> 在sites.xml文件中,向根站点元素添加命名空间声明,将xs前缀与XML Schema命名空间的URI相关联。为文件指定默认命名空间http

  • 类定义为: 要加载的xml示例部分: 我创建了一个验证收集器,以显示针对架构解封xml文件过程中所有验证错误,但结果令人困惑:验证收集器将继承我的RemoteHost的每个类的验证错误显示为: 元素“host firstsubtype”不能有字符或元素信息项[children],因为该类型的内容类型为空。 元素“Other HostType”必须没有字符或元素信息项[childrens],因为该类

  • 我在验证xml文件时遇到问题。在我的xml文件中,xsi:schemaLocation=“”指向xsd文件。不幸的是,这个xsd文件加载了模式依赖项。他们都指向一些网站。因此,验证失败。所有工具和库都失败,外汇xmlspy。当我指向xml文件中的xsi:schemalocation=“”本地xsd(所有依赖项都是本地的)时,验证工作很好。 我的问题与javax.xml.validation有关,我

  • 问题内容: 我想使用Java解析XML文件,并针对XSD模式在同一步骤中对其进行验证。XML文件可能包含几种模式的内容,如下所示: 给定一个名称空间,可以提供相应的xsd文件,但是在解析之前,所使用的名称空间是未知的。如果架构定义了属性的默认值,我也想以某种方式知道这一点。 如果知道架构,我可以验证文件,无需验证就可以解析文件,并且实现了LSResourceResolver。但是,我无法使其全部协