2012-02-28 59 views
1

我想讓命名空間寫在外部的css(實際上是兩個單獨的文件)。當我在瀏覽器上運行該文件時,它不會使用聲明的名稱空間。我認爲,該文件被調用CSS文件,但它不是採用命名空間的方式得到聲明的命名空間工作時出現問題

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<?xml version="1.0" encoding="utf-8"?> 


<html xmlns:act="http://www.superstarmovies.com/actors" 
    xmlns:mov="http://www.superstarmovies.com/movies" 
    xmlns="http://www.w3.org/1999/xhtml" > 

<head> 
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" /> 
<title>Superstar Movies: Stars of the Month</title> 
<link rel="stylesheet" href="movies.css" type="text/css" ?> 
<link rel="stylesheet" href="actors.css" type="text/css" ?> 
<link rel="stylesheet" href="superstar.css" type="text/css" /> 
</head> 

<body> 
<div id="heading"><img src="logo.gif" alt="Superstar Movies" /></div> 

<div id="main"> 
<h1>Stars of the Month</h1> 
<h3>Movies by Our Featured Stars!</h3> 
<act:actors> 

    <act:actor> 
     <act:name>Halle Berry</act:name> 
    <act:date>August 14, 1966</act:date> 
    <act:birthplace>Cleveland, Ohio</act:birthplace> 
      <mov:movie genre="action" star="Halle Berry"> 
     <mov:name>Catwoman</mov:name> 
    <mov:date>(2004)</mov:date> 
    <mov:length>104 minutes</mov:length> 
    </mov:movie> 

    <mov:movie genre="horror" star="Halle Berry"> 
     <mov:name>Gothika</mov:name> 
    <mov:date>(2003)</mov:date> 
    <mov:length>98 minutes</mov:length> 
    </mov:movie> 

    <mov:movie genre="drama" star="Halle Berry"> 
     <mov:name>Monster&apos;s Ball</mov:name> 
    <mov:date>(2001)</mov:date> 
    <mov:length>111 minutes</mov:length> 
    </mov:movie> 

    <mov:movie genre="fantasy" star="Halle Berry"> 
     <mov:name>X-Men</mov:name> 
    <mov:date>(2000)</mov:date> 
    <mov:length>104 minutes</mov:length> 
    </mov:movie> 

    <mov:movie genre="romance" star="Halle Berry"> 
     <mov:name>Jungle Fever</mov:name> 
    <mov:date>(1991)</mov:date> 
    <mov:length>132 minutes</mov:length> 
    </mov:movie> 
    </act:actor> 

    <act:actor> 
    <act:name>Tom Hanks</act:name> 
    <act:date>July 9, 1956</act:date> 
    <act:birthplace>Concord, California</act:birthplace> 

<mov:movie genre="drama" star="Tom Hanks"> 
    <mov:name>Catch Me If You Can</mov:name> 
<mov:date>(2002)</mov:date> 
<mov:length>141 minutes</mov:length> 
</mov:movie> 

<mov:movie genre="adventure" star="Tom Hanks"> 
    <mov:name>Cast Away</mov:name> 
<mov:date>(2000)</mov:date> 
<mov:length>143 minutes</mov:length> 
</mov:movie> 

<mov:movie genre="action" star="Tom Hanks"> 
    <mov:name>Saving Private Ryan</mov:name> 
<mov:date>(1998)</mov:date> 
<mov:length>170 minutes</mov:length> 
</mov:movie> 

<mov:movie genre="adventure" star="Tom Hanks"> 
    <mov:name>Apollo 13</mov:name> 
<mov:date>(1995)</mov:date> 
<mov:length>140 minutes</mov:length> 
</mov:movie> 

<mov:movie genre="comedy" star="Tom Hanks"> 
    <mov:name>Forrest Gump</mov:name> 
<mov:date>(1994)</mov:date> 
<mov:length>142 minutes</mov:length> 
</mov:movie> 

<mov:movie genre="drama" star="Tom Hanks"> 
    <mov:name>Philadelphia</mov:name> 
<mov:date>(1993)</mov:date> 
<mov:length>125 minutes</mov:length> 
</mov:movie> 

<mov:movie genre="comedy" star="Tom Hanks"> 
    <mov:name>Big</mov:name> 
<mov:date>(1988)</mov:date> 
<mov:length>104 minutes</mov:length> 
</mov:movie> 
    </act:actor> 

</act:actors> 
</div> 

<address> 
Superstar Movies &#183; 123 Moviestar Lane &#183; Hollywood, FL 12345 
</address> 

</body> 

</html> 

CSS

@namespace act "http://www.superstarmovies.com/actors"; 

act|actor  {display: block; font-family: Arial, Helvetica, sans-serif; 
      margin-bottom: 20pt} 

act|name, act|date {display: block} 

act|name   {font-size: 14pt; color: DarkRed; font-style: bold} 

act|date, act|birthplace {display: inline; font-style: italic; color: DarkRed} 

act|birthplace   {padding-left: 0.5em} 

@namespace mov "http://www.superstarmovies.com/movies"; 

mov|movie  {display:block; font-family: Arial, Helvetica, sans-serif} 

mov|name, mov|date, mov|length {display: inline} 

mov|name   {font-style: bold; color:#003;} 

mov|length  {font-style: italics; padding-left: 0.5em} 

的思考?

回答

5

在您的XHTML:

  1. 您的標記沒有形成良好:

    • 你的XML聲明應該是第一位的,那麼你的DOCTYPE聲明:

      <?xml version="1.0" encoding="utf-8"?> 
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
      
    • 您的某些<link />標籤未正確關閉,應該是:

      <link rel="stylesheet" href="movies.css" type="text/css" /> 
      <link rel="stylesheet" href="actors.css" type="text/css" /> 
      
  2. 您的網頁必須由服務器來擔任application/xhtml+xml。典型的服務器不知道您正在服務XHTML,因此他們將它們發送爲text/html。瀏覽器將無法將text/html文件視爲XML,因此它們不會將CSS應用於您的自定義XML元素。

    如果你用PHP的工作,它只是加入給你的XHTML文件的最頂端的一個問題:

    <?php header('Content-Type: application/xhtml+xml'); ?> 
    

    或者在ASP,補充一點:

    <% Response.ContentType = "application/xhtml+xml"; %> 
    

    你也應該有一個伴隨的元標記,但不需要您的網頁進行驗證,並且瀏覽器無論如何都忽略它:

    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" /> 
    

    爲了更好地理解這一點,你可以在UA上找到一篇簡短的歷史課程,將其作爲HTML標記湯在this answer中處理XHTML。

在你的CSS:

  1. font-weight: bold,不font-style: bold

  2. 這是font-style: italic而不是font-style: italics

  3. 確保您的@namespace語句放置在樣式表的開頭。從spec

    任何@namespace規則必須遵守所有@charset和@import規則和其他之前所有非忽略的規則在樣式表和規則集。


但與所有的說,你爲什麼不把你的演員和電影轉換成自己的XML文件,然後使用XSLT到熟悉,實際XHTML轉變呢?

+0

當然,除非你只是將它作爲練習,在這種情況下,除了突出顯示代碼中的錯誤外,沒有什麼需要說的。 – BoltClock 2012-02-29 00:11:57

+0

好的,我做了HTML文件中的所有更改,我認爲正在發生的是您在第4步中討論的內容。我不確定爲什麼它根本沒有讀取.css。是的,這是一個任務,我不使用PHP的這一個。我認爲我錯過了一些愚蠢和基本的東西。我編輯了原始html文件以反映更改 – Overcranked 2012-02-29 00:53:23

+2

@Overcranked:關於'application/xhtml + xml'的觀點是最重要的。如果您沒有爲正確的內容類型提供XHTML,那麼您的自定義元素將不會被正確處理,並且您的CSS無法應用。您是否在使用任何服務器端技術來完成您的任務? ASP.NET,也許? – BoltClock 2012-02-29 00:54:37