Sie sind auf Seite 1von 1

3236-7 ch04.F.

qc

6/29/99

1:04 PM

Page 67

Chapter 4 Structuring Data

Of course we will want to identify which season were talking about. To do that, we
should give the SEASON element a YEAR child. For example:
<?xml version=1.0?>
<SEASON>
<YEAR>
1998
</YEAR>
</SEASON>

Ive used indentation here and in other examples to indicate that the YEAR element
is a child of the SEASON element and that the text 1998 is the contents of the YEAR
element. This is good coding style, but it is not required. White space in XML is not
especially significant. The same example could have been written like this:
<?xml version=1.0?>
<SEASON>
<YEAR>1998</YEAR>
</SEASON>

Indeed, Ill often compress elements to a single line when theyll fit and space is at a
premium. You can compress the document still further, even down to a single line,
but with a corresponding loss of clarity. For example:
<?xml version=1.0?><SEASON><YEAR>1998</YEAR></SEASON>

Of course this version is much harder to read and understand which is why I didnt
write it that way. The tenth goal listed in the XML 1.0 specification is Terseness in
XML markup is of minimal importance. The baseball example reflects this goal
throughout.

XMLizing League, Division, and Team Data


Major league baseball is divided into two leagues, the American League and
the National League. Each league has a name. The two names could be encoded
like this:
<?xml version=1.0?>
<SEASON>
<YEAR>1998</YEAR>
<LEAGUE>
<LEAGUE_NAME>National League</LEAGUE_NAME>
</LEAGUE>
<LEAGUE>
<LEAGUE_NAME>American League</LEAGUE_NAME>
</LEAGUE>
</SEASON>

67

Das könnte Ihnen auch gefallen