XML Content formatting with CSS

As we all are aware of the power of XML technology. Even though the XML language doesn’t inherently include any mechanism for defining how a document looks, CSS (Cascading Style Sheets) makes it possible to add a view to XML documents.  CSS allows us to format the XML content so that it can be displayed in the browsers. CSS styles are stored in style sheets, which contain style rules that apply styles to elements of a given type. Style sheet are usually placed in external style sheet documents with the filename extension .css.

The application of CSS style rules is determined by selectors, which are constructs that identify portions of an XML document. A selector establishes the link between a document and a style or set of styles. There are three kind of selectors used in CSS :

  1. Element type : Selects an element of a given type
  2. Attribute class : Selects an element of a certain class that is identified by a special attribute.
  3. Attribute ID : Selects an element with an ID that is identified by a special attribute.

Though we have lots of properties to deal with. I am explaining only the basics. Let’s take an example :

XML document

<?xml version=”1.0″ ?>
<?xml-stylesheet type=”text/css” href=”message.css” ?>
<messages>
<messagea>
I am element type.
</messagea>
<messageb class=”msg”>
I am attribute class.
</messageb>
<messagec id=”imsg”>
I am attribute ID.
</messagec> 
</messages>

Save the following document with name message.xml. Now we need to create a style sheet file as message.css and save it in the same location as message.xml.

 
messagea
{
    font-size:20px;
    color:Blue;
}

messageb.msg
{
    font-size:15px;
    color:Green;
}

#imsg
{
    font-size:10px;
    color:Red;
}

Advertisements

B.E in Electronics and Telecommunications from Raipur, Chhattisgarh. Currently working on Microsoft's .NET and SQL Server Technology

Tagged with: , , ,
Posted in XML

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: