舊文件

此處文件僅供參考,請自行考量時效性與適用程度,其他庫藏文件請參考文件頁面
我們亟需您的協助,進行共筆系統搬移、及文件整理工作,詳情請查閱參與我們

「製作即時摘要」修訂間的差異

出自 MozTW Wiki

Adding an XSLT Transform Sheet
Specifying the Output Type
行 39: 行 39:
 
即時摘要來源中可以放任何形式的 XSLT,設定為產生超文件也行,但 Firefox 目前只會顯示純文字內容。
 
即時摘要來源中可以放任何形式的 XSLT,設定為產生超文件也行,但 Firefox 目前只會顯示純文字內容。
  
= Specifying the Output Type =
+
= 設定輸出格式 =
  
Since the XSLT transform sheet will generate a text microsummary, we should indicate this with the XSLT <code><output></code> element:
+
由於 XSLT 樣式表會產岀文字摘要,所以 XSLT <code><output></code> 元素需設定如下:
  
 
  <?xml version="1.0" encoding="UTF-8"?>
 
  <?xml version="1.0" encoding="UTF-8"?>

於 2006年7月24日 (一) 08:23 的修訂

即時摘要來源 (microsummary generator) 內含一組從網頁中製出即時摘要的指令,而網頁能在 <head> 元素中放入 <link rel="microsummary"> 以指定適用的摘要來源。即時摘要來源檔案中如含有適用頁面列表,則也可以獨立安裝。

在這份教學文件中,我們將建立 Spread Firefox 首頁的即時摘要來源,藉以顯示目前 Firefox 的總下載數。 (例: 174475447 Fx downloads)。

接下來我們會用一份 XSLT 樣式表將該網頁轉換為即時摘要、指定此摘要來源的適用頁面並了解摘要來源傳佈方式,讓使用者能下載及安裝。在這份文件中每階段新增的程式碼會以粗體顯示,方便您辨識。

開始

摘要來源是 XML 文件的一種,而其根元素為 <generator>、名稱空間 (namespace) 需指定為 http://www.mozilla.org/microsummaries/0.1,所以要建立一份摘要來源就先得在空的純文字檔中放入 XML 宣告及空的 <generator> 標籤:

<?xml version="1.0" encoding="UTF-8"?>
<generator xmlns="http://www.mozilla.org/microsummaries/0.1">
</generator>

命名

每個即時摘要來源都必須有個名字,該名稱以 name 屬性記載、明確說明此摘要來源所建立的摘要主題。由於我們要建立的摘要來源將顯示 Firefox 總下載數,所以便命名為「Firefox Download Count」:

<?xml version="1.0" encoding="UTF-8"?>
<generator xmlns="http://www.mozilla.org/microsummaries/0.1"
           name="Firefox Download Count">
</generator>

加上 XSLT 樣式表

即時摘要來源檔案中必須含有一份 XSLT 樣式表,以便將網頁依樣式表設定轉換為即時摘要。同一份資料碰上不同的 XSLT 樣式便能轉換為「相貌」不同的文件,這便是 XSLT 威力所在。

以下示範以 <template> 元素在摘要來源中添加 XSLT 樣式表:

<?xml version="1.0" encoding="UTF-8"?>
<generator xmlns="http://www.mozilla.org/microsummaries/0.1"
           name="Firefox Download Count">
  <template>
    <transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
    </transform>
  </template>
</generator>

即時摘要來源中可以放任何形式的 XSLT,設定為產生超文件也行,但 Firefox 目前只會顯示純文字內容。

設定輸出格式

由於 XSLT 樣式表會產岀文字摘要,所以 XSLT 的 <output> 元素需設定如下:

<?xml version="1.0" encoding="UTF-8"?>
<generator xmlns="http://www.mozilla.org/microsummaries/0.1"
           name="Firefox Download Count">
  <template>
    <transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <output method="text"/>
    </transform>
  </template>
</generator>

Using a Simple XSLT <template>

The XSLT processor transforms documents by comparing each XSLT <template> element in the transform sheet to a set of nodes in the document. When a <template>'s match attribute matches a node, the processor performs the transformations specified by the content of the element.

This mechanism is powerful, because it lets you traverse the node tree of a document, recursively generating output based on the contents of the document. But for the purposes of generating a microsummary for the Spread Firefox page, we only need to use a single <template> element that matches the root node of the document and is processed once:

<?xml version="1.0" encoding="UTF-8"?>
<generator xmlns="http://www.mozilla.org/microsummaries/0.1"
           name="Firefox Download Count">
  <template>
    <transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <output method="text"/>
      <template match="/">
      </template>
    </transform>
  </template>
</generator>

Including the Download Count

To include the download count in the output of the XSLT transform sheet, we need to add an XSLT <value-of> element to the template whose select attribute contains an XPath expression that points to the node containing the count.

XPath is a language for identifying nodes in HTML/XML documents. It also contains basic functions for manipulating those nodes and their content. The easiest way to get an XPath expression that points to the node in question is to use the XPath Checker extension.

Install the extension (restarting Firefox to complete installation) then go to the Spread Firefox home page, find the Firefox download count (a large number at the bottom of the right-hand column), context click on the number, and select View XPath from the context menu.

XPath Checker will open a new window. The new window will include an XPath field that contains this XPath expression pointing to the download count node: id('download-count').

Add a <value-of> element to the XSLT <template> element whose select attribute contains that XPath expression:

<?xml version="1.0" encoding="UTF-8"?>
<generator xmlns="http://www.mozilla.org/microsummaries/0.1"
           name="Firefox Download Count">
  <template>
    <transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <output method="text"/>
      <template match="/">
        <value-of select="id('download-count')"/>
      </template>
    </transform>
  </template>
</generator>

Adding Text

To include the label Fx downloads in the microsummary, we need to add an XSLT <text> element to the XSLT <template> element whose content is the text we want to add.

Add a <text> element to the XSLT template with the content Fx downloads:

<?xml version="1.0" encoding="UTF-8"?>
<generator xmlns="http://www.mozilla.org/microsummaries/0.1"
           name="Firefox Download Count">
  <template>
    <transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <output method="text"/>
      <template match="/">
        <value-of select="id('download-count')"/>
        <text> Fx downloads</text>
      </template>
    </transform>
  </template>
</generator>

Note that white space between XSLT tags is not included in the XSLT output, unlike in HTML where that white space is collapsed to a single space, so make sure to prepend a space to the phrase in order to separate it from the download count.

With this addition, we've finished writing the XSLT transform sheet that converts the Spread Firefox home page to its microsummary.

Specifying the Page to which the Generator Applies

Now that we've written the transform sheet, we have to specify the page to which it applies. If we were the Spread Firefox webmasters, we might simply reference the generator within the page itself by adding a <link rel="microsummary"> tag to its <head> element:

<head>
  ...
  <link rel="microsummary" href="path/to/our/generator.xml">
</head>

Since we're not that site's webmasters, however, we can specify the page to which the generator applies within the generator itself and then make the generator available for download and installation. To specify pages to which a generator applies, we use a <pages> element within the <generator> element:

<?xml version="1.0" encoding="UTF-8"?>
<generator xmlns="http://www.mozilla.org/microsummaries/0.1"
           name="Firefox Download Count">
  <template>
    <transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <output method="text"/>
      <template match="/">
        <value-of select="id('download-count')"/>
        <text> Fx downloads</text>
      </template>
    </transform>
  </template>
  <pages>
  </pages>
</generator>

The <pages> element can contain a sequence of <include> and <exclude> elements specifying the pages to which a generator does and doesn't apply, respectively.

To make a generator apply to a page, add an <include> element whose content is a regular expression matching the page. To make the generator not apply to a page, add an <exclude> element whose content is a regular expression matching the page.

By default, generators don't apply to any page, so you have to explicitly list the pages they apply to, and you don't have to exclude any pages unless you've previously included them.

Add an <include> element matching the Spread Firefox home page:

<?xml version="1.0" encoding="UTF-8"?>
<generator xmlns="http://www.mozilla.org/microsummaries/0.1" name="Firefox Download Count">
  <template>
    <transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <output method="text"/>
      <template match="/">
        <value-of select="id('download-count')"/>
        <text> Fx downloads</text>
      </template>
    </transform>
  </template>
  <pages>
    <include>http://(www\.)?spreadfirefox\.com/(index\.php)?</include>
  </pages>
</generator>

Making the Generator Available for Download

Now that the generator applies to the Spread Firefox home page, the only thing left to do is to make it downloadable. To do so, we need to put it up on the web and then create a JavaScript link on some web page that calls Firefox's window.sidebar.addMicrosummaryGenerator() method to download and install the generator.

For example, if we put the generator file on the web at http://people.mozilla.com/~myk/microsummaries/tutorial/sfx-generator.xml, and we wanted users to be able to install it from http://people.mozilla.com/~myk/microsummaries/tutorial/index.html, we might add the following code to the index.html page:

<a href="javascript:window.sidebar.addMicrosummaryGenerator('http://people.mozilla.com/~myk/microsummaries/tutorial/sfx-generator.xml')">Install the Spread Firefox home page microsummary!</a>

Clicking that link will generate a JavaScript error on browsers that don't support microsummaries, however, so to improve the experience for those users, we should check to see if the user is using a microsummaries-enabled browser and display an explanatory message if not. We might do so via the following code:

<script>
  const warning = "Sorry, you need a microsummary-enabled browser like Firefox 2.0 to install and use microsummary generators.";
  function addGenerator(url) {
    if (typeof window.sidebar == "object" &&
        typeof window.sidebar.addMicrosummaryGenerator == "function")
      window.sidebar.addMicrosummaryGenerator(url);
    else
     alert(warning);
  }
</script>
<a href="javascript:addGenerator('http://people.mozilla.com/~myk/microsummaries/tutorial/sfx-generator.xml')">Install the Spread Firefox home page microsummary!</a>

Conclusion

You should now have a microsummary generator that displays the current Firefox download count when you install it, bookmark the Spread Firefox home page, and select the microsummary from the Summary drop-down menu in the Add Bookmark dialog.

For more information about Microsummaries, see the Microsummaries home page.

譯者註:這個功能目前在中文部分有點問題,已提報 bug 345016

個人工具