生成pdf以在XSLT中记录数据库

时间:2013-04-11 05:22:00

标签: xml xslt xsl-fo apache-fop

我有一个与数据库相关的XML,必须生成一个PDF来记录数据库。我正在使用FOP将XSLT转换为PDF。

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sql-catalog xmlns="http://www.abc.com">
    <schemas>
        <schema name="abc>
            <tables>
                <table name="tab123" degree="1" type="TABLE" rows="144">
                    <columns>
                        <column name="asd" nullable="true" order="1" sqlType="CHARACTER VARYING" sqlLength="255" fixedLength="2"/>
                        <column name="pqr" nullable="true" order="2" sqlType="CHARACTER VARYING" sqlLength="255" fixedLength="2"/>
                    </columns>
                    <constraints/>
                    <datafiles>
                        <datafile href="abc.txt" checksum="6ba3a161d5" size="12354" format="FIXED" valid="true" recordTerminator="&#xD;&#xA;" checksumtype="MD5" rows="905"/>
                    </datafiles>
                </table>
            </tables>
        </schema>
    </schemas>
</sql-catalog>

我必须显示表名,列数和列名。我在XSLT中写了以下内容。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ora="http://www.oracle.com/XSL/Transform/java/" xmlns:xdofo="http://xmlns.oracle.com/oxp/fo/extensions" xmlns:xdoxslt="http://www.oracle.com/XSL/Transform/java/oracle.apps.xdo.template.rtf.XSLTFunctions" xmlns:xdoxliff="urn:oasis:names:tc:xliff:document:1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:cat="http://www.abc.com">
    <xsl:output method="xml" encoding="ISO-8859-1"/>
    <xsl:template match="cat:sql-catalog">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="A4" page-height="297mm" page-width="210mm" margin-left="2cm" margin-right="2cm" margin-top="2.0cm" margin-bottom="1.0cm">
            <fo:region-body/>
            <fo:region-after extent="1cm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>

      <fo:page-sequence master-reference="A4">
           <fo:static-content flow-name="xsl-region-after" font-size="8pt">
               <fo:block text-align="right">
                    <fo:page-number/>
               </fo:block>
           </fo:static-content>

      <fo:flow flow-name="xsl-region-body">

            <fo:block font-weight="bold" font-size="8pt" space-after="0.5cm">
                <xsl:value-of select="cat:schemas/schema/tables/table/@name" />
            </fo:block>

        </fo:flow>
    </fo:page-sequence>
    </fo:root>
  </xsl:template>
</xsl:stylesheet>

在代码中,我试图显示属性中的表的名称,但它似乎不起作用。请问有谁请告诉我这里哪里出错了?以及如何计算表格中的列数。

3 个答案:

答案 0 :(得分:1)

提供的转换不会声明任何带有前缀"cat"的命名空间,但此前缀在匹配模式和XPath表达式中都使用。

您必须收到如下错误消息:

Saxon 9.1.0.5J from Saxonica
Java version 1.6.0_31
Error at xsl:template on line 13 column 43 of marrowtr.xsl:
  XPST0081: XSLT Pattern syntax error at char 15 on line 13 in {cat:sql-catalog}:
    Undeclared namespace prefix {cat}

<强>解决方案

替换

xmlns:sqlcat="http://www.abc.comr"

<强>与

xmlns:cat="http://www.abc.com"

答案 1 :(得分:1)

当在XML上运行下面的XSLT时会得到结果

<强> XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ora="http://www.oracle.com/XSL/Transform/java/" xmlns:xdofo="http://xmlns.oracle.com/oxp/fo/extensions" xmlns:xdoxslt="http://www.oracle.com/XSL/Transform/java/oracle.apps.xdo.template.rtf.XSLTFunctions" xmlns:xdoxliff="urn:oasis:names:tc:xliff:document:1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
    <xsl:output method="xml" encoding="ISO-8859-1"/>
    <xsl:template match="sql-catalog">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="A4" page-height="297mm" page-width="210mm" margin-left="2cm" margin-right="2cm" margin-top="2.0cm" margin-bottom="1.0cm">
            <fo:region-body/>
            <fo:region-after extent="1cm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>

      <fo:page-sequence master-reference="A4">
           <fo:static-content flow-name="xsl-region-after" font-size="8pt">
               <fo:block text-align="right">
                    <fo:page-number/>
               </fo:block>
           </fo:static-content>

      <fo:flow flow-name="xsl-region-body">

            <fo:block font-weight="bold" font-size="8pt" space-after="0.5cm">
                <xsl:value-of select="schemas/schema/tables/table/@name" />
            </fo:block>

            <fo:block font-weight="bold" font-size="8pt" space-after="0.5cm">
                <xsl:value-of select="count(schemas/schema/tables/table/columns/column)"/>
            </fo:block>

            <xsl:for-each select="schemas/schema/tables/table/columns/column">
                <fo:block font-weight="bold" font-size="8pt" space-after="0.5cm">
                <xsl:value-of select="@name"/>
                </fo:block>
            </xsl:for-each>
        </fo:flow>
    </fo:page-sequence>
    </fo:root>
  </xsl:template>
</xsl:stylesheet>

输入XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sql-catalog xmlns:sqlcat="http://www.def.com">
    <schemas>
        <schema name="abc">
            <tables>
                <table name="tab123" degree="1" type="TABLE" rows="144">
                    <columns>
                        <column name="asd" nullable="true" order="1" sqlType="CHARACTER VARYING" sqlLength="255" fixedLength="2"/>
                        <column name="pqr" nullable="true" order="2" sqlType="CHARACTER VARYING" sqlLength="255" fixedLength="2"/>
                    </columns>
                    <constraints/>
                    <datafiles>
                        <datafile href="abc.txt" checksum="6ba3a161d5" size="12354" format="FIXED" valid="true" recordTerminator="&#xD;&#xA;" checksumtype="MD5" rows="905"/>
                    </datafiles>
                </table>
            </tables>
        </schema>
    </schemas>
</sql-catalog>

<强>输出:

<?xml version='1.0' encoding='ISO-8859-1' ?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xdoxslt="http://www.oracle.com/XSL/Transform/java/oracle.apps.xdo.template.rtf.XSLTFunctions" xmlns:ora="http://www.oracle.com/XSL/Transform/java/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xdofo="http://xmlns.oracle.com/oxp/fo/extensions" xmlns:xdoxliff="urn:oasis:names:tc:xliff:document:1.1"><fo:layout-master-set><fo:simple-page-master master-name="A4" page-height="297mm" page-width="210mm" margin-left="2cm" margin-right="2cm" margin-top="2.0cm" margin-bottom="1.0cm"><fo:region-body/><fo:region-after extent="1cm"/></fo:simple-page-master></fo:layout-master-set><fo:page-sequence master-reference="A4"><fo:static-content flow-name="xsl-region-after" font-size="8pt"><fo:block text-align="right"><fo:page-number/></fo:block></fo:static-content><fo:flow flow-name="xsl-region-body"><fo:block font-weight="bold" font-size="8pt" space-after="0.5cm">tab123</fo:block><fo:block font-weight="bold" font-size="8pt" space-after="0.5cm">2</fo:block><fo:block font-weight="bold" font-size="8pt" space-after="0.5cm">asd</fo:block><fo:block font-weight="bold" font-size="8pt" space-after="0.5cm">pqr</fo:block></fo:flow></fo:page-sequence></fo:root>

答案 2 :(得分:1)

您需要使用cat命名空间为XPath表达式中的每个元素名称添加前缀。尝试:

<xsl:value-of select="cat:schemas/cat:schema/cat:tables/cat:table/@name"/>