No matching global declaration available for the validation root
Asked Answered
S

3

62

Background

Validate an XML document using a schema.

Problem

The simplest form of the problem is shown in two files.

XML Document file.xml

<?xml version="1.0"?>
<recipe xmlns:r="http://www.namespace.org/recipe">
  <r:description>
    <r:title>sugar cookies</r:title>
  </r:description>
</recipe>

XSD Document schema.xsd

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
   version="1.0"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:r="http://www.namespace.org/recipe">

  <xsd:complexType name="recipe">
    <xsd:choice>
      <xsd:element name="description" type="descriptionType"
        minOccurs="1" maxOccurs="1" />
    </xsd:choice>
  </xsd:complexType>

  <xsd:complexType name="descriptionType">
    <xsd:all>
      <xsd:element name="title">
        <xsd:simpleType>
          <xsd:restriction base="xsd:string">
            <xsd:minLength value="5" />
            <xsd:maxLength value="55" />
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:element>
    </xsd:all>
  </xsd:complexType>
</xsd:schema>

Error

The full error message from xmllint:

$ xmllint --noout --schema schema.xsd file.xml

file.xml:2: element recipe: Schemas validity error : Element 'recipe': No matching global declaration available for the validation root. file.xml fails to validate

Question

What is the correct syntax (or what schema attributes are missing) to ensure that the given schema can be used to successfully validate the given XML document?

Spirogyra answered 8/12, 2011 at 4:27 Comment(0)
M
32

You need to change your XML instance. Your current one says that there is a type called description in the namespace http://www.namespace.org/recipe. However, in your XSD definition, the only types exposed in that namespace are called recipe and descriptionType.

So either define a type called description in the XSD schema, or change your instance so you are referencing the recipe type correctly:

File ns-changed.xml:

<?xml version="1.0"?>
<r:recipe xmlns:r="http://www.namespace.org/recipe">
  <description>
    <title>sugar cookies</title>
  </description>
</r:recipe>

UPDATE This is only half the solution - the other half is in @Aravind's answer here: https://mcmap.net/q/321256/-no-matching-global-declaration-available-for-the-validation-root

Mimi answered 8/12, 2011 at 8:19 Comment(1)
I believe that description, title etc. should also have namespace (r:description etc.).Tassel
D
21

Make recipe global

Only global element definitions can be used as root elements. Your schema only has complex types and hence the error.

Change the <xsd:complexType name="recipe"> to <xsd:element name="recipe"><xsd:complexType>:

Schema file: recipe-now-global.xsd

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
   version="1.0"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:r="http://www.namespace.org/recipe">

  <xsd:element name="recipe">
    <xsd:complexType>
      <xsd:choice>
        <xsd:element name="description" type="descriptionType"
          minOccurs="1" maxOccurs="1" />
      </xsd:choice>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="descriptionType">
    <xsd:all>
      <xsd:element name="title">
        <xsd:simpleType>
          <xsd:restriction base="xsd:string">
            <xsd:minLength value="5" />
            <xsd:maxLength value="55" />
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:element>
    </xsd:all>
  </xsd:complexType>
</xsd:schema>

Read more about this here

NOTE: This still won't validate, because you have an unreleated namespace error...

$ cat file.xml | xmllint --noout --schema recipe-now-global.xsd -
-:3: element description: Schemas validity error : Element '{http://www.namespace.org/recipe}description': This element is not expected. Expected is ( description ).
- fails to validate

...but it WILL validate if you ignore that namespace error quick-and-dirty like:

$ cat file.xml | sed 's/r://g' | xmllint --noout --schema recipe-now-global.xsd -
- validates

See tom redfern's answer for details on that namespace problem.

Dd answered 8/12, 2011 at 4:31 Comment(1)
Add elementFormDefault="qualified" to your xs:schema element. This means that locally-declared elements in the schema are assumed to be in the target namespace. This should have been the default, and you should think of it as boilerplate - the opposite scenario, where local elements are in no namespace, is very rare.Jobholder
C
12

In my practice, I got the No matching global declaration available for the validation root in two cases:

  • If XSD does not contain an <xsd:element name="recipe" .../> explained in @aravind-r-yarram's answer.

  • If <recipe/> in XML does not contain an xmlns attribute. In such case adding the xmlns will help:

      <recipe xmlns="http://www.namespace.org/recipe">
          ...
      </recipe>
    
Cipango answered 5/2, 2018 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.