Krishnan
Krishnan

Reputation: 1050

How to create an XSL FO From an XHTML File? How can I can convert XHTML to XSLFO?

I have an XHTML Template. I want to create an XSL FO from the XHTML Template. How can I can convert XHTML to XSLFO? I created XSL FO by using string replacement of XHTML File. Is it possible to create XSL FO for a template with out using string replacement? Below is my XHTML template.

< div>

{:header:}{:date:}

< p>

{:mailingattn:} < br />

{:facilityname:}  < br />

{:facilitystreet:} < br />

{:facilitystreet2:} <br />

{:facilitycity:}, {:facilitystate:}  {:facilityzip:} < br />

{:facilitycountry:}

< /p>

< br />

< p>abcd,</p>

< p>The Department of abcd:< /p>

< p>{:checkreturnreason:}< /p>

< p>{:message:}< /p>

< pnext>If you have any questions or need assistance, please feel free to abcd:< /pnext>

< pnext>Sincerely, <br />

{:signature:}

< /pnext>

{:footer:}

< /div> 

The Predefined XSL FO Template

< xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">

  < xsl:output method="xml"/>


  < !--
  expects a rootpath attribute in one of the tags. this should correspond to the
  physical path of the apps folder without the ending slash
  -->
  < xsl:variable name="rootpath"
                select="//@rootpath" />


  < xsl:template match="/">
      < xsl:apply-templates/>
  < /xsl:template>

  < xsl:template match="div">
      < xsl:apply-templates/>
  < /xsl:template>


 < xsl:template match="img">
    < fo:external-graphic height="auto" width="auto">
      < xsl:attribute name="src">
        < xsl:value-of select="concat($rootpath, substring-after(@src,'../..'))" />
      < /xsl:attribute>
    < /fo:external-graphic>
  < /xsl:template>


  < xsl:template match="xsl:value-of">
    < xsl:copy-of select="." />
  < /xsl:template>


  < xsl:template match="br">
    < fo:block>
    < /fo:block>
  < /xsl:template>


  < xsl:template match="p">
    < fo:block space-before="8pt" space-after="4pt">
       < xsl:apply-templates/>
    < /fo:block>
  < /xsl:template>

  < xsl:template match="pnext">
    < fo:block keep-with-next="always" space-before="8pt" space-after="4pt">
       < xsl:apply-templates/>
    < /fo:block>
  < /xsl:template>


  < xsl:template match="table">
    < fo:table width="100%" font-family="Arial">
      < fo:table-body>
        < xsl:for-each select="tr">
          < fo:table-row>
            < xsl:for-each select="td">
              < xsl:variable name="width">
                < xsl:value-of select="@width"/>
              < /xsl:variable>
              < fo:table-cell padding-top='4pt' width="{$width}">
                < xsl:for-each select="span">
                  < xsl:variable name="fsize">
                    < xsl:value-of select="@size"/>
                  < /xsl:variable>
                  < fo:block font-size="{$fsize}pt" text-align="right">
                    < xsl:apply-templates/>
                  < /fo:block>
                < /xsl:for-each>
              < /fo:table-cell>
            < /xsl:for-each>
          < /fo:table-row>
        < /xsl:for-each>
        < fo:table-row>
          < fo:table-cell border=".2pt solid black">
          < /fo:table-cell>
          < fo:table-cell border=".2pt solid black">
          < /fo:table-cell>
        < /fo:table-row>
      < /fo:table-body>
    < /fo:table>
  < /xsl:template>


< /xsl:stylesheet>

Upvotes: 0

Views: 2379

Answers (2)

z--
z--

Reputation: 2236

http://www.ibm.com/developerworks/xml/library/x-xslfo2app/index.html

has a comprehensive XSLT to convert XHTML to XSL-FO

Summary: Need help converting HTML documents to PDF? This reference guide shows by example how to use XSLT templates to convert 45 commonly used HTML elements to formatting objects (from the XSL-FO vocabulary) for easy transformation to PDF using XSLT. The examples assume that you're using the Apache XML Project's FOP tool, but most of the methods work just as well with other XSL-FO tools.

Upvotes: 1

Garrett Vlieger
Garrett Vlieger

Reputation: 9494

This has worked for me:

        //
        // Transform well-stuctured XHTML into XSL-FO.
        //
        var trans = new XslCompiledTransform();
        var xml = new MemoryStream();
        XmlWriter writer = new XmlTextWriter(xml, System.Text.Encoding.Default);

        var converterStream = File.Open("YourXsl.xsl");

        trans.Load(new XmlTextReader(converterStream));           

        // Load XHTML
        var html = File.Open("YourHtml.html");

        var outputStream = new MemoryStream(Encoding.ASCII.GetBytes(viewOutput));

        outputStream.Position = 0;

        using (var input = new XmlTextReader(outputStream)) {
            // Transform XHTML to XSL-FO
            trans.Transform(input, writer);
            writer.Flush();
            xml.Position = 0;

            // Do something with your XSL-FO
        }

Upvotes: 0

Related Questions