2013-02-12 28 views
0

我有一个XHTML模板。 我想从XHTML模板创建一个XSL FO。 我该如何将XHTML转换为XSLFO? 我使用XHTML文件的字符串替换创建了XSL FO。 是否有可能为没有使用字符串替换的模板创建XSL FO? 以下是我的XHTML模板。如何从XHTML文件创建XSL FO?我怎样才能将XHTML转换为XSLFO?

< 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> 

预定义的XSL FO模板

< 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> 
+0

请显示您想要的XSL-FO以及您当前拥有的XSLT。 – 2013-02-12 13:31:04

+0

嗨丹尼尔我必须使用预定义的模板生成XSL FO。现在我在问题下面添加了预定义的模板。 – Krishnan 2013-02-12 13:36:22

+0

现在你的实际问题是什么? – 2013-02-12 13:44:40

回答

0

这工作对我来说:

 // 
     // 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 
     } 
1

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

有一个全面的XSLT把XHTML转换为XSL-FO

总结:需要帮助将HTML文档转换为PDF?本参考指南通过示例演示如何使用XSLT模板将45个常用HTML元素转换为格式化对象(来自XSL-FO词汇表),以便使用XSLT轻松转换为PDF。这些示例假定您正在使用Apache XML Project的FOP工具,但大多数方法与其他XSL-FO工具一样正常工作。