XML Attributes Lecture 33 Robb T. Koether Hampden-Sydney College Wed, Apr 25, 2018 Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 1 / 15
1 XML Attributes 2 The getattribute() Function 3 Attributes as References 4 Assignment Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 2 / 15
Outline 1 XML Attributes 2 The getattribute() Function 3 Attributes as References 4 Assignment Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 3 / 15
XML Attributes XML Attributes <tag_name attr-1="value-1" attr-2="value-2"...>. </tag_name> An XML element may be given any number of attributes. Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 4 / 15
XML Attributes XML Attributes <book lang="en" cover="hard"> <title>treasure Island</title> </book> The attributes are intended to provide information about the element, but not data itself. The distinction is not always clear. Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 5 / 15
Elements vs. Attributes When should we use elements and when should we use attributes? The W3Schools website makes the remark There are no rules about when to use attributes or when to use elements. Attributes are handy in HTML. In XML my advice is to avoid them. Use elements instead. They give three reasons: Attributes cannot contain multiple values (elements can) Attributes cannot contain tree structures (elements can) Attributes are not easily expandable (for future changes) I agree, except in the following examples. Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 6 / 15
XML Attributes XML Attributes var book_list = xml_doc.getelementsbytagname("book"); var attr_list = book_list[0].attributes; var attr = attr_list[0]; var attr_name = attr.nodename; var attr_value = attr.nodevalue; In Javascript, each element has the attributes property, which is an indexed list of the element s attributes. Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 7 / 15
Attributes as References See the example library-attr.xml and library-attr.html. The XML file uses reference IDs to associate authors with books. Thus, each author does not need to be listed more than once. (This example assumes that each book has only one author.) Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 8 / 15
Outline 1 XML Attributes 2 The getattribute() Function 3 Attributes as References 4 Assignment Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 9 / 15
The getattribute() Function The getattribute() Function var book_list = xml_doc.getelementsbytagname("book"); var attr = book_list[0].getattribute("lang"); var attr_name = attr.nodename; var attr_value = attr.nodevalue; We can use the getattribute() function to select a specific attribute. Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 10 / 15
Outline 1 XML Attributes 2 The getattribute() Function 3 Attributes as References 4 Assignment Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 11 / 15
Attributes as References Attributes as References <book authid="123">...</book> <author id="123"> <fname>mark</fname> <lname>twain</lname> </author> Attributes are often used to allow one element to refer to another element. This allows us to impose a structure on the XML file beyond the basic tree structure. Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 12 / 15
Attributes as References See the example library-attr2.xml and library-attr2.html. The XML file uses reference IDs to associate authors with books. Thus, each author does not need to be listed more than once. (This example assumes that each book has only one author.) Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 13 / 15
Outline 1 XML Attributes 2 The getattribute() Function 3 Attributes as References 4 Assignment Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 14 / 15
Assignment Assignment Visit the W3Schools website http://www.w3schools.com/xml/xml_attributes.asp Visit the section labeled XML Attributes. Robb T. Koether (Hampden-Sydney College) XML Attributes Wed, Apr 25, 2018 15 / 15