Selecting by ID

Another way of selecting elements is by their ID values, which you set with an ID attribute. To create a selector that targets elements with a certain ID, you use the syntax ELEMENT_NAME # ID_VALUE . For example, here's how I create a rule for <P> elements with the ID value of "TOP" :

Listing ch09_10.css
 TITLE {display: block; font-size: 24pt; font-weight: bold; text-align: center; text-decoration: underline} AUTHOR {display: block; font-size: 18pt; font-weight: bold; text-align: center} SECTION {display: block; font-size: 16pt; font-weight: bold; text-align: center; font-style: italic} P {display: block; margin-top: 10}  P#TOP {display: block; margin-top: 30}  

To give an element the ID "TOP" , I can add an ID attribute like this, assuming that the browser can understand this attribute:

Listing ch09_11.xml
 <?xml version="1.0" standalone="yes"?> <?xml-stylesheet type="text/css" href="ch09_10.css"?> <DOCUMENT>     <TITLE>The Meditations</TITLE>     <AUTHOR>By Marcus Aurelius</AUTHOR>     <SECTION>Book One</SECTION>  <P ID="TOP">  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> 

Internet Explorer lets you use ID attributes to XML elements, so selecting by ID like this gives you the same results that you see in Figure 9-6. Note that, as with the CLASS attribute, you must declare the ID attribute if you want to use it. Such a declaration might look like this in a document type definition (DTD)note that I'm declaring this attribute of type ID :

 <!ELEMENT P (#PCDATA)>  <!ATTLIST P ID ID #REQUIRED> 


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