XML to FO

Das Beispiel beschreibt wie eine erstellte XML Datei und eine XSL Datei in das Ausgabeformat FOP transferiert wird. Die Beschreibungen über die Abhängigkeiten, befinden sich im Menüpunkt eine Ebene höher.

class XmlToFo


package de.snowbits.fop;

import java.io.OutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

public class XmlToFo extends Generator {

	@Override
	public void convert(Source xsltSource, Source xmlSource, String outputPDF)
			throws Exception {
		OutputStream out = new java.io.FileOutputStream(outputPDF);
		try {
			Result res = new StreamResult(outputPDF);
			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer(xsltSource);

			transformer.transform(xmlSource, res);
		} finally {
			out.close();
		}
	}

}
	

class XmlToFoTest


package de.snowbits.fop;

import java.io.File;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class XmlToFoTest  extends AbstractFoTestBase {

	private static final String OUTPUT_FO_PATH = RESOURCES_PATH + "Adressbuch.fo";
	
	@Before
	public void setUp() throws Exception {
		deleteFile(OUTPUT_FO_PATH);
	}

	@After
	public void tearDown() throws Exception {
		deleteFile(OUTPUT_FO_PATH);
	}

	@Test
	public void convertXslStremXmlStreamPdfFileParams() throws Exception {
		testee().convert(getXslStylesStream(), getXmlDataStream(), new File(OUTPUT_FO_PATH));
		assertTest();
	}

	@Test
	public void convertXslStremXmlStreamPdfStringParams() throws Exception {
		testee().convert(getXslStylesStream(), getXmlDataStream(), OUTPUT_FO_PATH);
		assertTest();
	}
	
	@Test
	public void convertXslStringXmlStreamPdfFileParams() throws Exception {
		testee().convert(INPUT_XSL_PATH, getXmlDataStream(), new File(OUTPUT_FO_PATH));
		assertTest();
	}
	
	@Test
	public void convertXslStringXmlStreamPdfStringParams() throws Exception {
		testee().convert(INPUT_XSL_PATH, getXmlDataStream(), OUTPUT_FO_PATH);
		assertTest();
	}
	
	@Test
	public void convertOnlyForFileParams() throws Exception {
		testee().convert(new File(INPUT_XSL_PATH), new File(INPUT_XML_PATH), new File(OUTPUT_FO_PATH));
		assertTest();
	}
	
	@Test
	public void convertOnlyForStringParams() throws Exception {
		testee().convert(INPUT_XSL_PATH, INPUT_XML_PATH, OUTPUT_FO_PATH);
		assertTest();
	}

	private void assertTest() {
		Assert.assertTrue("File not exist", existFile(OUTPUT_FO_PATH));
		Assert.assertTrue("File is empty", getFileSize(OUTPUT_FO_PATH) > 0);
	}
	
	private XmlToFo testee() {
		return new XmlToFo();
	}
	
}
	

Adressen.xml


<?xml version="1.0" encoding="UTF-8"?>
<adressen>
   <adresse id="1">
      <name>Max Mustermann</name>
      <strasse>Mustertraße 45</strasse>
      <plz>12345</plz>
      <ort>Musterstadt</ort>
   </adresse>
   <adresse id="2">
      <name>Maxi Musterfrau</name>
      <strasse>Musterweg 36</strasse>
      <plz>12345</plz>
      <ort>Musterstadt</ort>
   </adresse>
   <adresse id="3">
      <name>Max und Moritz</name>
      <strasse>Zu Hause 1a</strasse>
      <plz>12345</plz>
      <ort>Musterstadt</ort>
   </adresse>
</adressen>
	

Adressen.xsl


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" 
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
				xmlns:fo="http://www.w3.org/1999/XSL/Format" 
				xmlns:xlink="http://www.w3.org/1999/xlink">               

   <!-- ATTRIBUT-SETS -->
   <xsl:attribute-set name="header-color">
      <xsl:attribute name="background-color">#9bafde</xsl:attribute>
      <xsl:attribute name="color">#ffffff</xsl:attribute>
   </xsl:attribute-set>   
   <xsl:attribute-set name="cell">
      <xsl:attribute name="background-color">#FFFFFF</xsl:attribute>
      <xsl:attribute name="color">#000000</xsl:attribute>
      <xsl:attribute name="padding-start">5.15pt</xsl:attribute>
      <xsl:attribute name="vertical-align">top</xsl:attribute>
      <xsl:attribute name="padding-top">0.0pt</xsl:attribute>
      <xsl:attribute name="padding-end">5.15pt</xsl:attribute>
      <xsl:attribute name="number-columns-spanned">1</xsl:attribute>
      <xsl:attribute name="height">0.0pt</xsl:attribute>
      <xsl:attribute name="padding-bottom">0.0pt</xsl:attribute>
   </xsl:attribute-set>
   <xsl:attribute-set name="border">
      <xsl:attribute name="border-top">.10pt solid #000000</xsl:attribute>
      <xsl:attribute name="border-bottom">.10pt solid #000000</xsl:attribute>
      <xsl:attribute name="border-start-width"> .10pt</xsl:attribute>
      <xsl:attribute name="border-start-color">#000000</xsl:attribute>
      <xsl:attribute name="border-start-style">solid</xsl:attribute>
      <xsl:attribute name="border-end-width">.10pt</xsl:attribute>
      <xsl:attribute name="border-end-color">#000000</xsl:attribute>
      <xsl:attribute name="border-end-style">solid</xsl:attribute>
   </xsl:attribute-set>
   <xsl:attribute-set name="body-font">
      <xsl:attribute name="height">12.0pt</xsl:attribute>
      <xsl:attribute name="font-family">Times</xsl:attribute>
      <xsl:attribute name="white-space-collapse">false</xsl:attribute>
      <xsl:attribute name="font-size">10pt</xsl:attribute>
      <xsl:attribute name="font-weight">normal</xsl:attribute>
   </xsl:attribute-set>   

   <!-- SEITENAUFTEILUNG -->
   <xsl:template match="/">
      <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
         <fo:layout-master-set>
            <fo:simple-page-master master-name="DIN-A4"
                  page-height="29.7cm" page-width="21cm"
                  margin-top="2cm"     margin-bottom="2cm"
                  margin-left="2.5cm"  margin-right="2.5cm">
               <fo:region-body
                  margin-top="1.5cm" margin-bottom="1.8cm"
                  margin-left="2cm"  margin-right="2.5cm"/>
               <fo:region-before region-name="header" extent="1.3cm"/>
               <fo:region-after  region-name="footer" extent="1.5cm"/>
               <fo:region-start  region-name="left"   extent="1cm"/>
               <fo:region-end    region-name="right"  extent="2cm"/>
            </fo:simple-page-master>
         </fo:layout-master-set>
         <fo:page-sequence master-reference="DIN-A4">
            <fo:static-content flow-name="header">
               <fo:block font-size="14pt" text-align="center">
                  Mein Adressbuch
               </fo:block>
            </fo:static-content>
            <fo:static-content flow-name="footer">
               <fo:block text-align="center">
                  Seite <fo:page-number/> von <fo:page-number-citation ref-id="LastPage"/>
               </fo:block>
            </fo:static-content>
            <fo:flow flow-name="xsl-region-body">
               <xsl:apply-templates/>
               <fo:block id="LastPage"/>
            </fo:flow>
         </fo:page-sequence>
      </fo:root>
   </xsl:template>

   <!-- TEMPLATE Adressen = Root -->
   <xsl:template match="adressen">
      <fo:table xsl:use-attribute-sets="border" table-layout="fixed" width="100%">
         <fo:table-column column-width="3cm"/>
         <fo:table-column column-width="3cm"/>
         <fo:table-column column-width="4cm"/>
         <fo:table-header>
            <xsl:call-template name="table-head"/>
         </fo:table-header>
         <fo:table-body>
            <xsl:apply-templates select="adresse"/>
         </fo:table-body>
      </fo:table>
   </xsl:template>

   <!-- TABELLENKOPF -->
   <xsl:template name="table-head">
      <fo:table-row>
         <fo:table-cell xsl:use-attribute-sets="cell border header-color">
            <fo:block xsl:use-attribute-sets="body-font"
                      text-align="center">Name</fo:block>
         </fo:table-cell>
         <fo:table-cell xsl:use-attribute-sets="cell border header-color">
            <fo:block xsl:use-attribute-sets="body-font"
                      text-align="center">Straße</fo:block>
         </fo:table-cell>
         <fo:table-cell xsl:use-attribute-sets="cell border header-color">
            <fo:block xsl:use-attribute-sets="body-font"
                      text-align="center">PLZ und Ort</fo:block>
         </fo:table-cell>
      </fo:table-row>
   </xsl:template>
   
   <!-- TEMPLATE adresse -->
   <xsl:template match="adresse">
      <fo:table-row>
         <fo:table-cell xsl:use-attribute-sets="cell border">
            <fo:block xsl:use-attribute-sets="body-font">
               <xsl:value-of select="name"/>
            </fo:block>
         </fo:table-cell>
         <fo:table-cell xsl:use-attribute-sets="cell border">
            <fo:block xsl:use-attribute-sets="body-font">
               <xsl:value-of select="strasse"/>
            </fo:block>
         </fo:table-cell>
         <fo:table-cell xsl:use-attribute-sets="cell border">
            <fo:block xsl:use-attribute-sets="body-font">
               <xsl:value-of select="plz"/>
               <xsl:text> </xsl:text>
               <xsl:value-of select="ort"/>
            </fo:block>
         </fo:table-cell>
      </fo:table-row>
   </xsl:template>

</xsl:stylesheet>
	

Ergebnis: Adressbuch.fo


<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xlink="http://www.w3.org/1999/xlink">
	<fo:layout-master-set>
		<fo:simple-page-master master-name="DIN-A4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
			<fo:region-body margin-top="1.5cm" margin-bottom="1.8cm" margin-left="2cm" margin-right="2.5cm"/>
			<fo:region-before region-name="header" extent="1.3cm"/>
			<fo:region-after region-name="footer" extent="1.5cm"/>
			<fo:region-start region-name="left" extent="1cm"/>
			<fo:region-end region-name="right" extent="2cm"/>
		</fo:simple-page-master>
	</fo:layout-master-set>
	<fo:page-sequence master-reference="DIN-A4">
		<fo:static-content flow-name="header">
			<fo:block font-size="14pt" text-align="center">Mein Adressbuch</fo:block>
		</fo:static-content>
		<fo:static-content flow-name="footer">
			<fo:block text-align="center">Seite<fo:page-number/> von <fo:page-number-citation ref-id="LastPage"/>
			</fo:block>
		</fo:static-content>
		<fo:flow flow-name="xsl-region-body">
			<fo:table border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid" table-layout="fixed" width="100%">
				<fo:table-column column-width="3cm"/>
				<fo:table-column column-width="3cm"/>
				<fo:table-column column-width="4cm"/>
				<fo:table-header>
					<fo:table-row>
						<fo:table-cell background-color="#9bafde" color="#ffffff" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal" text-align="center">Name</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#9bafde" color="#ffffff" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal" text-align="center">Straße</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#9bafde" color="#ffffff" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal" text-align="center">PLZ und Ort</fo:block>
						</fo:table-cell>
					</fo:table-row>
				</fo:table-header>
				<fo:table-body>
					<fo:table-row>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Max Mustermann</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Mustertraße 45</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">12345 Musterstadt</fo:block>
						</fo:table-cell>
					</fo:table-row>
					<fo:table-row>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Maxi Musterfrau</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Musterweg 36</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">12345 Musterstadt</fo:block>
						</fo:table-cell>
					</fo:table-row>
					<fo:table-row>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Max und Moritz</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Zu Hause 1a</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">12345 Musterstadt</fo:block>
						</fo:table-cell>
					</fo:table-row>
				</fo:table-body>
			</fo:table>
			<fo:block id="LastPage"/>
		</fo:flow>
	</fo:page-sequence>
</fo:root>
	

Top


Example
Sitemap Kontakt Impressum