Attributes

Attributes in XML are much like attributes in HTML: They're name-value pairs that let you specify additional data in start and empty tags. To assign a value to an attribute, you use an equals sign.

For example, I'm assigning values to the STATUS attribute of the <CUSTOMER> elements in the XML here to indicate the status of the customer's credit:

Listing . ch02_03.xml
 
<?xml version = "1.0" standalone="yes"?>
<DOCUMENT>
    <CUSTOMER STATUS="Good credit"> <NAME> <LAST_NAME>Smith</LAST_NAME> <FIRST_NAME>Sam</FIRST_NAME> </NAME> <DATE>October 15, 2003</DATE> <ORDERS> <ITEM> <PRODUCT>Tomatoes</PRODUCT> <NUMBER>8</NUMBER> <PRICE>$1.25</PRICE> </ITEM> <ITEM> <PRODUCT>Oranges</PRODUCT> <NUMBER>24</NUMBER> <PRICE>$4.98</PRICE> </ITEM> </ORDERS> ...

Get Real World XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.