tags:

views:

25

answers:

1

I have a hierarchy of objects that are transmitted via XML. The hierarchy rules are easy to express in XML Schema by stating which element may contain which child elements. A simplifed example of the XML data would be (the actual object data is contained in attributes and omitted for brevity):

<root>
  <channel>
    <router>
      <message />
      <message />
      <message />
    </router>
    <router>
      <message />
      <message />
    </router>
  </channel>
  <channel></channel>
</root>

Now I'd like to introduce "folders", i. e. nodes that just serve to structure long lists of children for easier use. If I make a generic <folder> element, it would not impose rules of its own, but inherit them from the next non-folder element up the tree.

It seems to me that I can't express that in XSD, or can I?

I can think of alternatives, like having specific folder elements for each possible rule (ugly), simply ignoring the rules in XSD and enforcing them in code (fine with me) or somehow separating object hierarchy and intermediate nodes in the XML (not quite sure how). I'd appreciate your comments on that, too, if you have any.

+1  A: 

You are right that what you want cannot be expressed in XSD. It would be possible to introduce single-level folders, though, or, more generally, n-level folders, but not arbitrarily-recursive ones (since those would have to be global elements, which then wouldn't know what they are contained in).

It is possible to express such a constraint in Schematron; in turn, it would be possible to embed the schematron constraint in an XSD appinfo element. Your validator may or may not support Schematron embedded in XSD.

Martin v. Löwis