XSP (eXtensible Server Page )作為Cocoon的一個(gè)重要的組成部分可以為Cocoon的pipeline生成XML文件。XSP文件本身是XML文件,遵循XML的規(guī)范,但是支持在XML文件中包含程序代碼。XSP借助Bean Scripting Framework支持多種語(yǔ)言,包括Java,Javascript,Python 等。
XSP文件的根元素為<page>,通常使用xsp名字空間(<xsp:page>)。
一個(gè)簡(jiǎn)單的例子如:
1: <?xml version="1.0"?>
2: <?cocoon-process type=”xsp”?>
3:
4: <xsp:page
5: language=”java”
6: xmlns:xsp=”http://apache.org/xsp”>
7:
8: <date>
9: <xsp:expr>new java.util.Date().toString()</xsp:expr>
10: </date>
11: </xsp:page>
2004年出版的Professional XML Development with Apache Tools: Xerces, Xalan, FOP, Cocoon, Axis, Xindice一書稱2行是必須的,但是我在Cocoon 2.1.9下測(cè)試發(fā)現(xiàn),其中的第2行聲明可以去掉。
下面看一個(gè)在XSP里定義一個(gè)函數(shù)的例子:
1: <?xml version="1.0"?>
2: <?cocoon-process type=”xsp”?>
3:
4: <xsp:page
5: language=”java”
6: xmlns:xsp=”http://apache.org/xsp”>
7:
8: <xsp:structure>
9: <xsp:include>java.util.Date</xsp:include>
10: </xsp:structure>
11:
12: <xsp:logic>
13: String getDate() {
14: Date d = new Date();
15: return d.toString();
16: }
17: </xsp:logic>
18:
19: <date>
20: <xsp:expr>getDate()</xsp:expr>
21: </date>
22: </xsp:page>
可以看到,定義函數(shù)的代碼被放在了<xsp:logic>元素里,另外使用<xsp:structure>和其子元素<xsp:include>元素可以導(dǎo)入Java的類。
完整的XSP元素列表如下:
-
<?cocoon-process?> This processing instruction (PI) tells Cocoon how to process this file. You may have multiple cocoon-process PIs because Cocoon can process an XSP page in two different ways. It can process the document as an XSP file, causing the language code to be executed. To indicate this style of processing, specify “xsp” as the value of the type pseudo-attribute. Cocoon can also use an XSL stylesheet to transform the document. This can occur either before or after the XSP processing. The processing order is determined by the order in which the PIs appear in the document. To use a stylesheet with an XSP document, specify “xslt” as the value of the type pseudo-attribute. If you use a stylesheet with the document, you need to supply an XML stylesheet processing instruction that tells where to find the stylesheet. (See the next item.)
-
<?xml-stylesheet?> This PI is defined by the W3C’s Associating Style Sheets with XML Documents recommendation. Associating a stylesheet is easy; you supply two pseudo-attributes. The href pseudo-attribute contains the URI for the stylesheet, and the type pseudo-attribute contains the MIME type of the stylesheet, which should be “text/xsl” for XSLT stylesheets.
-
<xsp:page> The root element of an XSP page is <xsp:page>. It takes a language attribute that allows you to specify the programming language being used in the XSP. You’ll probably also define some namespace prefixes on this element. The minimum would be for you to define the xsp prefix. The <xsp:page> must contain at least one user-defined element that’s used as the root element of the XSP result.
-
<xsp:structure> This element is a container for <xsp:include> elements.
-
<xsp:include> XSP uses the <xsp:include> element to import type definitions that are needed by the rest of the XSP. In Java, these are specified either as fully qualified classnames or in wildcarded package style, like java.util.*.
-
<xsp:logic> The implementation of the logic of an XSP should be the content of the <xsp:logic> element. For Java-based XSPs, this includes member fields and methods.
-
<xsp:expr> An <xsp:expr> element invokes logic in the <xsp:logic> to return a string valued expression. In Java, this is through method calls, field accesses, or string literals. Java string literals that appear as the content of an <xsp:expr> tag must be inside double quotes (”").
-
<xsp:element> This element allows you to dynamically create an element in the output XML. The <xsp:element> element takes a name attribute whose value is the name of the element to be created. You can nest these elements to create element subtrees dynamically. You can also insert literal XML elements and character data as part of the content of this element.
-
<xsp:attribute> The <xsp:attribute> element should appear as the child of either an <xsp:element> element or a literal XML element. It allows you to dynamically create an attribute by supplying a name attribute for the name of the new attribute. The value of the new attribute is the content of the <xsp:attribute> element.
-
<xsp:comment> To create a comment in the XSP output, use the <xsp:comment> element and make the content of the element the text of your comment.
-
<xsp:pi> The <xsp:pi> element allows you to create processing instructions. You supply a target attribute that defines the PI target name. If you wish to create pseudo-attributes, you do so via <xsp:expr> elements in the content of the <xsp:pi> element. So, to create a PI that looks like <?xml-stylesheet href=”sheet.xsl” type=”text/xsl”?>, your <xsp:pi> element would look like this:
<xsp:pi target="xml-stylesheet"> <xsp:expr>"href=\"sheet.xsl\" type=\"text/xsl\""</xsp:expr> </xsp:pi>
- <xsp:content> You can use the <xsp:content> element inside an <xsp:logic> element to insert the Java code for an XSP fragment at that point on the program. This is particularly useful for inserting an XSP fragment that is to be output as the body of a loop.
- xsp:page — XSP 文檔的根元素,它必須只包含一個(gè)用戶元素
- xsp:structure、xsp:include — 允許將附加的 Java 類導(dǎo)入到 XSP 的已編譯版本中
- xsp:logic — 允許在 XSP 的已編譯版本中包含附加的編程代碼塊;這可以包含成員變量、方法或應(yīng)用程序邏輯
- xsp:expr — 允許對(duì) Java 表達(dá)式求值,并將它們的值添加到文檔
- xsp:element — 允許 XSP 動(dòng)態(tài)創(chuàng)建元素;這些元素可以用任意名稱創(chuàng)建,并且可以與任何名稱空間和前綴關(guān)聯(lián)
- xsp:attribute — 允許將屬性動(dòng)態(tài)地添加到元素;這些屬性可以用任意名稱和值創(chuàng)建,并且可以與任何名稱空間關(guān)聯(lián)
- xsp:comment — 允許將注釋添加到已生成的文檔
- xsp:pi — 允許動(dòng)態(tài)創(chuàng)建處理指令,并將之添加到已生成的文檔
- xsp:parameter — 允許為元素或?qū)傩陨擅Q
通過(guò)使用 xsp:element,還可以動(dòng)態(tài)地創(chuàng)建元素,如:
<xsp:element>
<xsp:param name="name"><xsp:expr>"myElementName"</xsp:expr></xsp:param>
Element content
</xsp:element>
這個(gè)示例顯示了用動(dòng)態(tài)生成名稱創(chuàng)建元素涉及使用 xsp:element 和 xsp:param 元素。xsp:param定義了一個(gè)參數(shù),在本例中是元素的名稱,其值是一個(gè)用于計(jì)算元素名稱的表達(dá)式。
上面的代碼將生成 XML 輸出
<myElementName>Element content</myElementName>
用這種方式創(chuàng)建的元素還可以與一個(gè)特定的名稱空間和前綴關(guān)聯(lián),如下例所示。請(qǐng)注意:這兩個(gè)名稱空間和前綴參數(shù)都是必需的;否則將產(chǎn)生錯(cuò)誤。
<xsp:element prefix="my" uri="http://www.examples.org">
<xsp:param name="name"><xsp:expr>"myElementName"</xsp:expr></xsp:param>
Element content
</xsp:element>
這個(gè)示例生成了以下 XML 輸出:
<my:myElementName xmlns:my="http://www.examples.org">Element content</my:myElementName>
如同元素一樣,可以在 XSP 頁(yè)面中動(dòng)態(tài)創(chuàng)建屬性。xsp:attribute 元素的工作原理類似于 xsp:element,它允許動(dòng)態(tài)創(chuàng)建屬性的名稱及其值:
<xsp:element>
<xsp:param name="name"><xsp:expr>"myElementName"</xsp:expr></xsp:param>
<xsp:attribute name="myAttribute">myAttributeValue</xsp:attribute>
Element content
</xsp:element>
這個(gè)屬性的名稱定義在 name 屬性內(nèi),盡管是用與 xsp:element 類似的方法定義的,但它還能通過(guò)使用 xsp:param 子元素來(lái)定義。屬性值被指定成元素內(nèi)容。這可以是一個(gè)簡(jiǎn)單文本值或更有效地由 xsp:expr 元素生成。
而 xsp:attribute 標(biāo)記不必與 xsp:element 結(jié)合使用。它可以被放置在任何用戶元素內(nèi),而且可以用相同的方法添加屬性。例如,可以通過(guò)使用調(diào)用定義在 XSP 頁(yè)面別處的方法的表達(dá)式來(lái)動(dòng)態(tài)創(chuàng)建 image 元素的 URL。
<image>
<xsp:attribute name="href"><xsp:expr>calculateImageURL()</xsp:expr></xsp:attribute>
</image>
如果生成的屬性與特定的名稱空間關(guān)聯(lián),那么這可以通過(guò)使用附加的 prefix 和 uri 屬性或 xsp:param 元素來(lái)表示,它類似于用于 xsp:element 的方法。同樣,如果只定義了其中的一個(gè),則是一個(gè)錯(cuò)誤。
創(chuàng)建注釋和處理指令
xsp:comment 和 xsp:pi 元素用于創(chuàng)建注釋和處理指令。
創(chuàng)建注釋十分簡(jiǎn)單。任何作為 xsp:comment 元素的子元素而提供的文本都變成了 XML 注釋:
<xsp:comment>This is a comment</xsp:comment>
然后這個(gè)注釋變成:
<!-- This is a comment -->
創(chuàng)建處理指令類似于創(chuàng)建動(dòng)態(tài)元素或?qū)傩浴sp:pi 元素應(yīng)該有一個(gè)標(biāo)識(shí)處理指令目標(biāo)的嵌套參數(shù)。照例對(duì) xsp:pi 元素的剩余內(nèi)容求值。這里是一個(gè)簡(jiǎn)單示例:
<xsp:pi target="myApplication">
<xsp:expr>"param1=value, param2=value, generatorTimestamp=" +
System.currentTimeMillis()</xsp:expr>
</xsp:pi>
輸出如下:
<?myApplication param1=value, param2=value, generatorTimestamp=1017407796870?>
還可以通過(guò)在 xsp:param 元素內(nèi)創(chuàng)建處理指令的目標(biāo)來(lái)自動(dòng)生成它,如同以下示例演示的那樣:
<xsp:pi>
<xsp:param name="target"><xsp:expr>"myApplication"</xsp:expr></xsp:param>
<xsp:expr>"param1=value, param2=value, generatorTimestamp=" +
System.currentTimeMillis()</xsp:expr>
</xsp:pi>