XML Attributes
XML Attributes
XML elements can have attributes in the start tag, just like HTML. From HTML you may remember:
<img src= "image_filename.jpeg"/>
In HTML (and in XML) attributes are name-value pairs that provide additional information about an element:
|
Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:
|
Use of elements vs. Attributes
Data can be stored in child elements or in attribute. Take a look at the following examples:
|
|
In the first example gender is an attribute. In the last, gender is a child element. Both examples provide that same information.
There is no rule about when to use attributes, ane when to use child elements. Use child elements if the information feels like data. Use attributes when the information is data about data (further describes elements). Als, it is easier to carry our searches based on elements.
Viewing XML files
Raw XML files can be viewed in any browser. However, to make XML documents display as web pages, you have to add display infrmation with support software.
Open an XML file (by double clicking on the file). The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign(-) to the left can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" oor "View Source" from the browser menu.
Look at this XML file: Example XML
Viewing an invalid XML file
If an erroneous XML file is opened, the browser will report the error.
Look at this XML file: Example XML with error
Other example XML files
An example CD catalogue.
An example breakfast menu.
Why does XML Display like this?
XML documents do not carry information about how to display the data
Since XML tags are "invented" by the author the XML document, browsers do not know if a tag like <table> describes an HTML tag or a dining table.
Without any information about how to display the data, most browsers will display the XML document as it is.
We wll take a look at using CSS to display the data.
Displaying XML with CSS
With CSS (Cascading Style Sheets) you can add display information to an XML document
Below there is an example of how to use CSS style sheets to format an XML document:
Look at the Book example XML file.
Look at the Book example CSS file.
|
Look at the Book example with CSS.
Note: If you want to carry out sophisticated selections you will need to use XSLT, but this is not covered in this module.
We wll take a look at using CSS to display the data.
Exercise
- Checking XML syntax: âwell-formedâ rules Examine the following xml file. Identify all of its syntactical errors and write in the corrections.
- Check for one root element.
- Check for properly opened and closed element tags.
- Check for case sensitive element naming.
- Check for properly nested elements.
- Check for quoted attribute values.
|
- Task: Design a DTD for an address book that includes fields for name, email, and phone number
- Extend the DTD with Optional Fields: Modify the previous DTD to include optional fields for address and notes