Attaching Stylesheets to XML Documents

In HTML, there are three ways of connecting stylesheets to documents: You can use the <STYLE> HTML element to attach an internal or external stylesheet, or you can use the STYLE attribute in HTML elements to style an individual element. In XML, there's really only one way of connecting a stylesheet to an XML document, and that's by using the <?xml-stylesheet?> processing instruction (in fact, <?xml-stylesheet?> is only an agreed-upon convention and does not appear in the XML 1.0 W3C recommendation). To use <?xml-stylesheet?> with CSS stylesheets, you set the type attribute to "text/css" and set the href attribute to the URI of the stylesheet. For example, to attach ch09_02.css to the XML document, I can use a <?xml-stylesheet?> processing instruction like this:

Listing ch09_03.xml
 <?xml version="1.0" standalone="yes"?>  <?xml-stylesheet type="text/css" href="ch09_02.css"?>  <DOCUMENT>     <TITLE>The Meditations</TITLE>     <AUTHOR>By Marcus Aurelius</AUTHOR>     <SECTION>Book One</SECTION>     <P>         From my grandfather, Verus, I learned good morals         and the government of my temper.     </P>     <P>         From the reputation and remembrance of my father,         modesty and a manly character.     </P>     <P>         From my mother, piety and beneficence, and abstinence,         not only from evil deeds, but even from evil         thoughts; and further, simplicity in my way of living,         far removed from the habits of the rich.     </P>     <P>         From my great-grandfather, not to have frequented         public schools, and to have had good teachers at home,         and to know that on such things a man should spend         freely.     </P> </DOCUMENT> 

That's all it takes. Now you can open this document in a browser, as you see in Figure 9-2, and the browser will know how to handle the document.

Figure 9-2. A style XML document.

graphics/09fig02.gif

Although I've said that using the <?xml-stylesheet?> processing instruction is the only way to attach a stylesheet to an XML document, some browsers, such as Internet Explorer, also support a STYLE attribute in XML elements. You can specify styles using this attribute, like this:

 From my grandfather, <UL STYLE="text-decoration: underline">Verus</UL>, I learned good graphics/ccc.gif morals  and the government of my temper. 

I'll take a look at this way of styling individual elements, called inline styling, as well. However, it's worth noting that it's even less standard than the <?xml-stylesheet?> processing instruction, and its use is discouraged by style purists ( mainly because it decentralizes your style specification for a document).



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net