Tuesday, October 31, 2006

Using CSS when displaying an XML document

When you view an XML document in a web-browser you see the XML tree structure.

But it is possible for you to format the display using CSS, but mostly you would be using XSLT to transform the document with.

Here is a small example of how you can use CSS formatting for an XML document.

CSS file
books {
background-color: #000000;
width: 40%;
}
book {
background-color: #FF0000;
display : block;
margin-bottom: 20px;
}
title, price {
display : block;
color: blue;
}


XML document
<?xml version="1.0" encoding="ISO8859-1" ?>
<?xml-stylesheet type="text/css" href="books.css">
<books>
<book>
<title>Lord of the rings</title>
<price>1.99</price>
</book>
<book>
<title>The davinci mystery</title>
<price>2.99</price>
</book>
</books>

Friday, October 27, 2006

DTD

The purpose of a DTD is to describe the building blocks of an XML document, you can declare a DTD inline in your XML document or you can create an external file and reference to that from your XML document.

The DTD should be located just after the XML declaration, if you use an external DTD the reference is likewise after the XML declaration.

Example of an external DTD reference
<!DOCTYPE book SYSTEM "book.dtd">

Example of an internal DTD
<!DOCTYPE book [
<!ELEMENT book (title, author)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
]>
<book>
<title>James goes to hollywood</title>
<author>Sam Jones</author>
</book>

Monday, October 23, 2006

XML declaration

One thing I forgot about a well-formed document, is that it should have an XML declaration.
  • An XML declaration is not mandatory
  • It have 3 attributes that have a specific order
    • version - Must always be included, currently the only possible value is "1.0"
    • encoding - Specifies the character set use in the document, defaults to "UTF-8"
    • standalone - Specifies if the document relys on extanal documents, such as a DTD or XML schema. Defaults to "no"
  • Must start at the first character of the XML document

Example of XML declaration
<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>

Sunday, October 22, 2006

Comments

Comments in XML is written in the same way as in HTML.

You can not put a comment inside a tag likewise the XML declaration always have to be the first line in the document so you can not put a comment there.

A comment can be used to comment out blocks of tags, but you have to ensure that the XML is still well-formed.

<!-- This is a comment -->