Java Reference
In-Depth Information
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 instance, 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
explicitly. Similarly, the value for the second attribute uses single quotes so the string can contain a
double quote. 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 for the one that is a delimiter. For instance, we 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.
In our Sketcher program, we can create circles that are specified by a radius and a position. We can easily
define a circle in XML - in fact there are many ways in which we could do this. Here's one example:
<circle radius="15">
<position x="30" y="50"/>
</circle>
The radius attribute for the <circle> tag specifies the radius, and its position is specified by an
empty <position/> tag with the x and y coordinates of the circles position specified by attributes x
and y . A reasonable question to ask is whether this the best way of representing a circle. We should
explore our options in this context a little further.
Attributes versus Elements
Obviously we could define a circle without using attributes, maybe like this:
<circle>
<radius>15</radius>
<position>
<x-coordinate>30</x-coordinate>
<y-coordinate>50</y-coordinate>
</position>
</circle>
This is the opposite extreme. There are no attributes here, only elements. Where the content of an
element is one or more other elements - as in the case of the <circle> and <position> elements
here - it is described as element content . A document design where all the data is part of element
content and no attributes are involved is described as element-normal .
Of course, it is also possible to represent the data defining a circle just using attributes within a single element:
Search WWH ::




Custom Search