XSL:RML 报表

RML 报表不需要编程,但是需要两种简单的 XML 文件:

  • 一种文件描述导出数据 (*.xml)

  • 一种可以包含数据和表现规则的文件 (*.xsl)

/doc_static/5.0/_images/automatic-reports.png

XML模板(template)主要描述哪些域(field)的资源需要(由server)导出. XSL:RML 样式表单(style sheet)处理输出数据,就像报表(report)中的 “static text” 一样. “static text” 是指在报表中相同、不怎么变化的部分,比如表格头部的标题(title of table column).

范例

下面给出一个例子,说明生成ERP报表的不同文件.

/doc_static/5.0/_images/ids-report.png

XML 模板

  1. <?xml version="1.0"?>
  2. <ids>
  3. <id type="fields" name="id">
  4. <name type="field" name="name"/>
  5. <ref type="field" name="ref"/>
  6. </id>
  7. </ids>

XML 数据文件 (生成的)

  1. <?xml version="1.0"?>
  2. <ids>
  3. <id>
  4. <name>Tiny sprl</name>
  5. <ref>pnk00</ref>
  6. </id><id>
  7. <name>ASUS</name>
  8. <ref></ref>
  9. </id><id>
  10. <name>Agrolait</name>
  11. <ref></ref>
  12. </id><id>
  13. <name>Banque Plein-Aux-As</name>
  14. <ref></ref>
  15. </id><id>
  16. <name>China Export</name>
  17. <ref></ref>
  18. </id><id>
  19. <name>Ditrib PC</name>
  20. <ref></ref>
  21. </id><id>
  22. <name>Ecole de Commerce de Liege</name>
  23. <ref></ref>
  24. </id><id>
  25. <name>Elec Import</name>
  26. <ref></ref>
  27. </id><id>
  28. <name>Maxtor</name>
  29. <ref></ref>
  30. </id><id>
  31. <name>Mediapole SPRL</name>
  32. <ref></ref>
  33. </id><id>
  34. <name>Opensides sprl</name>
  35. <ref>os</ref>
  36. </id><id>
  37. <name>Tecsas sarl</name>
  38. <ref></ref>
  39. </id>
  40. </ids>

XSL 样式表

  1. <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  2. <xsl:template match="/">
  3. <xsl:apply-templates select="ids"/>
  4. </xsl:template>
  5. <xsl:template match="ids">
  6. <document>
  7. <template pageSize="21cm,29.7cm">
  8. <pageTemplate>
  9. <frame id="col1" x1="2cm" y1="2.4cm" width="8cm" height="26cm"/>
  10. <frame id="col2" x1="11cm" y1="2.4cm" width="8cm" height="26cm"/>
  11. </pageTemplate>
  12. </template>
  13. <stylesheet>
  14. <blockTableStyle id="ids">
  15. <blockFont name="Helvetica-BoldOblique" size="12" start="0,0" stop="-1,0"/>
  16. <lineStyle kind="BOX" colorName="black" start="0,0" stop="-1,0"/>
  17. <lineStyle kind="BOX" colorName="black" start="0,0" stop="-1,-1"/>
  18. </blockTableStyle>
  19. </stylesheet>
  20. <story>
  21. <blockTable colWidths="2cm, 6cm" repeatRows="1" style="ids">
  22. <tr>
  23. <td t="1">Ref.</td>
  24. <td t="1">Name</td>
  25. </tr>
  26. <xsl:apply-templates select="id"/>
  27. </blockTable>
  28. </story>
  29. </document>
  30. </xsl:template>
  31. <xsl:template match="id">
  32. <tr>
  33. <td><xsl:value-of select="ref"/></td>
  34. <td><para><xsl:value-of select="name"/></para></td>
  35. </tr>
  36. </xsl:template>
  37. </xsl:stylesheet>

对应的RML文件 (生成的)

  1. <?xml version="1.0"?>
  2. <document>
  3. ...
  4. <story>
  5. <blockTable colWidths="2cm, 6cm" repeatRows="1" style="ids">
  6. <tr>
  7. <td t="1">Ref.</td>
  8. <td t="1">Name</td>
  9. </tr>
  10. <tr>
  11. <td>pnk00</td>
  12. <td><para>Tiny sprl</para></td>
  13. </tr>
  14. <tr>
  15. <td></td>
  16. <td><para>ASUS</para></td>
  17. </tr>
  18. <tr>
  19. <td></td>
  20. <td><para>Agrolait</para></td>
  21. </tr>
  22. <tr>
  23. <td></td>
  24. <td><para>Banque Plein-Aux-As</para></td>
  25. </tr>
  26. <tr>
  27. <td></td>
  28. <td><para>China Export</para></td>
  29. </tr>
  30. <tr>
  31. <td></td>
  32. <td><para>Ditrib PC</para></td>
  33. </tr>
  34. <tr>
  35. <td></td>
  36. <td><para>Ecole de Commerce de Liege</para></td>
  37. </tr>
  38. <tr>
  39. <td></td>
  40. <td><para>Elec Import</para></td>
  41. </tr>
  42. <tr>
  43. <td></td>
  44. <td><para>Maxtor</para></td>
  45. </tr>
  46. <tr>
  47. <td></td>
  48. <td><para>Mediapole SPRL</para></td>
  49. </tr>
  50. <tr>
  51. <td>os</td>
  52. <td><para>Opensides sprl</para></td>
  53. </tr>
  54. <tr>
  55. <td></td>
  56. <td><para>Tecsas sarl</para></td>
  57. </tr>
  58. </blockTable>
  59. </story>
  60. </document>

更多所用格式的帮助:

这些格式都是作为 XML specification. 的拓展。

XML 模板

XML 模板(template)是简单XML文件,用来描述报表所有可用的object字段(field)中哪些字段(object fields)是有用的(available).

文件格式

标签(tag)名称可以任意(但在XML中必须是有效的)。XSL文件中,你要使用到这些名称。大多数情况下,标签名(the name of tag)和引用的对象字段(object field)是一致的.

Nodes without type attribute are transferred identically into the XML destination file (the data file). Nodes with a type attribute will be parsed by the server and their content will be replaced by data coming from objects. In addition to the type attribute, nodes have other possible attributes. These attributes depend on the type of the node (each node type supports or needs different attributes). Most node types have a name attribute, which refers to the name of a field of the object on which we work.

关于object中的 “browse” 方法,报表的字段名称是可以使用一种类似面向对象语言中的notation来表示。这意为着 “(关联字段)是可以用这种类似 “bridges” (桥)的方式获得相关对象数据.

让我们试用 “account.transfer” 对象来举例。Account.transfer对象包含一个partner_id字段,这个字段是一 个指向”res.partner” 对象的 (“many to one”)(多对一)的关系。我们假设要创建一个转账(transfers)的报表,,而报表中需要从partner中选择收款人。我们可以这样写以获得收 款人的名字字段:

partner_id.name

其它类型

这里是可用的字段类型的列表:

  • field: 最简单的类型。有这种类型的结点(node),server都会用已知字段(field)的名字属性替换掉node的内容.

  • fields: 这种结点(node)类型,server会在XML数据文件中生成一个和已知名字属性唯一相同的结点(?).

注释:

** 这个结点(node)类型经常使用 “id” 作为名字属性。user使用选择resource的接口创建结点(node)时,会受到影响. ** 结点(node)的语法 和SQL语句 “SELECT FROM object_table WHERE id in identifier_list GROUP BY field_name” 相似,其中identifier_list 是::user (in the interface)可以通过接口选择使用的resource .

  • eval: 这个结点(node)类型计算 expr 属性中表达式(expression)的值。表达式(expression)可以是python表达式,也可以是object的字段名字.

  • zoom: 这个结点(node)类型允许 “enter” 通过relation字段使用name属性中列出的资源. 意味着子结点(child node)不用其他对象名字字段做前缀也可以使用资源字段(fields of that source)。在上面的例子中,我们可以通过以下方式获得parter的name字段:

  1. <partner type="zoom" name="partner_id">
  2. <name type="field" name="name"/>
  3. </partner>
  4. In this precise case, there is of course no point in using this notation instead of the standard notation below:
  5. <name type="field" name="partner_id.name"/>

zoom 类型往往用于恢复同一个object的多个字段(field).

  • function: 返回name属性中所调用函数的结果。这个函数必须是提前定义的。暂时可用的函数是today,用来返回现在日期.

  • call: calls对象name属性是方法名称,args属性是参数名称。函数的结果放进一个字典(dictionary),形如:{‘name_of_variable’: value, … } 这个结果可以被子结点使用。使用call类型的结点必须有相应的value属性,以和调用方法所返回的字典键(key)匹配.

样例:

  1. <cost type="call" name="compute_seller_costs" args="">
  2. <name value="name"/>
  3. <amount value="amount"/>
  4. </cost>

TODO: documenter format methode appellée def compute_buyer_costs(self, cr, uid, ids, *args):

  • attachment: 提取属性name中对应id的资源,作为报表的图像(image).

范例:

XSL:RML 报表 - 图3

范例

XML 文件的例子:

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <transfer-list>
  3. <transfer type="fields" name="id">
  4. <name type="field" name="name"/>
  5. <partner_id type="field" name="partner_id.name"/>
  6. <date type="field" name="date"/>
  7. <type type="field" name="type"/>
  8. <reference type="field" name="reference"/>
  9. <amount type="field" name="amount"/>
  10. <change type="field" name="change"/>
  11. </transfer>
  12. </transfer-list>

RML 介绍

有关RML格式的更多信息, 请参照官方文档.

XSL:RML 样式表

生成报表的XSL样式表有两个可选项. 两者都可以我们自己定制, 或者,使用我们自己定制的模板(template)

可以是freestyle,也可以用 corporate_defaults + rml_template

引入 rml_template.xsl

需要的模板:

  • frames?

  • stylesheet

  • story

可选的模板:

Translations

As OpenERP can be used in several langages, reports must be translatable. But in a report, everything doesn’t have to be translated : only the actual text has to be translated, not the formatting codes. A field will be processed by the translation system if the XML tag which surrounds it (whatever it is) has a t=”1” attribute. The server will translate all the fields with such attributes in the report generation process.

有用的链接:

Example (with corporate defaults):

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" :xmlns:fo="http://www.w3.org/1999/XSL/Format">
  2. <xsl:import href="../../custom/corporate_defaults.xsl"/>
  3. <xsl:import href="../../base/report/rml_template.xsl"/>
  4. <xsl:variable name="page_format">a4_normal</xsl:variable>
  5. <xsl:template match="/">
  6. <xsl:call-template name="rml"/>
  7. </xsl:template>
  8. <xsl:template name="stylesheet">
  9. </xsl:template>
  10. <xsl:template name="story">
  11. <xsl:apply-templates select="transfer-list"/>
  12. </xsl:template>
  13. <xsl:template match="transfer-list">
  14. <xsl:apply-templates select="transfer"/>
  15. </xsl:template>
  16. <xsl:template match="transfer">
  17. <setNextTemplate name="other_pages"/>
  18. <para>
  19. Document: <xsl:value-of select="name"/>
  20. </para><para>
  21. Type: <xsl:value-of select="type"/>
  22. </para><para>
  23. Reference: <xsl:value-of select="reference"/>
  24. </para><para>
  25. Partner ID: <xsl:value-of select="partner_id"/>
  26. </para><para>
  27. Date: <xsl:value-of select="date"/>
  28. </para><para>
  29. Amount: <xsl:value-of select="amount"/>
  30. </para>
  31. <xsl:if test="number(change)>0">
  32. <para>
  33. Change: <xsl:value-of select="change"/>
  34. </para>
  35. </xsl:if>
  36. <setNextTemplate name="first_page"/>
  37. <pageBreak/>
  38. </xsl:template>
  39. </xsl:stylesheet>