XML TIPS

  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>
  2. Image Upload 2
    • <bookstore>
    • <book category="COOKING">
    • <title lang="en">Everyday Italian</title>
    • <author>Giada De Laurentiis</author>
    • <year>2005</year>
    • <price>30.00</price>
    • </book>
    • </bookstore>
  3. All XML Elements Must Have a Closing Tag
    • <p>This is a paragraph</p>
    • <p>This is another paragraph</p>
  4. XML Tags are Case Sensitive
    • <Message>This is incorrect</message>
    • <message>This is correct</message>
  5. 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.
  6. XML Documents Must Have a Root Element<root>
    • <root>
    • <child>
    • <subchild>.....</subchild>
    • </child>
    • </root>
  7. 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">
  8. 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
  9. The syntax for writing comments in XML:
    <!-- This is a comment -->
  10. 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
  11. 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
  12. What is an XML Element?
    An XML element is everything from (including) the element's start tag to (including) the element's end tag.
  13. An element can contain:
    • other elements
    • text
    • attributes
    • or a mix of all of the above...
  14. 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
  15. 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.
  16. XML Elements are Extensible.
    XML elements can be extended to carry more information.

    <note>
    <to>Tove</to>
    <from>Jani</from>
    <body>Don't forget me this weekend!</body>
    </note>
    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.
  17. 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>
  18. XML Elements vs. Attributes
    Take a look at these examples:

    <person sex="female">
    <firstname>Anna</firstname>
    <lastname>Smith</lastname>
    </person>

    <person>
    <sex>female</sex>
    <firstname>Anna</firstname>
    <lastname>Smith</lastname>
    </person>
    • 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.
  19. 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.
  20. 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:


    <messages>
    <note id="501">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    </note>
    <note id="502">
    <to>Jani</to>
    <from>Tove</from>
    <heading>Re: Reminder</heading>
    <body>I will not</body>
    </note>
    </messages>
    • 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.
  21. XML VALIDATION

    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)
  22. XML VALIDATION

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

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

    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)>
    • ]>
  23. 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>
  24. 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>
    • <COUNTRY>USA</COUNTRY>
    • <COMPANY>Columbia</COMPANY>
    • <PRICE>10.90</PRICE>
    • <YEAR>1985</YEAR>
    • </CD>


    The CSS file

    • CATALOG
    • {
    • 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;
    • }
    • COUNTRY,PRICE,YEAR,COMPANY
    • {
    • display: block;
    • color: #000000;
    • margin-left: 20pt;
    • }
  25. 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>
Author
salenka
ID
98012
Card Set
XML TIPS
Description
Pieces of information on XML from http://www.w3schools.com/xml/default.asp
Updated