使用级联样式表格式化XML外文翻译资料

 2022-01-04 09:01

Formatting XML by Using Cascading Style Sheets

Say that you want to take a look at the data in an XML document by using a browser. You might be out of luck unless you#39;re using a specialized XML browser that can handle the particular XML markup you#39;re using, such as the W3C Amaya browser that handles MathML. However, there are very few specialized XML browsers out there, and there#39;s a great deal of XML. Don#39;t you have any other options? You do. For one, you can use Cascading Style Sheets (CSS). Many browsers, such as Netscape Navigator and Internet Explorer, let you use CSS to format the data in XML documents for display, and today you#39;re going to see how that works. It#39;s getting more and more common to see CSS-formatted XML on the Web, and it often makes sense to display data by using CSS. You might want to create a table, for example, which is a lot easier on the eyes than a 40-page XML document.

Here#39;s an overview of today#39;s topics:

  • Creating CSS style sheets and CSS rules
  • Using CSS style sheets with XML documents
  • Selecting elements in style sheets
  • Grouping elements
  • Creating CSS classes
  • Selecting elements by ID
  • Using inline styles
  • Using block elements
  • Formatting text
  • Aligning text
  • Creating margins
  • Displaying images
  • Creating lists
  • Creating tables

Our Sample XML Document

You already know that XML provides a good way of storing data; for example, say that you want to store the text of your favorite stoic philosopher in an XML document. Listing 8.1 shows an example of text from the philosopher Epictetus, as ch08_01.xml.

Listing 8.1 An XML Document (ch08_01.xml)

The data of Epictetus#39; text is stored in ch08_01.xml, but it#39;s not exactly presented optimally. Even for a browser that can display XML directly, such as Internet Explorer, this text isn#39;t easy to read because of the embedded markup. Figure 8.1 shows ch08_01.xml in Internet Explorer.

Figure 8.1. An XML document displayed in Internet Explorer.

It turns out that you can improve this visual display by telling browsers how to format the XML elements you use for markup—for example, you can say that you want the lt;titlegt; element to be displayed using a particular font, in a particular size, and even in italics if you want. To doThat#39;s what today#39;s work is all about—convincing standard Web browsers to display XML by telling them how to format the data in various XML elements (and removing all the markup).

To do that, you#39;re going to use CSS today. CSS was first introduced in December 1996. It#39;s now widely in use for HTML browsers, and although it was originally used only to format HTML, thanks to CSS, the major browsers now let you format XML as well. In some ways, CSS works even better with XML than it does with HTML, because in XML you#39;re not limited to the predefined HTML elements and you can style sophisticated nestings of elements and more.

Introducing CSS

CSS has been standardized by the W3C, and as of this writing, there are three levels of CSS available: CSS1, CSS2, and CSS3. You can find the W3C recommendations for CSS1 and CSS2 at http://www.w3.org/TR/REC-CSS1 and http://www.w3.org/TR/REC-CSS2. CSS3 is still under development; it#39;s broken up into many modules, and at this point those modules are at various stages of acceptance, from working drafts to candidate recommendations. You can find more about the many CSS3 modules at http://www.w3.org/Style/CSS/current-work. There are also many CSS resources available at the W3C CSS page, http://www.w3.org/Style/CSS, including CSS tutorials and links to free tools.

Today you#39;ll be dealing almost exclusively with CSS1 because it gives you all you need in order to present XML documents. CSS2 includes some changes compared to CSS1, but its main thrust is in providing extra features, such as aural stylesheets for sounds and so on; CSS3 is still under development. Support for CSS1 in both Netscape Navigator and Internet Explorer is good—in fact, Internet Explorer supports all of CSS1 (and that was true as of March 27, 2000, when Internet Explorer 5.0 shipped). One notable aspect of CSS2 that you#39;re going to use today is its support for formatting tables.

Formatting using CSS revolves around using style sheets. Style sheets are collections of style rules, each of which shows how to format an XML element. For example, say that you want to specify how to format the text in the lt;titlegt; element in the ch08_01.xml sample XML document shown in Listing 8.1. How could you construct a style rule to do that? A rule is made up of a selector, which is the element(s) you want to format, and the rule specification, which shows what formatting you want to apply. Here#39;s how a rule might look if you wanted to format the text in the lt;

全文共27300字,剩余内容已隐藏,支付完成后下载完整资料


使用级联样式表格式化XML

如果你想通过浏览器查看一篇XML文档中的数据,除非你使用专门的XML浏览器来处理你使用的特定XML标记,比如处理MthML的W3C阿马亚浏览器,否则你可能会不走运。然而,很少有专门的XML浏览器存在,而且XML有很多。那你有没有其他选择呢?答案是肯定的。举个例子,你可以使用级联样式表(CSS)。

许多浏览器,比如Netscape Navigator和Internet Explorer,允许你使用CSS格式化XML文档中的数据以便显示,今天你将会看到它是如何运作的。在Web上看到CSS格式化XML变得越来越普遍,而且使用CSS显示数据通常是有意义的。

例如,你可能想要创建一个表,它比一篇40页的XML文档要悦目得多。

下面是今天的主题概述:

·创建CSS样式表和CSS规则

·使用带有XML文档的CSS样式表

·选择样式表中的元素

·分组元素

·创建CSS类

·通过ID选择元素

·使用内联样式

·使用块元素

·格式化文本

·对齐文本

·创建页边空白

·显示图片

·创建列表

·创建表

我们的XML文档示例

你已经知道XML提供了一种存储数据的好方法;例如,假设你想要将你最喜欢的斯多葛派哲学家的文本存储在XML文档中。清单8.1显示了哲学界埃皮克提图的文本示例,如ch08_01.xml。

埃皮克提图的文本数据存储在ch08_01.xml中,但并没有很好地展示出来。即使对于可以直接显示XML的浏览器,例如Internet Explorer,由于嵌入了标记,此文本也不容易阅读。图8.1显示了Internet Explorer中的ch08_01.xml。

事实证明,你可以通过告诉浏览器如何格式化用于标记的XML元素来改进这种可视化显示。例如,你可以说,你希望使用lt;titlegt;元素以特定字体、特定大小显示,如果需要,甚至可以用斜体显示。这就是今天的工作内容----通过告诉一般的Web浏览器如何在各种XML元素中格式化数据(以及删除所有标记),来让它们显示XML。

要做到这一点,你今天就要使用CSS了。CSS是在1996年12月首次引入的。它现在广泛用于HTML浏览器,虽然它最初只用于格式化HTML,但是由于CSS,现在主要浏览器也允许你格式化XML。在某些方面,CSS用于XML比用于HTML效果更好,因为在XML中,你不会局限于预定义的HTML元素,而且你可以设计复杂的元素嵌套等等。

介绍CSS

CSS已经被W3C标准化,并且截至此时,可用的CSS有三个级别:CSS1、CSS2和CSS3。你可以在http://www.w3.org/TR/REC-CSS1http://www.w3.org/TR/REC-CSS2中找到W3C推荐的CSS1和CSS2。CSS3仍在开发中;它被分解成许多模块,此时此刻那些模块处于被认可的不同阶段,从工作草案到候选推荐。你可以在http://www.w3.org/Style/CSS/current-work中找到更多关于这些CSS3模块的信息。在W3C CSS页面http://www.w3.org/Style/CSS上也有很多可用的CSS资源,包括CSS教程和免费工具的链接。

今天你将几乎只用到CSS1,因为它为你提供了展示XML文档所需要的所有东西。

与CSS1相比,CSS2包含了一些变化,但是其主旨是提供额外的特性,比如声音的听觉样式表等等;CSS3仍在开发中。Netscape Navigator和Internet Explorer两者都支持CSS1----实际上,Internet Explorer支持CSS1的所有东西(截至2000年3月27日,Internet Explorer 5.0上市的时候,是如此)。你今天将要用到的CSS2一个显著方面是它对格式化表的支持。

使用CSS格式化围绕使用样式表进行。样式表是样式规则的集合,每一个样式规则都显示如何格式化一个XML元素。例如,假设你想要指定如何格式化清单8.1所示的ch08_01.xml示例XML文档中的lt;titlegt;元素中的文本。你怎样建立一个样式规则来做这件事?规则由选择器和规则规范组成,选择器是你想要格式化的元素,规则规范显示你想要应用什么格式。如果你想在lt;titlegt;元素中以粗体、居中、带下划线的36点字体(1点是1/72英寸)格式化文本,那么规则可能是如下这个样子:

title {display: block; font-size: 36pt; font-weight: bold;text-align: center; text-decoration: underline}

注意,规则规范包含在大括号{ 和 }中。在这个规则中,title是选择器,{display: block; font-size: 36pt; font-weight: bold; text-align: center; text-decoration: underline}是规则规范。display、font-size、font-weight等等都是CSS属性,今天你会看到很多这样的属性。事实上,了解CSS很大程度上意味着了解什么样的属性是可用的。今天讨论的第二部分致力于各种CSS属性以及它们对你的作用。

规则规范由属性/值对组成,比如display:block,用分号分隔,正如你在编写自己的样式表时将看到的。display:block属性/值对尤其重要,因为它提供你在显示中格式化其自身行的元素,创建块级元素,就像HTML中的lt;H1gt;元素是块级元素一样。你将在几页中详细讨论这一点。

清单8.2显示了一个CSS样式表,它处理示例XML文档-lt;titlegt;、lt;philosophergt;、lt;bookgt;和lt;paragraphgt;中的所有四种元素类型。注意CSS样式表文件使用扩展名.css。

在今天的讨论中,你将编写清单8.2所示的这一类样式表。样式表看起来不太像XML文档,但是与XML一样,Web上有一些CSS验证器可以帮助你检查CSS。有两个如下:

·W3C CSS验证器,在http://jigsaw.w3.org/css-validator中,为你检查页面中的CSS。

·W3C TIDY程序可以将HTML文档中的样式转换为CSS。TIDY在http://tidy.sourceforge.net中可用。

下一步是将CSS样式表ch08_02.css连接到XML文档ch08_01.xml中。

连接CSS样式表和XML文档

将CSS样式表与HTML连接有三种方法:可以使用内部样式表、可以使用外部样式表,或者可以使用HTML STYLE属性将CSS样式与特定的HTML元素相关联。在XML中,你不会享有这些选择,至少以浏览器目前的成效来看不会。就此而言,真的只有一种将样式表连接到XML文档的方法:使用lt;xml-stylesheet?gt;处理指令。

实际上,lt;xml-stylesheet?gt;很本不属于XML规范。(与XML模式一样,W3C对于指示如何将样式表与XML文档等文件连接起来并不是特别好。)它已经成为了一种协定公约,在Netscape Navigator和Internet Explorer中得到了一致的支持。在HTML浏览器中,将处理指令的type属性设置为“text/css”,将href属性设置为样式表的URI。你可以看到它在ch08_03.xml中是如何工作的,它与ch08_01.xml相同,只是它使用了lt;?xml-stylesheet?gt;处理指令将自身连接到ch08_02.css。

当你将ch08_03.xml和ch08_03.css放在同一个目录中并在Internet Explorer中打开ch08_03.xml时,Internet Explorer读取并应用ch08_02.css中的CSS样式规则。图8.2显示了这样做的结果。

你可以在图8.2中看到发生了什么:你告诉Internet Explorer如何格式化lt;titlegt;、lt;philosophergt;、lt;bookgt;和lt;paragraphgt;元素,然后它按照你的需要做了,从显示中删除了所有标记。注意,不同的浏览器可以为同一CSS格式提供不同的结果。例如,图8.3显示了Netscape Navigator的同一文档ch08_03.xml。可以注意到文本看起来与Internet Explorer中的文本不一样。首先,段落之间没有垂直空间(因为Netscape Navigator不支持CSS关键字margin-top ,它指定在元素之间留下多少垂直空间)。

实际上,还有另外一种方式使用Internet Explorer将样式连接到XML元素。你可以使用样式属性来创建内联样式,就像这样:

He is free who lives as he lt;i style='font-style: italic'gt;wishes to live...lt;/igt;

一般而言,对于XML文档来说,这当然是不规范的,但Internet Explorer支持它,正如你今天将看到的。

由于你已经获得了一些使用CSS样式表和XML的经验,接下来的部分将展示如何创建样式表,从指定你想要样式化什么元素开始

创建样式表选择器

若要指定你要格式化的XML元素,请在XSS样式表中使用选择器。你已经在样式表ch08_02.css中看到了一些简单的选择器,其中每个选择器只是要格式化的元素的名称,比如title和philosopher。

在本例中,你将样式规则规范{display: block; font-size: 36pt; font-weight: bold; text-align: center; text-decoration: underline}应用于lt;titlegt;元素,样式规则规范{display: block; font-size: 16pt; text-align: center} 应用于lt;philosophergt;元素,等等。这是最简单的选择器,你只需命名要格式化的元素。

你也可以把元素分组在一起,只是用逗号隔开它们。清单8.4展示了该方法的一个例子,在ch08_04.css中,它以相同的方式格式化了lt;titlegt;和lt;bookgt;元素。

图8.4显示了应用于示例XML文档的样式表。正如你所看到的,lt;titlegt;和lt;bookgt;元素以相同的方式被格式化。

创建样式类

为了创建选择器,你不需要指定元素名称,相反,你可以使用样式类作为选择器。用一个点(.)作为名称的前缀来创建一个类,并用它作为选择器。例如,假设你想创建一个名为standout的类,它在珊瑚色的背景下用青色格式化文本。你可以通过创建standout类来实现这一点,如清单8.5所示。(这个例子依赖于这样一个事实,即你将使用的浏览器Internet Explorer有很多种颜色,包括已经内置的珊瑚色和青色)。

通过使用Internet Explorer中的名为class的属性(Netscape Navigator不支持此属性),你可以将standout类应用于诸如lt;titlegt;和lt;philosophergt;之类的元素。注意,类属性不是内置到XML中的,只是习惯上在Internet Explorer中使用它用于此目的。清单8.6展示了如何使用样式类ch08_06.css来对这些元素进行样式化的示例。

图8.5显示了这个格式化文档ch08_07.xml在Internet Explorer中的样子。注意,新的颜色被添加到显示器中,它们没有简单地替换已经存在的其他格式。

如果要在有效文档中使用class属性,则必须声明它。在DTD中,看起来可能是这样的:

lt;!ATTLIST title class CDATA #IMPLIEDgt;

在XML模式中,它可能是这样的:

lt;xsd:attribute name='class' type='xsd:text'/gt;

你也可以将样式类定位为特定元素。例如,假设你想要以特定的方式格式化文本中的第一段,通过使用文本缩进CSS关键字对其进行缩进,并使用边距顶部关键字将其与前一段文本进一步分离。你可以通过创建一个新的类来做到这一点,例如,命名为paragraph.first的类,如清单8.7所示。在这种情况下,你将指定

全文共8409字,剩余内容已隐藏,支付完成后下载完整资料


资料编号:[2286]

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。