2015-04-23 30 views
0

我有一個XML通過Apache FOP轉換爲PDF,嵌入到Java程序和XSLT中。這個XML包含幾個項目列表;這些名單是在XML的格式是這樣的:Apache FOP:在不知道列表大小的情況下從PDF中打印XML列表的內容?

<NameOfList> 
    <Listitem> 
     <ListItemAttributeOne/> 
     <ListItemAttributeTwo/> 
    </ListItem> 
    <ListItem> 
     <ListItemAttributeOne/> 
     <ListItemAttributeTwo/> 
    </ListItem> 
    <...more ListItems> 
</NameOfList> 

我不知道提前多少listItems中也有,我需要打印的信息在PDF文件中是這樣的:

(1)列表項屬性的一個:
列表項屬性的兩個:
(2)列表項屬性的一個:
列表項屬性的兩個:
(...)
(n)的列表項屬性一:
列出項目屬性兩個:

我通常是一個Java開發人員,所以我知道如何用Java做到這一點:獲取ListItem對象的列表,將它們存儲在自定義類型「ListItem」的ArrayList中,然後循環訪問ArrayList和打印出它們的相關屬性,增加每個新項目的標籤(1,2等)。

是否有類似的方式來使用XSLT 2.0來做到這一點?你可以從XML中讀取一個列表到一個數組中,並在動態生成的列表中一次打印出一個項目嗎?

+0

你能說明XSL-FO應該是什麼樣子嗎?如果沒有,我們需要對PDF輸出的描述非常清楚(目前還不清楚)。 –

回答

1

這是一個XSLT 1.0(你甚至不需要用XSLT 2.0引入的功能)是改變你輸入在XSL-FO列表:

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 method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/"> 
     <fo:root> 
      <fo:layout-master-set> 
       <fo:simple-page-master master-name="simple" margin="0.5in"> 
        <fo:region-body/> 
        </fo:simple-page-master> 
      </fo:layout-master-set> 
      <fo:page-sequence master-reference="simple"> 
       <fo:flow flow-name="xsl-region-body"> 
        <xsl:apply-templates/> 
       </fo:flow> 
      </fo:page-sequence> 
     </fo:root> 
    </xsl:template> 

    <xsl:template match="NameOfList"> 
     <fo:list-block provisional-distance-between-starts="2cm" provisional-label-separation="2mm"> 
      <xsl:apply-templates select="*"/> 
     </fo:list-block> 
    </xsl:template> 

    <xsl:template match="ListItem"> 
     <fo:list-item> 
      <fo:list-item-label end-indent="label-end()"> 
       <fo:block>(<xsl:value-of select="position()"/>)</fo:block> 
      </fo:list-item-label> 
      <fo:list-item-body start-indent="body-start()"> 
       <xsl:apply-templates/> 
      </fo:list-item-body> 
     </fo:list-item> 
    </xsl:template> 

    <xsl:template match="ListItemAttributeOne"> 
     <fo:block>List Item Attribute One: <xsl:value-of select="."/></fo:block> 
    </xsl:template> 

    <xsl:template match="ListItemAttributeTwo"> 
     <fo:block>List Item Attribute Two: <xsl:value-of select="."/></fo:block> 
    </xsl:template> 
</xsl:stylesheet> 

你可能需要根據您的具體需求(頁面大小,頁邊距,字體...)進行調整,但它應該給你一個大致的想法。

+0

這是完美的!我確實需要爲字體調整一下,但這正是我所期待的。非常感謝你! –

相關問題