|MICRO CENTER: COMPUTERS AND ELECTRONICS|
Introduction to XML, Part I
In this example XML file, I listed my previous Random Access articles based on the date and type of article. XML tags depend on hierarchy in which these descriptive tags are nested within one another based on relevance. This structure is called a tree, and each tag represents an element of this tree. My starting tag is named "newsletter" which is referred to as the "root element", because it holds all of the subsequent tags called "child elements." My XML file has four child elements: articles, version, section and articletitle. When there is a new element, the XML follows the same logical pattern and ends with the final root element tag which in this case is </newsletter>. Every element has to have an open <mytag> and closing </mytag> tags, but you can nest them and use any name that you want. Also, there are a few rules on naming tags. You can't use dashes, colons, periods or non-English characters. It's essential to have good structure to your XML document, but the tree design can be unique between different documents. Another important point to keep in mind: XML is case sensitive, and you need keep the same syntax and format for your tags. Unlike HTML, XML keeps white space, so be careful inputting your data.
Write Your Code
Once you have your editor of choice, you need to begin by saving a file with the .xml extension. Next, enter the document identifier tag in the first line of your file as shown in the sample code:<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
This is called a declaration and shows the version and encoding type of the XML. The current version of XML is 1.0. The encoding schema refers to the character sets for a language. In the sample code, UTF-8 is designated as the default English language set, but there are many options depending on type of characters you want to use in your XML. The final piece of the declaration is the standalone reference. A "yes" notation makes the document self-contained. A "no" value declares that the document refers to an external document for information. When using a WYSIWYG editor or other programming-assist application, the XML declaration is usually created automatically for you, but it's good to know for future reference.
Below the declaration, there are a few comments included to explain the purpose of the document. Comments are created by using <!-- comments --> notation which is the same as HTML comments. Between the two tags you can write whatever you want, and it will be ignored by the browser. Remember: Comments are good and use them freely throughout your code. This helps the next person understand your logic and where to make changes.
<!DOCTYPE root element name [ element declarations ]>
Sample DTD <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE newsletter [ <!ELEMENT articles ANY> <!ELEMENT version ANY> <!ELEMENT section ANY> <!ELEMENT articletitle ANY> ]>
Each element is declared by <!ELEMENT > followed by the element name. The word ANY represents that any type of data can be entered into this tag. There are other options available such as EMPTY for elements without content and PCDATA for parsed character data. DTDs have many more attributes to describe data, but going forward, schemas are more practical to use.
Schemas are actually the preferred method over DTDs. They are more versatile and are reusable for other XML documents. To create a schema, you need to make a new file with an .xsd extension.
Unlike DTDs, schemas are always external and connected to the XML file through an import statement which is declared at the top of your XML file as:<xsi:schemaLocation="http://www.yourwebsite.com/your_schema.xsd">
Now back to the schema file. You start with the same XML declaration as before, but add the schema declaration also like this:<schema xmlns="http://www.w3.org/2001/XMLSchema"> your schema content </schema>
Then, start composing your schema definition. First, declare your root element by using:
On the next line, you need to define whether the content type is simple or complex. The difference is that simple has a single value where complex has one or more. In this example, I am using <xs:complexType> in my description. For the <xs:sequence> tag, I am stating that the following elements are always in the same order: articles, version, section and articletitle. Subsequently, I have to define the elements themselves. The notation follows the same pattern with <xs:element name="yourelementname" type="xs:datatype"/>. The type designation has many possible values. It depends on what kind of data is included in the element tag such as a number or characters. Since my data can be a combination of numbers or letters, I left the type as "string" to cover my bases. Finally, close all of the tags.
There is an easier way to create XML schemas by using third party software. Again, you don't need a DTD, schema or namespace for your XML to work, but it is good to know what they are in the event that you run across it.
Tune in next month for a continuation of my XML tutorial. Next, I am introducing how to use CSS (Cascading Style Sheets) and XSL (Extensible Stylesheet Language) to style your XML, plus how to integrate it into XHTML. In the meantime, check out Beginning XML from Wiley books for a more comprehensive look at XML.
© Micro Center