Subscribe:

Ads 468x60px

Thursday, September 6, 2012

All about styling XML with internal and external CSS


           Embedding the CSS in XML for styling 

                      During one of my assignment I came across the requirement of styling XML with CSS, as styling was specific to the file only so, I found internal/embedded CSS better. During this development process, I documented the progress here about all possiblities, tricks and example.

We know that we can use CSS in HTML in 3 ways.
        1.) Internal CSS:
<head>
<style type="text/css">
hr {color:blue;}
p {margin-left:15px;}
body {background-image:url("_images/back_ground.jpg");}
</style>
</head>


    2.) External CSS:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>


    3.) Inline CSS:
 <p style="color:blue;margin-left:15px">This is a paragraph.</p>


I tried to emulate internal/embedded styling in XML as like 2nd or 3rd point above.
First Point can be very easily done with xml-stylesheet tag in XML
For example:
We can have a xml like this , a CSS like this and include the CSS definition in XML in this way:


<?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>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
.
.
</CATALOG>


Like HTML uses link tag to connect to external CSS, similarily XML uses xml-stylesheet as Processing Instructions to process external CSS, This Processing Instruction i.e. sml-stylesheet must be before the first tag of the XML document. That's important !!. And like you know, the type="text/css"  is not actually required, but it helps browser to understand that if Browser for some reason don't understand CSS or CSS file doesn't exist then browser should ignore and not download the CSS file. Like link tag, xml-stylesheet  can also be used any no. of time. Please note, that xml-stylesheet is not a tag, but a Processing Instruction. and unlike link tag it has to be used only in the starting of the XML document.

Now coming to the main point of:

Embedding the CSS in the XML 



<?xml-stylesheet href="#style" type="text/css"?>
<ROOT>
  <EXTRAS id="style">
    HEADLINE {font-size: x-large;  color: #AAAAAA;}
    AUTHOR, PARA {display: block;color: #DD00FF;}
    EXTRAS { display: none; }
  </EXTRAS>
<ARTICLE>
  <HEADLINE>India My Country</HEADLINE>
  <AUTHOR>Om Sao</AUTHOR>
  <PARA> India is a beautiful South Asian Country which is surrouned by Oceans in three sides. </PARA>
</ARTICLE>
</ROOT>




Output of the above embedded CSS in XML  is as expected: 


If you try opening the XML with embedded CSS in Google Chrome browser, it fails to render anything..
It's a known bug in Chrome. So, please check in Firefox or IE.

[Please like/share/comment if you find it useful !]

23 comments:

  1. I simply would like to offer you a big thumbs up for your great info you have got here on this post. I am returning to your site for more soon.
    website design

    ReplyDelete
  2. Images Retouch is a professional Clipping Path Service Company and provides Clipping Path Services, Images Retouching Services and more for additional details http://www.imagesretouch.com

    ReplyDelete
  3. Nice description which is given by you in this blog. Its provide a solution related to my problem. Thank you for this blog
    Visit :- Embedded Design

    ReplyDelete
  4. P&P Infotech Perform to provide Web design & development services, mobile app creation, SEO Services, graphic design, web testing & hosting services all over the world.

    ReplyDelete
  5. Hi,

    This post is written in a very good and entails a lot of useful information for me. I am glad to find a good way of writing the post.

    Website development company in New York

    ReplyDelete
  6. this was intresting blog but same information about web desining is missing from here to know about web desining visit here

    ReplyDelete
  7. Thank you for sharing. This is very useful and good

    information . I got more information from your site.

    Web Development Company

    ReplyDelete
  8. nice blog.thank you for sharing useful post.
    web programming tutorial
    welookups

    ReplyDelete
  9. This is really a great piece of information. Great work Om Sao. For learning more about CSS styling in Lucknow you can walk in to Web Designing Training in Lucknow
    to get the best hands on training in Lucknow.

    ReplyDelete
  10. thanks for sharing this nice informative article....

    ReplyDelete
  11. Nonetheless, improper utilization of images in the plan can discolor the corporate character of any business. Following are a few images utilized in logo plan and their belongings: logo design service

    ReplyDelete
  12. This article is quite interesting and I am looking forward to reading more of your posts. Thanks for sharing this article with us.
    Website Design

    ReplyDelete
  13. Mama and that i ended up walking my house whenever we been aquainted with (Jordan Shoes For Sale Online) her brother Carmela and her early friend which one suffered with got here by means of france (Coach Outlet Clearance Sale) the last week. Employed to be almost (Ray Ban New Wayfarer Polarized) all fifteen, Chip six life (Yeezy Boost 350 Cheap) aging. Attired held in (Ray Ban Outlet Store) your off white, Quickly designed wear furthermore neckbring, Chinese shoe but rare bridal on the our pinkie, Your lover had to be stunningly tidy and i also was thrown (Ray Ban Outlet) off in a flash in love.

    Guiding each V12 Speedster is now Aston Martin's (New Yeezys 2020) scintillating 5.2 litre, (New Jordan Releases 2020) Combined Turbo V12 electric motor, With an optimum results pertaining to 700hp1 together with 753Nm1 twisting. Front middle wall mount, A all blend, Quad camera 48 device motor is mated on toward a ZF 8

    ReplyDelete
  14. 68.2% (338/496) of the DDI alert Coach Handbags Clearance overrides Yeezy Boost 350 were considered appropriate. Among inappropriate overrides, the therapeutic combinations put patients at increased risk of several specific conditions New Jordan Shoes 2020 including: serotonin syndrome (21.5%, n=34), cardiotoxicity (16.5%, Yeezy Discount n=26), or sharp falls in blood pressure or significant hypotension (28.5%, n=45). A small number of drugs and DDIs accounted for Ray Ban Glasses a disproportionate share of alert overrides.

    The Sanders campaign is furious about New York primary cancellation Ray Ban Outlet the Sanders campaign is furious about New York primary cancellation York cancelled its Democratic presidential primary on April 27. The Post Sean Sullivan explains why Bernie Coach Outlet Store Sanders Coach Outlet is outraged. York cancelled its Democratic presidential primary on April 27.

    ReplyDelete