0
votes

I am looking to create an XSD document that would validate some XML for me. Let's say, for example, that the XML documents are designed to describe books:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <comment>Bob's very first book</comment>
    <name>Bob's book</name>
    <author>Bob</author>
    <year>2009</year>
    <publisher>
        <name>Dan's book publishing enterprise</name>
        <address>123 Fake St.</address>
    </publisher>
</book>

Let's also say that I only really care about three elements - the name, the author and the year. They are mandatory and should be validated against the schema. I do not control the XML files that I receive so the order of elements should not matter and any additional elements are to be allowed to pass through XSD validation unchecked.

Following these requirements, I have tried to construct an XSD schema that would be able to do this kind of validation, but I can't get it right. The constraint that elements can be defined in any order rules out the sequence indicator. What I'm left is the all or the choice indicators. all would be the obvious choice, but it does not allow me to use the any element.

I also toyed with the idea of using this:

<include schemaLocation="year.xsd"/>
<include schemaLocation="name.xsd"/>
<include schemaLocation="author.xsd"/>
....
    <sequence>
        <any processContents="lax" minOccurs="0" maxOccurs="unbounded" />
    </sequence>

While if found in the XML file, name year and author will be validated, this does not check for mandatory elements - I cannot specify that the year, the author and the name of the book are required to pass validation.

Could anyone give me hint of how to construct an XSD document that will validate a number of unordered mandatory elements and will still allow undefined elements in the XML file to pass validation?

Thanks!

1

1 Answers

1
votes

I am quite sure that you can't do this with the XML Schema. Suggestions:

  • Prepare you document for validation with XSLT.
  • Use Schematron.