xsd 验证同时抱怨缺少属性和错误属性

xsd validation complains about missing attribute and wrong attribute at the same time

我从 xsd 验证中得到了一些精神分裂症的行为。此 link 显示 xml 和 xsd + 在线模式验证器中的错误。当我 运行 在本地使用 xmllint

xmllint --noout --nonet --schema devhelp2.xsd tester.devhelp2

我收到类似的警告:

tester.devhelp2:5: element sub: Schemas validity error : Element '{urn:devhelp}sub', attribute 'name': The attribute 'name' is not allowed.
tester.devhelp2:5: element sub: Schemas validity error : Element '{urn:devhelp}sub', attribute 'link': The attribute 'link' is not allowed.
tester.devhelp2:5: element sub: Schemas validity error : Element '{urn:devhelp}sub': The attribute '{urn:devhelp}name' is required but missing.
tester.devhelp2:5: element sub: Schemas validity error : Element '{urn:devhelp}sub': The attribute '{urn:devhelp}link' is required but missing.

但这暗示命名空间有问题。

PS:

我可以通过完全删除 xmlns 来验证它(取自 zvon.org). See here for the new online validator example - 不过我仍然想了解它,难道没有解决方案保持 xmlns?

简化示例

我将您的示例简化为遵循 XML 模式。

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="urn:devhelp"
  xmlns="urn:devhelp"
  elementFormDefault="qualified">

  <xsd:attribute name="title" type="xsd:string"/>

  <xsd:element name="book">
    <xsd:complexType>
      <xsd:attribute ref="title" use="required"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

和这个XML

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<book xmlns="urn:devhelp" title="tester Reference Manual"/>

验证错误

Not valid.
Error - Line 2, 60: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 60; cvc-complex-type.3.2.2: Attribute 'title' is not allowed to appear in element 'book'.
Error - Line 2, 60: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 60; cvc-complex-type.4: Attribute 'title' must appear on element 'book'.

相互矛盾的验证错误很好地表明命名空间存在问题。

规范:XML 1.0

中的命名空间

规格说明

6.2 Namespace Defaulting

... Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.

第一个子句说明属性不会继承元素的默认命名空间声明。因此 /book/@title 没有命名空间,而您的 XML 模式需要命名空间 urn:devhelp.

title 属性

第二个条款很棘手,因为它很容易被误解。它只是说属性不需要命名空间,因为它们可以根据周围的元素以不同的方式使用。

一个例子也提到了这种行为:

6.3 Uniqueness of Attributes

... However, each of the following is legal, the second because the default namespace does not apply to attribute names:
...
<x xmlns:n1="http://www.w3.org" xmlns="http://www.w3.org" >
  <good a="1" b="2" />
  <good a="1" n1:a="2" />
</x>

解决方案

为属性明确设置命名空间。

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<book xmlns="urn:devhelp" xmlns:mine="urn:devhelp" mine:title="tester Reference Manual"/>

或避免 complexType 之外的属性定义:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns="http://www.w3.org/2001/XMLSchema"
  xmlns:mine="urn:devhelp"
  targetNamespace="urn:devhelp"
  elementFormDefault="qualified">

  <xsd:element name="book">
    <xsd:complexType>
      <xsd:attribute name="title" type="xsd:string" use="required"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>