2012-02-25 90 views
6

我想對xsl-fo進行xslt轉換,但我不確定我能做到這一點。我嘗試將XML列表轉換爲xsl-fo列表。任何人都可以告訴我在哪裏可以找到我長時間使用谷歌搜索是沒有很多這樣的例子。我的XML就是這樣。Xslt到xsl-fo轉換

<p>TEXT</p> 
<ul> 
    <li>Item1</li> 
    <li>Item2</li> 
</ul> 
<p>ANOTHERTEXT</p> 

我嘗試使用的模板,這種轉變,但是我的模板不工作得到XSL-FO可有人告訴我,如果模板在這一轉變的工作。如果他們工作可以讓我看到一個例子,我找不到任何人。我objetive是得到一個PDF絲毫FOP

感謝


這是我的XML文檔的一部分,我recived在HTML源的某些部分,並更改HTML到XML現在我嘗試轉換XML(白衣一列表)轉換爲XSLT的XSL-FO。我的問題是,我不能模仿這種轉變。我的最終目標是得到一個pdf惠普FOP。

感謝

UPDATE

這是我的XML:

<Memoria> 
    <name>TITLE</name> 
    <Index>INDEX 2010</Index> 
    <Seccion> 
    <name>INFORMATION</name> 
    <Contenido> 
     <p>TEXT</p> 
     <ul> 
    <li>ITEM1</li> 
    <li>ITEM2</li> 
     </ul> 
     <p>ANOTHER</p> 
    </Contenido> 
    </Seccion> 
</Memoria> 

我測試您的解決方案感謝所有

+1

HTML不具有100%的翻譯XSL-FO。 – driis 2012-02-25 18:30:39

+0

什麼是確切的預期結果?請編輯問題並提供。 XSLT專家很少也是xsl-fo-cognizant。 – 2012-02-25 18:47:12

回答

6

如果遇到與你的模板不工作的問題,這可能是一個命名空間的問題。您應該使用更精確的XML示例更新問題。

下面是一個例子。

XML輸入(固定爲合格的)

<root> 
    <p>TEXT</p> 
    <ul> 
    <li>Item1</li> 
    <li>Item2</li> 
    </ul> 
    <p>ANOTHERTEXT</p> 
</root> 

XSLT 1。0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/root"> 
    <fo:root> 
     <fo:layout-master-set> 
     <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in"> 
      <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/> 
     </fo:simple-page-master> 
     </fo:layout-master-set> 
     <fo:page-sequence master-reference="my-page"> 
     <fo:flow flow-name="xsl-region-body"> 
      <xsl:apply-templates/> 
     </fo:flow> 
     </fo:page-sequence> 
    </fo:root> 
    </xsl:template> 

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

    <xsl:template match="ul"> 
    <fo:list-block padding="4pt"> 
     <xsl:apply-templates/> 
    </fo:list-block> 
    </xsl:template> 

    <xsl:template match="li"> 
    <fo:list-item> 
     <fo:list-item-label end-indent="label-end()"> 
     <fo:block>&#x02022;</fo:block> 
     </fo:list-item-label> 
     <fo:list-item-body start-indent="body-start()"> 
     <fo:block> 
      <xsl:apply-templates/> 
     </fo:block> 
     </fo:list-item-body> 
    </fo:list-item>  
    </xsl:template> 
</xsl:stylesheet> 

XSL-FO輸出

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <fo:layout-master-set> 
     <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in"> 
     <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/> 
     </fo:simple-page-master> 
    </fo:layout-master-set> 
    <fo:page-sequence master-reference="my-page"> 
     <fo:flow flow-name="xsl-region-body"> 
     <fo:block>TEXT</fo:block> 
     <fo:list-block padding="4pt"> 
      <fo:list-item> 
       <fo:list-item-label end-indent="label-end()"> 
        <fo:block>•</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="body-start()"> 
        <fo:block>Item1</fo:block> 
       </fo:list-item-body> 
      </fo:list-item> 
      <fo:list-item> 
       <fo:list-item-label end-indent="label-end()"> 
        <fo:block>•</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="body-start()"> 
        <fo:block>Item2</fo:block> 
       </fo:list-item-body> 
      </fo:list-item> 
     </fo:list-block> 
     <fo:block>ANOTHERTEXT</fo:block> 
     </fo:flow> 
    </fo:page-sequence> 
</fo:root> 

Apache防輸出

enter image description here

0

首先,確保你有一個XHTML文件(無<等等)。然後應用xslt轉換來創建fo文件,然後將其提供給fop並顯示pdf。 XSLT的

片段FO風格:

<xsl:template match="html:body"> 
    <fo:page-sequence master-reference="all-pages"> 
     <fo:title> 
     <xsl:value-of select="/html:html/html:head/html:title"/> 
     </fo:title> 
     <fo:static-content flow-name="page-header"> 
     <fo:block font-weight="bold" font-size="16pt" space-before.conditionality="retain" xsl:use-attribute-sets="page-header"><!-- space-before="{$page-header-margin}" --> 
      <xsl:if test="$title-print-in-header = 'true'"> 
      <xsl:value-of select="/html:html/html:head/html:title"/> 
      </xsl:if> 
     </fo:block> 
     </fo:static-content> 
    </fo:page-sequence> 
    </xsl:template> 

檢查http://www.w3schools.com/xslfo/default.asp爲XSLFO語法。

查看http://xmlgraphics.apache.org/fop/trunk/running.html有關運行fop的信息; fop.jar的下載必須在附近。

從VBA例如運行如下:

Set shell = CreateObject("WScript.Shell") 
    cmd = "java -Dfop.home=" & baseDir & " -cp " & baseDir & "build\fop.jar org.apache.fop.cli.Main -fo " & foName & " -pdf " & pdfName 
Call shell.Run(cmd, vbWindowFrame, True) 

(類似命令行)