2006-06-19

学习XSP 1

XSP (eXtensible Server Page )作为Cocoon的一个重要的组成部分可以为Cocoon的pipeline生成XML文件。XSP文件本身是XML文件,遵循XML的规范,但是支持在XML文件中包含程序代码。XSP借助Bean Scripting Framework支持多种语言,包括Java,Javascript,Python 等。

XSP文件的根元素为<page>,通常使用xsp名字空间(<xsp:page>)。
一个简单的例子如:


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下测试发现,其中的第2行声明可以去掉。

下面看一个在XSP里定义一个函数的例子:

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>


可以看到,定义函数的代码被放在了<xsp:logic>元素里,另外使用<xsp:structure>和其子元素<xsp:include>元素可以导入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.

1 条评论:

  1. 在使用元素时,如果引用了一个在里没有定义的变量会引发一个错误。另外要注意元素内没有分号来结束,如:getDate()

    回复删除