Adding Stylesheets

The XML stylesheet can be attached to XML document using PI xml-stylesheet. Multiple stylesheets can be attached to a document just as it is done using link element in HTML. User can set alternatives between stylesheets through attributes like "title". The “type” is optional attribute while declaring stylesheet, but helps browser.

In the example below, default.css is the default style applied to a file.  The other stylesheets applied are of the user choice.             

Example of adding style sheets to XML

<?xml-stylesheet href=”default.css”?>

<?xml-stylesheet href=”style1.css” title=”styleType1” type=”text/css”?>

<?xml-stylesheet href=”style2.css” title=”styleType2” type=”text/css”?>

<root>

      <sports>

    <game>cricket</game>

    <player>Sachin Tendulkar</player>

    <trophy>Cricket World Cup</trophy>

      </sports>

</root>

Syntax rules in stylesheets

  • The style for text elements such as font size, family… etc has to be defined.
  • Each element has to be defined if it is block, inline or list.
  • Separate color, margin… etc can be defined for each element.
  • Child elements inherit the properties from parents. It has to be overridden, if it has to be changed.
  • If style for an element is changed in more than one place, the rule applied at last is implemented.

Examples of proper syntax of stylesheets in XML

sports{ 

  display:block;

}

game{ 

   text-transform: uppercase;

   text-decoration: underline;

}

player{

   color:green;

}

Limitations of CSS stylesheets in XML

  • Adding CSS to XML is feasible solution when XML is rendered in browser and the content of XML is text only.
  • HTML has standard tags like <h1>, <h2> …etc which shows big font by default. XML does not have any standard tags for default styling.
  • Data transformation done in XSLT such as adding, deleting, renaming an element or attribute is not possible with CSS.
  • CSS files have to be validated if modern features are used in styling.

 

›› go to examples ››