02.06.2013 Views

XML Demystified

XML Demystified

XML Demystified

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

42<br />

<strong>XML</strong> Demystifi ed<br />

<strong>XML</strong> developers define middle_name as having zero to one occurrence. That is,<br />

the customer may have no middle name, or only one middle name but not multiple<br />

middle names.<br />

Reviewing Table 3-1, you’ll notice that the question mark is the symbol you use<br />

to indicate zero to one occurrence. Here’s how you write this in the declaration for<br />

the middle_name:<br />

<br />

<br />

<br />

<br />

This declaration of the name and the middle_name enables the <strong>XML</strong> document<br />

to contain the following data. You’ll notice two customers are entered into this<br />

document. The first customer doesn’t have a middle name and second has one. Both<br />

are valid. However, the first customer name will be invalid if you remove the<br />

question mark from the middle_name in the name declaration in the DTD because<br />

the first customer doesn’t have a middle name.<br />

<br />

Bob<br />

Smith<br />

<br />

<br />

Bob<br />

Alex<br />

Smith<br />

<br />

We show you a good example of zero to many in our original <strong>XML</strong> example. The<br />

customers tag may contain zero to many customer elements. You may have a process<br />

where you transmit a file every day, whether or not there are any new records. There<br />

are many business cases where you’ll need a positive confirmation that there haven’t<br />

been any new customers that day.<br />

Now you can change the business rule a little bit and say that you only want the<br />

file transmitted if at least one customer is defined. If this is the business rule, then<br />

you can change the definition of the customers element so it has one to many<br />

customer elements.<br />

Optional Child Elements<br />

The declaration of a parent element contains the names of child elements that are<br />

contained within the parent, which we discussed earlier in this chapter. However, all<br />

those child elements must be present for the <strong>XML</strong> document to be valid. If one is<br />

missing, then the entire document is invalid.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!