An Introduction to RELAX NG - Adding attributes
(Page 4 of 4 )
We've defined two elements within the schema, but what about attributes? RELAX NG actually seeks to treat elements and attributes as similarly as possible, so throwing some attributes into the mix isn't that difficult.
Say that we want to add a date attribute to the person element. It could represent the date that the entry was added into the document. The instance document might look something like this:
<people>
<person date="2008-06-20"/>
<person date="2008-06-21"/>
</people>
In order to declare an attribute in the XML schema, the attribute element is used. It's placed as a child of the definition of whatever element the attribute belongs to, just as a child element would be. Also just as with a child element, the attribute element has a name attribute which corresponds to the name of the attribute being added. If we add the attribute declaration to the XML schema, the result would look like this:
<element name="people" xmlns="http://relaxng.org/ns/structure/1.0">
<zeroOrMore>
<element name="person">
<attribute name="date"/>
</element>
</zeroOrMore>
</element>
Notice how we're able to remove the empty element without any complaints from RELAX NG. RELAX NG expects the element element to have at least one child element, but it doesn't care if it's an attribute or something such as the empty element.
You're probably able to guess what the compact schema would look like. Since, as was mentioned earlier, RELAX NG seeks to treat elements and attributes in the same way, adding an attribute is just like adding a child element. The resulting schema would look like this:
element people {
element person {
attribute date { text }
}*
}
Above, the attribute is defined within the person element. We've declared that the attribute will have text within it. However, we did not explicitly do this in the XML schema. Using the XML syntax, the attribute element is, unlike the element element, allowed to be empty. The default datatype (we'll discuss datatypes in more detail later on) will be set to text, although it's perfectly possible to specify this manually:
<attribute name="date">
<text/>
</attribute>
Above, we modify the attribute declaration, adding a text element, although it's certainly not required. Note, however, that a content type is required using the compact syntax.
That's all we have time and room for today. Check back next week for the second part of this three-part series.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |