为什么 XSD anyAttribute 无法正常工作?

Why is XSD anyAttribute not working properly?

我正在开发一个 XSD 文件来验证我的 XML 并且 anyAttribute 规范没有按预期工作,至少正如文档所报告的那样。

我在论坛上查找过类似的问题或问题,但没有找到重复的问题。

我想用 xs:anyAttribute 扩展 menu 元素的指定属性,但在验证阶段,任何其他未指定的属性 return 都是例外。

这是我的架构:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="https://www.github.com/overit-official/schemas" targetNamespace="https://www.github.com/overit-official/schemas">

  <xs:element name="menu">
    <xs:complexType>
      <xs:sequence>
        <xs:any minOccurs="0" maxOccurs = 'unbounded'></xs:any>
      </xs:sequence>
      <xs:attribute name="layer" type="xs:string" use="required"/>
      <xs:attribute name="label" type="xs:string"/>
      <xs:attribute name="icon" type="xs:string"/>
      <xs:attribute name="priority" type="xs:string"/>
      <xs:anyAttribute/>
    </xs:complexType>
  </xs:element> 

</xs:schema>

这是我的 XML 的片段:

<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns="https://www.github.com/overit-official/schemas" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="https://www.github.com/overit-official/schemas https://www.github.com/overit-official/schemas/geocall-menu.xsd"
      layer="" 
      label="Main menu" 
      icon="../r/std/icons/cartelle64.png" 
      mycustom="foo" 
      priority="3">
...
</menu>

当我验证 XML 我得到这个错误:

cvc-complex-type.3.2.2: Attribute 'mycustom' is not allowed to appear in element 'menu'.

我错过了什么?

xs:anyAttributeprocessContents 属性的默认值为 strict,这需要在 XSD 中定义该属性才能通过验证。

添加 processContents="skip" 以允许甚至未定义的属性。

另见

  • processContents strict vs lax vs skip for xsd:any