Card Set Information

2011-08-22 22:45:39

Pieces of information on XML from http://www.w3schools.com/xml/default.asp
Show Answers:

  1. The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/West European character set).
    • <?xml version="1.0" encoding="ISO-8859-1"?>
    • <note>
    • <to>Tove</to>
    • <from>Jani</from>
    • <heading>Reminder</heading>
    • <body>Don't forget me this weekend!</body>
    • </note>
    • <bookstore>
    • <book category="COOKING">
    • <title lang="en">Everyday Italian</title>
    • <author>Giada De Laurentiis</author>
    • <year>2005</year>
    • <price>30.00</price>
    • </book>
    • </bookstore>
  2. All XML Elements Must Have a Closing Tag
    • <p>This is a paragraph</p>
    • <p>This is another paragraph</p>
  3. XML Tags are Case Sensitive
    • <Message>This is incorrect</message>
    • <message>This is correct</message>
  4. XML Elements Must be Properly Nested
    <b><i>This text is bold and italic</i></b>

    In the example above, "Properly nested" simply means that since the <i> element is opened inside the <b> element, it must be closed inside the <b> element.
  5. XML Documents Must Have a Root Element<root>
    • <root>
    • <child>
    • <subchild>.....</subchild>
    • </child>
    • </root>
  6. Attribute values must always be quoted. Either single or double quotes can be used
    • For a person's sex, the person
    • element can be written like this:

    <person sex="female">

    or like this:

    <person sex='female'>

    If the attribute value itself contains double quotes you can use single quotes, like in this example:

    <gangster name='George "Shotgun" Ziegler'>

    or you can use character entities:

    <gangster name="George &quot;Shotgun&quot; Ziegler">
  7. Some characters have a special meaning in XML.
    If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.

    There are 5 predefined entity references in XML:
    &lt; < less than

    &gt; > greater than

    &amp; & ampersand

    &apos; ' apostrophe

    &quot; " quotation mark
  8. The syntax for writing comments in XML:
    <!-- This is a comment -->
  9. HTML truncates multiple white-space characters to one single white-space:

    HTML: Hello (many spaces) Tove
    Output: Hello Tove
    White-space is Preserved in XML
  10. In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and line feed (LF).
    XML Stores New Line as LF
  11. What is an XML Element?
    An XML element is everything from (including) the element's start tag to (including) the element's end tag.
  12. An element can contain:
    • other elements
    • text
    • attributes
    • or a mix of all of the above...
  13. XML Naming Rules
    • Names can contain letters, numbers, and other characters
    • Names cannot start with a number or punctuation character
    • Names cannot start with the letters xml (or XML, or Xml, etc)
    • Names cannot contain spaces
  14. Best Naming Practices
    • Make names descriptive. Names with an underscore separator are nice: <first_name>, <last_name>.
    • Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>.
    • Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first.
    • Avoid "." characters. If you name something "first.name," some software may think that "name" is a property of the object "first."
    • Avoid ":" characters. Colons are reserved to be used for something called namespaces (more later).
    • XML documents often have a corresponding database. A good practice is to use the naming rules of your database for the elements in the XML documents.
    • Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software vendor doesn't support them.
  15. XML Elements are Extensible.
    XML elements can be extended to carry more information.

    <body>Don't forget me this weekend!</body>
    Extended code:

    • <note>
    • <date>2008-01-10</date>
    • <to>Tove</to>
    • <from>Jani</from>
    • <heading>Reminder</heading>
    • <body>Don't forget me this weekend!</body>
    • </note>

    The original application should still be able to find the <to>, <from>, and <body> elements in the XML document and produce the same output.

    One of the beauties of XML, is that it can be extended without breaking applications.
  16. Attributes 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 can be important to the software that wants to manipulate the element:

    <file type="gif">computer.gif</file>
  17. XML Elements vs. Attributes
    Take a look at these examples:

    <person sex="female">

    • 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.
  18. Use attributes for information that is not relevant to the data.

    Avoid XML Attributes?

    Some of the problems with using attributes are:
    • Attributes cannot contain multiple values (elements can)
    • Attributes cannot contain tree structures (elements can)attributes are not easily expandable (for future changes)
    • Attributes are difficult to read and maintain. Use elements for data.
  19. XML Attributes for Metadata
    Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements in much the same way as the id attribute in HTML. This example demonstrates this:

    <note id="501">
    <body>Don't forget me this weekend!</body>
    <note id="502">
    <heading>Re: Reminder</heading>
    <body>I will not</body>
    • The id attributes above are for identifying the different notes. It is not a part of the note itself.
    • What I'm trying to say here is that metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.

    XML with correct syntax is "Well Formed" XML.

    XML validated against a DTD is "Valid" XML.
    A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD)

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE note SYSTEM "Note.dtd">
    <body>Don't forget me this weekend!</body>

    The DOCTYPE declaration in the example above, is a reference to an external DTD file. The content of the file is shown next:

    The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements:

    • <!DOCTYPE note
    • [
    • <!ELEMENT note (to,from,heading,body)>
    • <!ELEMENT to (#PCDATA)>
    • <!ELEMENT from (#PCDATA)>
    • <!ELEMENT heading (#PCDATA)>
    • <!ELEMENT body (#PCDATA)>
    • ]>
  22. XML Schema

    W3C supports an XML-based alternative to DTD, called XML Schema:
    • <xs:element name="note">
    • <xs:complexType>
    • <xs:sequence>
    • <xs:element name="to" type="xs:string"/>
    • <xs:element name="from" type="xs:string"/>
    • <xs:element name="heading" type="xs:string"/>
    • <xs:element name="body" type="xs:string"/>
    • </xs:sequence>
    • </xs:complexType>
    • </xs:element>
  23. Displaying XML with CSS

    With CSS (Cascading Style Sheets) you can add display information to an XML document.

    (Formatting XML with CSS is not the most common method. W3C recommends using XSLT instead.)

    Next is a fraction of the XML file. The second line links the XML file to the CSS file:
    • <?xml version="1.0" encoding="ISO-8859-1"?>
    • <?xml-stylesheet type="text/css" href="cd_catalog.css"?>
    • <CATALOG>
    • <CD>
    • <TITLE>Empire Burlesque</TITLE>
    • <ARTIST>Bob Dylan</ARTIST>
    • <COMPANY>Columbia</COMPANY>
    • <PRICE>10.90</PRICE>
    • <YEAR>1985</YEAR>
    • </CD>

    The CSS file

    • {
    • background-color: #ffffff;
    • width: 100%;
    • }
    • CD
    • {
    • display: block;
    • margin-bottom: 30pt;
    • margin-left: 0;
    • }
    • TITLE
    • {
    • color: #FF0000;
    • font-size: 20pt;
    • }
    • ARTIST
    • {
    • color: #0000FF;
    • font-size: 20pt;
    • }
    • {
    • display: block;
    • color: #000000;
    • margin-left: 20pt;
    • }
  24. Displaying XML with XSLT

    XSLT is the recommended style sheet language of XML.
    XSLT (eXtensible Stylesheet Language Transformations) is far more sophisticated than CSS.
    XSLT can be used to transform XML into HTML, before it is displayed by a browser.
    XML Code

    <?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy® --><breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description>two of our famous Belgian Waffles with plenty of real maple syrup</description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>$7.95</price> <description>light Belgian waffles covered with strawberries and whipped cream</description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> <calories>900</calories> </food> <food> <name>French Toast</name> <price>$4.50</price> <description>thick slices made from our homemade sourdough bread</description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>$6.95</price> <description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> <calories>950</calories> </food></breakfast_menu>

    XSLT Code

    <?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy® --><html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"> <xsl:for-each select="breakfast_menu/food"> <div style="background-color:teal;color:white;padding:4px"> <span style="font-weight:bold"><xsl:value-of select="name"/></span> - <xsl:value-of select="price"/> </div> <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> <xsl:value-of select="description"/> <span style="font-style:italic"> <xsl:value-of select="calories"/> (calories per serving) </span> </div> </xsl:for-each> </body></html>