Home > Software engineering >  Why is XSD anyAttribute not working properly?
Why is XSD anyAttribute not working properly?

Time:04-08

I'm developing an XSD file to validate my XMLs and the anyAttribute specification doesn't work as expected, at least as how the documentation reports.

I've looked for similar questions or issues on the forum but I found no duplicate.

I want to extend the specified attributes for the menu element with the xs:anyAttribute but in the validation phase, any other nonspecified attributes return an exception.

This is my schema:

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

Here a snippet of my 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>

When I validate the XML I get this error:

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

What am I missing?

CodePudding user response:

The default value for the processContents attribute of xs:anyAttribute is strict, which requires that the attribute be defined in the XSD in order for validation to pass.

Add processContents="skip" to allow even undefined attributes.

See also

  • Related