Basically my question is whether or not there is way to validate a non-root
element according to a given XSD schema.
Theoretically speaking, yes, Section 5.2 Assessing Schema-Validity of the XSD Recommendation allows for non-root element validity assessment. (Credit and thanks to C. M. Sperberg-McQueen for pointing this out.)
Practically speaking, no, not directly. XML Schema tools tend to validate against XML documents — not selected, individual elements — and XML documents must have a single root element. Perhaps you might embed the element you wish to validate within a rigged context that simulates the element's ancestry up to the proper root element for the document.
If I reference the root element in foo.xsd it works.
The root element in foo.xsd had better be xsd:schema
, actually. You probably mean to say
If I reference an element defined globally (under
xsd:schema
) in foo.xsd it works.
That is also how @ref
is defined to work in XML Schema — it cannot reference locally defined elements such as foo:booIsNotRoot
. Furthermore, any globally defined element can serve as a root element. (In fact, only globally defined elements can serve as a root element.)
So, your options include:
- Rig a valid context in which to test your targeted element.
- Make your targeted element globally defined in its XSD.
- Identify an implementation-specific interface to your XSD validator that provides element-level validation.