15.1. eXtensible Markup Language - XML¶
XML is a mark-up language. HTML is also a mark-up language. XML looks very similar to HTML, but it is different from XML.
Some of the differences between XML and HTML are:
XML is used to store and transport data, not to create web pages like HTML.
XML tags are not predefined, but HTML tags are.
XML can be extended (you can add new tags). You can not add new tags to HTML.
For more information on XML see https://www.w3schools.com/xml/default.asp.
Here is a sample of an XML document:
<person>
<name>Chuck</name>
<phone type="intl">
+1 734 303 4456
</phone>
<email hide="yes" />
</person>
Each pair of opening (e.g., <person>
) and closing tags
(e.g., <\person>
) represents a element or node with the same
name as the tag (e.g., person
). Each element can have some text,
some attributes (e.g., hide
), and other nested elements. If an XML
element is empty (i.e., has no content), then it may be depicted by
a self-closing tag (e.g., <email />
).
Often it is helpful to think of an XML document as a tree structure
where there is a top/root element (here: person
), and other tags (e.g.,
phone
) are drawn as children of their parent elements.
- person
- The person tag is the parent tag of the email tag.
- name
- The name tag is a sibling of the email tag.
- phone
- The phone tag is a sibling of the email tag.
- hide
- The hide is an attribute name of the email tag.
csp-10-2-2: What are the sibling tags of the email tag?
<food> <name>French Toast</name> <price>$4.50</price> <description> Thick slices made from our homemade sourdough bread </description> <calories>600</calories> </food>
<person> <name>Chuck</name> <phone type="intl"> +1 734 303 4456 </phone> <email hide="yes" /> </person>
15.1.1. Properly Formatted XML¶
Solve the next couple of problems and think about what are the rules for properly formatted XML.
Put the blocks into order to define just the body of simple XML document that defines a note. A note has a date, subject, and a body in that order. Indent the blocks to show the structure (parent and child).
Put the blocks into order to define just the body of simple XML document that stores information for a message: to, from, time, subject, and body in that order. Indent the blocks to show the structure (parent and child).
- There must be a root tag
- Correct! There must be a root tag.
- The case of tags doesn't matter
- Incorrect. Case matters.
- All elements must be closed
- Correct! All elements must be closed.
- Attribute values must be in quotes
- Correct! Attribute values must be in quotes.
csp-10-2-8: Select all of the following that are true about XML.
- title
- The title tag is a grandchild of the bookstore tag. It is the child of the book tag.
- author
- The author tag is a grandchild of the bookstore tag. It is the child of the book tag.
- year
- The year tag is a grandchild of the bookstore tag. It is the child of the book tag.
- book
- Correct! The book tag is a child tag of the bookstore tag.
csp-10-2-10: Select the child tag of the bookstore tag.
- category
- No, category is the attribute name.
- children
- Correct, "children" is a value of the category attribute.
- lang
- No, lang is the attribute name.
- en
- Correct, "en" is a value for the lang attribute.
csp-10-2-11: Select all of the attribute values in the example XML for a bookstore.
- An element in XML that is empty (does not have content) can use self-closing tags.
- Correct! An element in XML that is empty (does not have content) can use self-closing tags.
- Nodes in XML can have parent and children nodes.
- Correct! Nodes in XML can have parent and children nodes.
- XML has predefined (standard) tags.
- Incorrect. HTML has predefined (standard) tags but XML does not.
- XML has opening and closing tags like HTML.
- Correct! XML does have opening and closing tags like HTML.
- XML is the standard language for describing web pages.
- Incorrect. HTML is the standard language for describing web pages. XML is used to describe data.
csp-10-2-12: Select all of the following that are true.