Attaching Style Sheets to XML Documents

In HTML, there are three ways to connect style sheets to documents: You can use the <STYLE> HTML element to attach an internal style sheet or an external one, or you can use the STYLE attribute in HTML elements to style an individual element. In XML, there's really only one way to connect a style sheet 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 style sheets, you set the type attribute to "text/css" and the href attribute to the URI of the style sheet. For example, to attach style.css to the XML document, I can use a <?xml-stylesheet?> processing instruction like this:

<?xml version="1.0" standalone="yes"?>
<?xml-stylesheet type="text/css" href="style.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.


Although I've said that using the <?xml-stylesheet?> processing instruction is the only way to attach a style sheet 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 morals and the government of my temper.

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.89.85