cocoon xsp

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.
  • xsp:page — XSP 文档的根元素,它必须只包含一个用户元素
  • xsp:structure、xsp:include — 允许将附加的 Java 类导入到 XSP 的已编译版本中
  • xsp:logic — 允许在 XSP 的已编译版本中包含附加的编程代码块;这可以包含成员变量、方法或应用程序逻辑
  • xsp:expr — 允许对 Java 表达式求值,并将它们的值添加到文档
  • xsp:element — 允许 XSP 动态创建元素;这些元素可以用任意名称创建,并且可以与任何名称空间和前缀关联
  • xsp:attribute — 允许将属性动态地添加到元素;这些属性可以用任意名称和值创建,并且可以与任何名称空间关联
  • xsp:comment — 允许将注释添加到已生成的文档
  • xsp:pi — 允许动态创建处理指令,并将之添加到已生成的文档
  • xsp:parameter — 允许为元素或属性生成名称

通过使用 xsp:element,还可以动态地创建元素,如:

<xsp:element>
<xsp:param name="name"><xsp:expr>"myElementName"</xsp:expr></xsp:param>
Element content
</xsp:element>

这个示例显示了用动态生成名称创建元素涉及使用 xsp:element 和 xsp:param 元素。xsp:param定义了一个参数,在本例中是元素的名称,其值是一个用于计算元素名称的表达式。

上面的代码将生成 XML 输出

<myElementName>Element content</myElementName>

用这种方式创建的元素还可以与一个特定的名称空间和前缀关联,如下例所示。请注意:这两个名称空间和前缀参数都是必需的;否则将产生错误。

<xsp:element prefix="my" uri="http://www.examples.org">
<xsp:param name="name"><xsp:expr>"myElementName"</xsp:expr></xsp:param>
Element content
</xsp:element>

这个示例生成了以下 XML 输出:

<my:myElementName xmlns:my="http://www.examples.org">Element content</my:myElementName>

如同元素一样,可以在 XSP 页面中动态创建属性。xsp:attribute 元素的工作原理类似于 xsp:element,它允许动态创建属性的名称及其值:

<xsp:element>
<xsp:param name="name"><xsp:expr>"myElementName"</xsp:expr></xsp:param>
<xsp:attribute name="myAttribute">myAttributeValue</xsp:attribute>
Element content
</xsp:element>

这个属性的名称定义在 name 属性内,尽管是用与 xsp:element 类似的方法定义的,但它还能通过使用 xsp:param 子元素来定义。属性值被指定成元素内容。这可以是一个简单文本值或更有效地由 xsp:expr 元素生成。

而 xsp:attribute 标记不必与 xsp:element 结合使用。它可以被放置在任何用户元素内,而且可以用相同的方法添加属性。例如,可以通过使用调用定义在 XSP 页面别处的方法的表达式来动态创建 image 元素的 URL。

<image>
<xsp:attribute name="href"><xsp:expr>calculateImageURL()</xsp:expr></xsp:attribute>
</image>

如果生成的属性与特定的名称空间关联,那么这可以通过使用附加的 prefix 和 uri 属性或 xsp:param 元素来表示,它类似于用于 xsp:element 的方法。同样,如果只定义了其中的一个,则是一个错误。

创建注释和处理指令

xsp:comment 和 xsp:pi 元素用于创建注释和处理指令。

创建注释十分简单。任何作为 xsp:comment 元素的子元素而提供的文本都变成了 XML 注释:

<xsp:comment>This is a comment</xsp:comment>

然后这个注释变成:

<!-- This is a comment -->

创建处理指令类似于创建动态元素或属性。xsp:pi 元素应该有一个标识处理指令目标的嵌套参数。照例对 xsp:pi 元素的剩余内容求值。这里是一个简单示例:

<xsp:pi target="myApplication">
<xsp:expr>"param1=value, param2=value, generatorTimestamp=" +
System.currentTimeMillis()</xsp:expr>
</xsp:pi>

输出如下:

<?myApplication param1=value, param2=value, generatorTimestamp=1017407796870?>

还可以通过在 xsp:param 元素内创建处理指令的目标来自动生成它,如同以下示例演示的那样:

<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>

posted on 2007-01-26 12:02 leoli 阅读(436) 评论(0)  编辑  收藏 所属分类: Frame


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿(6)

随笔分类

随笔档案(17)

文章分类(86)

收藏夹(3)

flex blog

good site

java blog

my friend

tools

抓虾

搜索

最新评论

阅读排行榜

评论排行榜