Monday, 1 August 2011

Part 4 - Elements&Attributes | XML

  XML Element

XML is a markup language that is used to store data in a self-explanatory manner. Making the data "self-explanatory" comes about by containing information in elements. If a piece of text is a title then it will be contained within a "title" element.

  XML Tag

A tag is just a generic name for a <element>. An opening tag looks like <element>, while a closing tag has a slash that is placed before the element's name: </element>. From now on we will refer to the opening or closing of an element as open or close tags. All information that belongs to an element must be contained between the opening and closing tags of an element.

  XML Attribute

Attributes are used to specify additional information about the element. It may help to think of attributes as a means of specializing generic elements to fit your needs. An attribute for an element appears within the opening tag.
If there are multiple values an attribute may have, then the value of the attribute must be specified. For example, if a tag had a color attribute then the value would be: red, blue, green, etc. The syntax for including an attribute in an element is:
  • <element attributeName="value">
In this example we will be using a madeup XML element named "friend" that has an optional attribute age.

  XML Code:

<friend age="23">Samantha</friend>

  Element Review

  • Elements are used to classify data in an XML document so that the data becomes "self-explanatory".
  • Opening and closing tags represent the start and end of an element.
  • Attributes are used to include additional information on top of the data that falls between the opening and closing tag.

  XPath - Attribute

You have already learned how to select any element in an XML document, but how would you get that element's attribute? Those attributes values are within your reach once you learn how to use @ !

  XML Code (Example)

<inventory>
 <drink>
<lemonade supplier="mother" id="1">
<price>$2.50</price>
<amount>20</amount>
</lemonade>
<pop supplier="store" id="2">
<price>$1.50</price>
<amount>10</amount>
</pop>
</drink>
<snack>
<chips supplier="store" id="3">
<price>$4.50</price>
<amount>60</amount>
<calories>180</calories>
</chips>
</snack>
</inventory>

  XPath - @ is for Attribute!

After you have figured out how to select an element in your XML document, just take it one step further to get the attribute.
At the end of your XPath expression, which is normally the element you want to select, add the at sign "@" plus the name of the attribute you wish to select. The following XPath expression selects chips element.

  XPath Expression:

inventory/snack/chips
If we wanted to select the supplier attribute of chips, we would add "@supplier" to our expression.

  XPath Expression:

inventory/snack/chips@supplier
If we wanted to select the supplier attribute of pop, the XPath expression would have to be changed slightly.

  XPath Expression:

inventory/drink/pop@supplier

0 comments em “Part 4 - Elements&Attributes | XML”

Post a Comment

 

E-Learning Copyright © 2011 Powered by Blogger