Pages

Monday, May 23, 2011

Give different users flexibility when choosing elements vs. attributes using the RELAX NG schema

If you want to provide options for using either elements or attributes in a given instance, you can use the choice mechanism in RELAX NG (RNG):

element event {
  (element date {text} | attribute {text})
}

The <event> element above allows the author to either insert a element containing text or treat the element as an empty tag with a "date" attribute:

<event>
  <date>2004-10-15</date>
</event>

Or

<event date="2004-10-15"/>

You can't do this with either DTDs or XSD. Why would you want to give this option? In some situations, you may find that clients differ between using elements or attributes. One client may not want to process any attributes, preferring instead to put everything in elements, while another may prefer mixing elements and attributes in a more traditional way. In RNG, you can cover both methods in the same schema.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.