使用xsl-fo将xml文档转换为PDF

时间:2014-12-17 15:01:32

标签: xml xslt xsl-fo

我想使用xsl-fo将我的xml文档转换为PDF。当我尝试在左上边距插入图像时,我收到一个错误: Javax.xml.transform.TransformetException:org.apache.fop.fo.ValidationException: “fo:external-graphic”不是“fo:table-body”的有效孩子! (没有上下文信息)。有人可以解释一下我做错了什么,并告诉我如何在左上边缘插入图像?

xsl-fo code:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" version="1.0" indent="yes" encoding="UTF-8" />

<xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <!--First page-->
        <fo:layout-master-set>
          <fo:simple-page-master master-name="FirstPage" margin="1cm">
            <fo:region-body  margin="2cm" margin-left="1cm" margin-right="1cm"/>
            <fo:region-before extent="3cm"/>

      <fo:region-start extent="3cm" region-name="LeftMargin"/>  
      <fo:region-after extent="0.5cm"/>
          </fo:simple-page-master>

      <fo:simple-page-master master-name="Druga" margin="1cm">
        <fo:region-body  margin="2cm" margin-left="1cm" margin-right="1cm"/>
        <fo:region-before extent="3cm"/>
        <fo:region-after extent="0.5cm"/>
      </fo:simple-page-master>

        </fo:layout-master-set>

        <fo:page-sequence master-reference="FirstPage">
            <fo:static-content flow-name="xsl-region-before">
                <fo:block font-size="24pt" font-family="Calibri">Players data</fo:block>
            </fo:static-content>

      <fo:static-content flow-name="LeftMargin">
        <fo:block text-align="end" >
          <xsl:apply-templates select="data/wydawnictwo/logo" />
        </fo:block>
      </fo:static-content>


      <fo:static-content flow-name="xsl-region-after">
        <fo:block font-size="10pt" font-family="Calibri">
          <xsl:call-template name="dataCzas"/>
        </fo:block>

        <fo:block text-align="end" font-size="80%">
          <xsl:text>Page </xsl:text>
          <fo:page-number />
        </fo:block>

      </fo:static-content>


            <fo:flow flow-name="xsl-region-body">
                <fo:block font-size="12pt" font-family="Calibri" padding-after="1cm">
                    <fo:table table-layout="fixed" width="100%" border="solid black 1px">
                    <fo:table-column column-width="13mm"/>
                    <fo:table-column column-width="30mm"/>
                    <fo:table-column column-width="30mm"/>
                    <fo:table-column column-width="13mm"/>
                    <fo:table-column column-width="25mm"/>
          <fo:table-column column-width="25mm"/>
                        <fo:table-header>
                            <fo:table-row>
                                <fo:table-cell border="solid black 2px">
                                    <fo:block font-weight="bold" background-color="#cccccc">Num.</fo:block>
                                </fo:table-cell>
                                <fo:table-cell border="solid black 2px">
                                    <fo:block font-weight="bold" background-color="#cccccc">First name</fo:block>
                                </fo:table-cell>
                                <fo:table-cell border="solid black 2px">
                                    <fo:block font-weight="bold" background-color="#cccccc">Surname</fo:block>
                                </fo:table-cell>
                                <fo:table-cell border="solid black 2px">
                                    <fo:block font-weight="bold" background-color="#cccccc">Age</fo:block>
                                </fo:table-cell>
                                <fo:table-cell border="solid black 2px">
                                    <fo:block font-weight="bold" background-color="#cccccc">Country</fo:block>
                                </fo:table-cell>
                <fo:table-cell border="solid black 2px">
                  <fo:block font-weight="bold" background-color="#cccccc">Town</fo:block>
                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-header>
                        <fo:table-body>
                            <xsl:apply-templates />
                        </fo:table-body>
                    </fo:table>
                </fo:block>
            </fo:flow>

        </fo:page-sequence>

    <!--Second page-->
    <fo:page-sequence master-reference="Druga">
      <fo:static-content flow-name="xsl-region-before">
        <fo:block font-size="24pt" font-family="Calibri">Filmoteka</fo:block>
      </fo:static-content>

      <fo:static-content flow-name="xsl-region-after">
        <fo:block font-size="10pt" font-family="Calibri">
          <xsl:call-template name="dataCzas"/>
        </fo:block>

        <fo:block text-align="end" font-size="80%">
          <xsl:text>Page </xsl:text>
          <fo:page-number />
        </fo:block>
      </fo:static-content>

      <fo:flow flow-name="xsl-region-body">
        <fo:block font-size="12pt" font-family="Calibri" padding-after="1cm">
          <fo:table table-layout="fixed" width="100%" border="solid black 1px">
            <fo:table-column column-width="12mm"/>
            <fo:table-column column-width="40mm"/>
            <fo:table-column column-width="40mm"/>
            <fo:table-column column-width="13mm"/>
            <fo:table-column column-width="25mm"/>
            <fo:table-column column-width="25mm"/>
            <fo:table-header>
              <fo:table-row>
                <fo:table-cell border="solid black 2px">
                  <fo:block font-weight="bold" background-color="#cccccc">Num.</fo:block>
                </fo:table-cell>
                <fo:table-cell border="solid black 2px">
                  <fo:block font-weight="bold" background-color="#cccccc">First name</fo:block>
                </fo:table-cell>
                <fo:table-cell border="solid black 2px">
                  <fo:block font-weight="bold" background-color="#cccccc">Surname</fo:block>
                </fo:table-cell>
                <fo:table-cell border="solid black 2px">
                  <fo:block font-weight="bold" background-color="#cccccc">Age</fo:block>
                </fo:table-cell>
                <fo:table-cell border="solid black 2px">
                  <fo:block font-weight="bold" background-color="#cccccc">Country</fo:block>
                </fo:table-cell>
                <fo:table-cell border="solid black 2px">
                  <fo:block font-weight="bold" background-color="#cccccc">Town</fo:block>
                </fo:table-cell>
              </fo:table-row>
            </fo:table-header>
            <fo:table-body>
              <xsl:apply-templates />
            </fo:table-body>
          </fo:table>
        </fo:block>
      </fo:flow>

    </fo:page-sequence>

    </fo:root>
</xsl:template>


<xsl:template match="players_data/player_data">
    <fo:table-row>
        <fo:table-cell border="solid black 1px">
            <fo:block><xsl:number format="1"/></fo:block>
        </fo:table-cell>
        <fo:table-cell border="solid black 1px">
            <fo:block font-family="Calibri"><xsl:value-of select="first_name"/></fo:block>
        </fo:table-cell>
        <fo:table-cell border="solid black 1px">
            <fo:block font-family="Calibri"><xsl:value-of select="surname"/></fo:block>
        </fo:table-cell>
        <fo:table-cell border="solid black 1px">
            <fo:block font-family="Calibri"><xsl:value-of select="age"/></fo:block>
        </fo:table-cell>
        <fo:table-cell border="solid black 1px">
            <fo:block font-family="Calibri"><xsl:value-of select="country"/></fo:block>
        </fo:table-cell>
    <fo:table-cell border="solid black 1px">
      <fo:block font-family="Calibri">
        <xsl:value-of select="town"/>
      </fo:block>
    </fo:table-cell>
    </fo:table-row>
</xsl:template>

<xsl:template name="dataCzas">
    <xsl:value-of select="java:format(java:java.text.SimpleDateFormat.new('dd MMMM yyyy, HH:mm:ss'), java:java.util.Date.new())"/>
</xsl:template>

  <xsl:template match="logo" >
    <xsl:element name="fo:external-graphic">
      <xsl:attribute name="src">
        <xsl:value-of select="@src" />
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

Xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="DaneXSL.xsl"?>

<data>
  <wydawnictwo>
    <nazwa>PG_1</nazwa>
    <logo src="Logo.jpg" />
  </wydawnictwo>

  <zbior na="marzec 2006">
    <ksiazka ISBN="83-7197-255-X" >
      <!-- wydanie 1 z grudnia 2003 -->
      <autor>Alicja Marcinkiewiczowska</autor>
      <autor plec="M"> Maria Jakub </autor>
      <tytul>Rozprawa na temat blizej nieokreślony.</tytul>
      <recenzja>
        Jest to bardzo dobra publikacja. Książkę czyta się
        przyjemnie i łatwo przyswaja przedstawioną tematykę.
      </recenzja>
    </ksiazka>
    <ksiazka>
      <autor>Alicja Marcinkiewiczowska</autor>
      <tytul>Nowe języki na bazie metajezyka XML</tytul>
      <recenzja>
        ciekawa pozycja - polecam dla usystematyzowania
        posiadanych informacji
      </recenzja>
    </ksiazka>
    <ksiazka ISBN="er/34/o56/2004" >
      <autor> Radosław Mały </autor>
      <tytul> Stare języki etniczne a metajęzyk XML </tytul>
      <recenzja> pozycja godna polecenia koneserom jezyków </recenzja>
    </ksiazka>
  </zbior>

<!--For first page-->
    <players_data>
    <player_data>
      <first_name>Tony</first_name>
      <surname>Hanks</surname>
      <age>23</age>

      <country continent="EUROPE">England</country>
      <town>London</town>
      <street>Long Street</street>
      <postal_code>81-100</postal_code>
      <favorite_character>Aatrox</favorite_character>
    </player_data>
    <player_data>
     <first_name>Richard</first_name>
      <surname>Dimitri</surname>
      <age>31</age>

      <country continent="EUROPE">Scotland</country>
      <town>Aberdeen</town>
      <street>Keynes Street</street>
      <postal_code>14-354</postal_code>
      <favorite_character>Lux</favorite_character>
    </player_data>
    <player_data>
      <first_name>Denis</first_name>
      <surname>Henks</surname>
      <age>14</age>

        <country continent="EUROPE">England</country>
      <town>Manchester</town>
      <street>Short Street</street>
      <postal_code>32-323</postal_code>
      <favorite_character>Garen</favorite_character>
    </player_data>
    <player_data>
    <first_name>David</first_name>
      <surname>Szlaufenberg</surname>
      <age>36</age>

      <country continent="NORTH AMERICA">U.S.A</country>
      <town>New York</town>
      <street>Long Island</street>
      <postal_code>34-555</postal_code>
      <favorite_character>Sivir</favorite_character>
  </player_data>
    <player_data>
    <first_name>Rick</first_name>
      <surname>Wasiliev</surname>
      <age>16</age>

      <country continent="NORTH AMERICA">U.S.A</country>
      <town>Chicago</town>
      <street>Long Awenue</street>
      <postal_code>45-525</postal_code>
      <favorite_character>Sivir</favorite_character>
    </player_data>  
    <player_data>
      <first_name>Annah</first_name>
      <surname>Darcy</surname>
      <age>32</age>   
        <country continent="SOUTH AMERICA">U.S.A</country>
      <town>Rio De Janeiro</town>
      <street>Buenosdias</street>
      <postal_code>25-525</postal_code>
      <favorite_character>Teemo</favorite_character>
   </player_data>   
  </players_data>



</data>

1 个答案:

答案 0 :(得分:2)

你有:

<xsl:template match="/">

    (some stuff...)

        <fo:table-body>
          <xsl:apply-templates />
        </fo:table-body>
  (and so on...)

您的<xsl:apply-templates />将再次处理<logo>标记。您应该插入类似<xsl:apply-templates select="data/players_data/player_data" />的内容。