views:

71

answers:

3

Hi,

I have a document with a bunch of elements under a single root element which has a few elements I care about, and want to validate; but those elements may occur anywhere in the document.

There are also other elements that I don't want to validate; I don't care if they're there or what's in 'em.

I tried the obvious thing, an XSD with the elements I care about in an 'all' element, and following that an 'any' element, but this isn't allowed.

I've considered:

  • including the other elements in my schema - yuk.
  • transforming the document to get rid of the other elements - woah, overkill.

There must be a way to do this, right?

If we need an example:

<book>
  <title/>
  <author/>
  <editor/>
  <pubdate/>
</book>

For the example, I would want to check that title and editor are present, and ignore author and pubdate. Note, the sequence of elements my not be the same.

+1  A: 

You would have to create a schema that will validate all instance documents that you feel should be valid. For instance, you might need to use a <choice> element to indicate that one of several elements must be present. You would want to include author and pubdate in the schema, but indicate that you don't care about their contents or attributes, and that they should be optional.

John Saunders
A: 

With your requirments ("a few elements I care about, and want to validate; but those elements may occur anywhere in the document") Schematron is probably better than XSD.

bortzmeyer
+2  A: 

I wound up using an XSLT transform to eliminate the fields I'm not interested in first.

Then, my XMLSchema doesn't need to have the element, and just has the elements I am interested in, in an ... element.

XSL transform doesn't seem to add any perceptible time to the process. If I see any performance problems in future, I'll probably incorporate the interested-elements-filter into the API I'm calling.

Works a treat.

Brabster