Wednesday 17 November 2010

How to add presentation to an XML document?

How to add presentation to an XML document?
 
The  way of adding presentation to an XML document is to use a style sheet language such as Cascading Style Sheets and XSLT Extensible Style sheet Language Transformations. XML lets you separate form appearance from content. Your XML file contains your document information text, data and identifies its structure: your formatting and other processing needs are identified separately in a style sheet or processing system. The two are combined at output time to apply the required formatting to the text or data identified by its structure (location, position, rank, order, or whatever). An XML file is by far not the best thing to read when you just want to look at the data. Extensible Stylesheet Language XSL gives you the capability to transform XML files into other format  such as  Extensible StyleSheet Language Transformations, i.e HTML.

An Extensible Style sheet Language Transformation document can use HTML and Cascading Style Sheet to present the XML data in a more readable format for you to gaze.  The Data within an XML file is retrieved using a language called the XPath.  The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria. an XPath expression is often referred to simply as an X Path.

Sunday 14 November 2010

What are the differences and uses between elements and attributes?

 What are the differences and uses between elements and attributes?

Both elements and attributes have been designed to have a Name and a Value.
Elements can be parents of other elements and/or attributes and can be repeated within the same level of an XML document.  They also usually have start and end tags.
An element would look like: <Customer>John Doe</Customer>

Attributes consist of a named pair attached to an element start-tag.  An example of how an attribute is: <Customer ID="1234">John Doe</Customer>.  Attribute values must be enclosed in single or double quotes.  Attribute names must be unique within a single element occurrence.

When to use Elements versus Attributes is a very  tricky question and more related to architectural considerations than technical details.

Nevertheless there are things only Elements can do...

Elements can occur more than once (repeating) within the same level, while attributes can only appear once within the same level, example:

It is okay to have:
    <Root>
        <Customer ID="1234">John Doe</Customer>
        <Customer ID="2345">Jane Doe</Customer>
    </Root>

But it would be invalid to have:
    <Root>
       <Customer ID="1234" ID="2345">
    </Root>

Elements can be defined to be in a certain order, while attributes can appear in any order. As you get more familiar with XML, the distinction between an element and an attribute becomes more slippery. Attributes cannot contain markup and are thus guaranteed to be atomic, whether this is either good or bad depends on your point of view. Elements are flexible and hierarchical and can have zero or more textual elements in them. Again, either good or bad depending on how you look at it.

Saturday 6 November 2010

What is the importance of writing the first line when writing XML?


An XML declaration takes the following form:

<?xml version 
                opt._encoding 
                opt._standalone?>

The three key bits of the declaration are what some call pseudo-attributes, because they look syntactically similar to attributes. If present, the encoding declaration must follow the version, and, if present, the standalone declaration must be the last pseudo-attribute.


Declaring the XML version is especially important now that XML 1.1 has been approved as a W3C Recommendation. XML 1.1 changes the definition of well-formedness in small but definite ways. One nice change is that XML 1.1 makes the XML declaration mandatory. The recommendation states:
XML 1.1 documents MUST begin with an XML declaration which specifies the version of XML being used.
By definition, any XML document without a declaration is an XML 1.0 document. However, you should never leave the version unstated, especially since it is also very important to specify the encoding.


The foundation of XML is Unicode. Every character in an XML document is a Unicode character. If you were to remember only one fact about XML, this would be the one to choose. It's even more important than, say, the fact that all non-empty elements must have an opening and closing tag. Since a Unicode character is an abstraction, there must be a mechanism for actually representing these characters in a form that can be processed by computers. This form is called an encoding

The encoding of the document is only a convenience for transmitting the document, but you should understand clearly that the substance of the XML content is still strictly Unicode. It's the parser's job to translate from the encoding to Unicode.

The most common encodings are UTF-8 and UTF-16, which transmit Unicode characters as a sequence of 8-bit and 16-bit values, respectively. These are also the two encodings that must be supported by parsers. If you do not specify an encoding, an XML processor must assume UTF-8 or UTF-16 depending on the presence or absence of a special byte sequence (called the Byte Order Mark or BOM) at the very beginning of the file being parsed.

Standalone Declaration

The standalone declaration indicates whether a document relies on information from an external source, such as external document type definition (DTD), for its content. If the standalone declaration has a value of "yes", for example, <?xml version="1.0" standalone="yes"?>, the parser will report an error if the document references an external DTD or external entities.Leaving out the standalone declaration produces the same result as including a standalone declaration of "no". The XML parser will accept external resources, if there are any, without reporting an error.