Pages

Tuesday, June 21, 2011

Define mixed content in XSD to add functionality to your content

It's often useful to tag specific items in running text to enhance output, facilitate indexing, and create linking mechanisms. Blending text and elements this way is called mixed content. For example, a drug information publisher might want to identify generic, brand name, and emphasized content within running text, like this:

When applying <generic>Ibdellium HCl</generic> (<brand>Blemish-Be-Gone</brand>) to the face, <emph>contact with eyes</emph>. Should eye contact occur, flush with warm water.

This isn't possible unless the element can contain mixed content. In XSD, you can define it this way:

<xsd:element name="para">
<xsd:complexType mixed="true">
  <xsd:choice minOccurs="0" maxOccurs="unbounded"/>
    <xsd:element name="generic" type="xsd:string"/>
    <xsd:element name="brand" type="xsd:string"/>
    <xsd:element name="emph" type="xsd:string"/>
  </xsd:choice>
</xsd:complexType>
</xsd:element>

The key components of this code are as follows:

  1. The complexType needs an attribute of mixed="true". This allows mixed content.
  2. The addition of minOccurs="0" and maxOccurs="unbounded" attributes to the <xsd:choice> element. The occurrence indicators will allow any elements within <xsd:choice> to occur zero or more times in any order, giving you the flexibility to use more than one <generic>, <brand>, or <emph> tag within a paragraph, interspersed with text.


No comments:

Post a Comment

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