麻烦声明命名空间的工作 [英] trouble getting declared namespaces to work
问题描述
我试图获得用外部css编写的命名空间(实际上是两个单独的文件)。当我在浏览器上运行该文件时,它不会使用声明的名称空间。我认为这个文件正在调用css文件,但它没有应用命名空间风格。
$ b
HTML
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN
xmlns:mov =http://www.superstarmovies.com /电影
http://www.w3.org/ TR / XHTML1 / DTD / XHTML1-transitional.dtd>
<?xml version =1.0encoding =utf-8?>
xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =application / xhtml + xml; charset = utf-8/>
< title>超级明星电影:本月之星< / title>
< link rel =stylesheethref =movies.csstype =text / css?>
< link rel =stylesheethref =actors.csstype =text / css?>
< link rel =stylesheethref =superstar.csstype =text / css/>
< / head>
< body>
< div id =heading>< img src =logo.gifalt =Superstar Movies/>< / div>
< div id =main>
< h1>本月之星< / h1>
< h3>由我们的精选明星制作的电影!< / h3>
< act:actors>
< act:actor>
< act:name> Halle Berry< / act:name>
< act:date> 1966年8月14日< / act:date>
< act:出生地>克利夫兰,俄亥俄州< / act:birthplace>
< mov:movie genre =actionstar =Halle Berry>
< mov:name>猫女< / mov:name>
< mov:date>(2004)< / mov:date>
< mov:length> 104分钟< / mov:length>
< / mov:movie>
< mov:movie genre =horrorstar =Halle Berry>
< mov:name> Gothika< / mov:name>
< mov:date>(2003)< / mov:date>
< mov:长度> 98分钟< / mov:长度>
< / mov:movie>
< mov:movie genre =dramastar =Halle Berry>
< mov:name> Monster& Ball< / mov:name>
< mov:date>(2001)< / mov:date>
< mov:length> 111分钟< / mov:length>
< / mov:movie>
< mov:movie genre =fantasystar =Halle Berry>
< mov:name> X-Men< / mov:name>
< mov:date>(2000)< / mov:date>
< mov:length> 104分钟< / mov:length>
< / mov:movie>
< mov:movie genre =romancestar =Halle Berry>
< mov:name>丛林发烧< / mov:name>
< mov:date>(1991)< / mov:date>
< mov:length> 132分钟< / mov:length>
< / mov:movie>
< / act:actor>
< act:actor>
< act:name> Tom Hanks< / act:name>
< act:date> 1956年7月9日< / act:date>
&Concord,California< / act:birthplace>
< mov:movie genre =dramastar =Tom Hanks>
< mov:name>如果您可以,赶上我< / mov:name>
< mov:date>(2002)< / mov:date>
< mov:length> 141分钟< / mov:length>
< / mov:movie>
< mov:movie genre =adventurestar =Tom Hanks>
< mov:name>抛出< / mov:name>
< mov:date>(2000)< / mov:date>
< mov:length> 143分钟< / mov:length>
< / mov:movie>
< mov:movie genre =actionstar =Tom Hanks>
< mov:name>储存私人瑞恩< / mov:name>
< mov:date>(1998)< / mov:date>
< mov:length> 170分钟< / mov:length>
< / mov:movie>
< mov:movie genre =adventurestar =Tom Hanks>
< mov:name> Apollo 13< / mov:name>
< mov:date>(1995)< / mov:date>
< mov:length> 140分钟< / mov:length>
< / mov:movie>
< mov:movie genre =comedystar =Tom Hanks>
< mov:name> Forrest Gump< / mov:name>
< mov:date>(1994)< / mov:date>
< mov:length> 142分钟< / mov:length>
< / mov:movie>
< mov:movie genre =dramastar =Tom Hanks>
< mov:name>费城< / mov:name>
< mov:date>(1993)< / mov:date>
< mov:length> 125分钟< / mov:length>
< / mov:movie>
< mov:movie genre =comedystar =Tom Hanks>
< mov:name> Big< / mov:name>
< mov:date>(1988)< / mov:date>
< mov:length> 104分钟< / mov:length>
< / mov:movie>
< / act:actor>
< / act:actors>
< / div>
<地址>
Superstar Movies&#183; 123 Moviestar Lane&#183;好莱坞,佛罗里达州12345
< / address>
< / body>
< / html>
CSS
< pre class =lang-css prettyprint-override> @namespace acthttp://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;颜色:DarkRed; font-style:bold}
act | date,act | birthplace {display:inline; font-style:italic; color:DarkRed}
act | birthplace {padding-left:0.5em}
@namespace movhttp://www.superstarmovies.com/movies;
mov | movie {display:block; font-family:Arial,Helvetica,sans-serif}
mov | name,mov |日期,mov |长度{display:inline}
$ b $ mov | name {font-风格:粗体;颜色:#003;}
mov | length {font-style:italics;填充左边:0.5em}
想法?
-
您的标记格式不正确:
>解决方案
-
您的XML声明应该先出现,然后是您的文档类型声明:
<?xml version =1.0encoding =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 =stylesheethref = movies.csstype =text / css/>
< link rel =stylesheethref =actors.csstype =text / css/>
application / xhtml + xml
服务器。典型的服务器不知道你在为XHTML服务,所以他们将它们作为 text / html
来发送。浏览器将无法将 text / html
文件视为XML,因此它们不会将CSS应用于您的自定义XML元素。
如果您使用PHP,只需将它添加到XHTML文件的顶部即可:
$ b
<?php header('Content-Type:application / xhtml + xml'); ?>
或者在ASP中添加:
<%Response.ContentType =application / xhtml + xml; %GT;
您还应该有一个伴随的元标记,但您的页面无需验证,浏览器无论如何忽略它:
< meta http-equiv =Content-Typecontent =application / xhtml + xml; charset = utf-8/>
您可以在UA上将XHTML视为HTML标签汤, https://stackoverflow.com/a/8871965/106224\">这个答案,以更好地理解这一点。
在您的CSS中: $ b
-
font-weight:bold ,而不是 font-style:bold
。font-style :斜体,而不是 font-style:italics
。 确保您的@namespace
语句放置在样式表的开头。从规范:
任何
@namespace
规则都必须遵循所有@charset和@import规则,并且在所有其他不被忽略的规则和规则集之前在样式表中。
但是,尽管如此,为什么不把演员和电影放入自己的XML文件中,然后使用XSLT将它们转换为熟悉的实际的XHTML ?
?I am trying to get namespaces written in an external css (two separate files actually). When I run the file on my browser it will not use the declared namespaces. I think that the file is calling the css file but its not applying the namespaced style
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'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 · 123 Moviestar Lane · 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}
Thoughts?
In your XHTML:
Your markup is not well-formed:
Your XML declaration should come first, then your doctype declaration:
<?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">
Some of your
<link />
tags aren't closed properly, they should be:<link rel="stylesheet" href="movies.css" type="text/css" /> <link rel="stylesheet" href="actors.css" type="text/css" />
Your page must be served as
application/xhtml+xml
by the server. Typical servers don't know that you're serving XHTML, so they send them astext/html
instead. Browsers won't be able to treattext/html
files as XML, so they won't apply CSS to your custom XML elements.If you work with PHP, it's simply a matter of adding this to the very top of your XHTML file:
<?php header('Content-Type: application/xhtml+xml'); ?>
Or in ASP, add this:
<% Response.ContentType = "application/xhtml+xml"; %>
You should also have an accompanying meta tag, but it's not necessary for your page to validate, and browsers ignore it anyway:
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
You can find a short history lesson on UAs treating XHTML as HTML tag soup in this answer to better understand this.
In your CSS:
It's
font-weight: bold
, notfont-style: bold
.It's
font-style: italic
, notfont-style: italics
.Make sure that your
@namespace
statements are placed at the beginning of your stylesheets. From the spec:Any
@namespace
rules must follow all @charset and @import rules and precede all other non-ignored at-rules and rule sets in a style sheet.
But with all that said, why aren't you placing your actors and movies into their own XML files, then transforming them using XSLT into familiar, actual XHTML?
这篇关于麻烦声明命名空间的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!