Pages

Thursday, February 9, 2012

Create reusable attribute groups in your XML DTD

In XML DTDs, you can set up reusable attribute groups for common attributes by creating a parameter entity and referencing it in an attribute list. For example, you can set up an ID for a topic, with link options to parent elements and child elements.

<!ENTITY % id-group "id ID #REQUIRED
  parent IDREF #IMPLIED
  children IDREFS #IMPLIED" >

The ID keyword in the id attribute serves as a flag to the XML parser indicating that the ID value must be unique. No duplicate values are allowed. The IDREF keyword on the parent attribute allows a single reference to the ID of the parent of the current element. The IDREFS keyword on the children attribute allows references to the IDs of multiple child elements (each value must be separated by a space). The last two attributes are #IMPLIED, meaning they don't have to be used.

Now you can use this parameter entity as the definition of the attribute list (ATTLIST) for any element in your DTD:

<!ELEMENT topic (title , body, subtopic*) >
<!ATTLIST topic %id-group; >

<!ELEMENT subtopic (title, body, subtopic*) >
<!ATTLIST subtopic %id-group; >

A resulting document instance may look like this ("..." is used to represent content):

<topic id="tp-001 children="tp-002 tp-003 tp-004">
<title>...</title>
<body>...</body>
<subtopic id="tp-002" parent="tp-001">
<title>...</title>
<body>...</body>
</subtopic>
<subtopic id="tp-003" parent="tp-001">
<title>...</title>
<body>...</body>
</subtopic>
<subtopic id="tp-004" parent="tp-001" children="tp-005">
<title>...</title>
<body>...</body>
<subtopic id="tp-005" parent="tp-004">
<title>...</title>
<body>...</body>
</subtopic>
</subtopic>
</topic>


No comments:

Post a Comment