Java Reference
In-Depth Information
This is normally written in the shorthand form, like this:
<color red="255" green="128" blue="64" />
You can use single quotes to delimit an attribute value if you want. The names of the three attributes here
are red , green , and blue , which identify the primary components of the color. The values for these between
0 and 255 represent the contribution of each primary color to the result. Attribute names are defined using
the same rule as element names. The attributes for an element follow the element name in the start tag (or
the only tag in the case of an empty element) and are separated from it by at least one space. If a tag has
multiple attributes, they must be separated by spaces. You can also put spaces on either side of the = sign,
but it is clearer without, especially where there are several attributes. HTML fans should note that a comma
separator between attributes is not allowed in XML and is reported as an error.
A string that is an attribute value must not contain a delimiting character explicitly within the string, but
you can put a double quote as part of the value string if you use single quotes as delimiters, and vice versa.
For example, you could write the following:
<textstuff answer="it's mine" explanation='He said"It is mine"'/>
The value for the answer attribute uses double quotes as delimiters, so it can contain a single quote ex-
plicitly; thus the value is it's mine . Similarly, the value for the second attribute uses single quotes so the
string can contain a double quote, so its value is He said "It is mine" . Of course, someone is bound to
want both a single quote and a double quote as part of the value string. Easy, just use an escape sequence
within the value for the one that is a delimiter. For example, you could rewrite the previous example as
<textstuff answer='it&apos;s mine' explanation="He said&quot;It's mine&quot;"/>
In general it's easiest to stick to a particular choice of delimiter for strings and always escape occurrences
of the delimiter within a string.
You can define a circle by a diameter and a position. You can easily define a circle in XML — in fact,
there are many ways in which you could do this. Here's one example:
<circle diameter="30">
<position x="40" y="50"/>
</circle>
The diameter attribute for the <circle> tag specifies the diameter of the circle, and its position is spe-
cified by an empty <position/> tag, with the x and y coordinates of the circle's position specified by at-
tributes x and y . A reasonable question to ask is whether this is the best way of representing a circle. Let's
explore the options in this context a little further.
Attributes versus Elements
Obviously you could define a circle without using attributes, maybe like this:
<circle>
<diameter>30</diameter>
<position>
<x-coordinate>40</x-coordinate>
<y-coordinate>50</y-coordinate>
</position>
Search WWH ::




Custom Search