2015-08-23 51 views
2

我的源XML的樣子:XSLT FO列表元素

<events> 
    <entry> 
     <event>Event 1</event> 
     <event>Event 2</event> 
     <event>Event 3</event> 
     <event>Event 4</event> 
    </entry> 
</events> 

這裏是我的XSL轉換的相應代碼:

<fo:block-container> 
    <fo:list-block> 
    <xsl:for-each select="//event"> 
     <fo:list-item> 
     <fo:list-item-label/> 
      <fo:list-item-body> 
       <fo:block> 
       <xsl:value-of select="//event"/> 
       </fo:block> 
      </fo:list-item-body> 
     </fo:list-item> 
    </xsl:for-each> 
    </fo:list-block> 
</fo:block-container> 

而且FO輸出:

<fo:list-block> 
    <fo:list-item> 
    <fo:list-item-label/> 
     <fo:list-item-body> 
     <fo:block>Event 1Event 2Event 3Event 4</fo:block> 
     </fo:list-item-body> 
    </fo:list-item> 
    <fo:list-item> 
    <fo:list-item-label/> 
     <fo:list-item-body> 
     <fo:block>Event 1Event 2Event 3Event 4</fo:block> 
     </fo:list-item-body> 
    </fo:list-item> 
    <fo:list-item> 
    <fo:list-item-label/> 
     <fo:list-item-body> 
     <fo:block>Event 1Event 2Event 3Event 4</fo:block> 
     </fo:list-item-body> 
    </fo:list-item> 
    <fo:list-item> 
    <fo:list-item-label/> 
     <fo:list-item-body> 
     <fo:block>Event 1Event 2Event 3Event 4</fo:block> 
     </fo:list-item-body> 
    </fo:list-item> 

我問題在於每個事件元素都應該轉換爲單獨的fo:list-item,如:

<fo:list-block> 
    <fo:list-item> 
    <fo:list-item-label/> 
     <fo:list-item-body> 
     <fo:block>Event 1</fo:block> 
     </fo:list-item-body> 
    </fo:list-item> 
    <fo:list-item> 
    <fo:list-item-label/> 
     <fo:list-item-body> 
     <fo:block>Event 2</fo:block> 
     </fo:list-item-body> 
    </fo:list-item> 
    <fo:list-item> 
    <fo:list-item-label/> 
     <fo:list-item-body> 
     <fo:block>Event 3</fo:block> 
     </fo:list-item-body> 
    </fo:list-item> 
    <fo:list-item> 
    <fo:list-item-label/> 
     <fo:list-item-body> 
     <fo:block>Event 4</fo:block> 
     </fo:list-item-body> 
    </fo:list-item> 

我希望你能幫助我......

回答

4

而不是

<xsl:value-of select="//event"/> 

使用

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

你要輸出的電流事件,畢竟,不是所有的人。


更概括地說,我建議從<xsl:for-each>改變你的XSLT程序遠向<xsl:template>/<xsl:apply-templates>基於形式:

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

<xsl:template match="events/entry"> 
    <fo:list-block> 
    <xsl:apply-templates /> 
    <fo:list-block> 
</xsl:template> 

<xsl:template match="events/entry/event"> 
    <fo:list-item> 
    <fo:list-item-label/> 
    <fo:list-item-body> 
     <fo:block> 
     <xsl:value-of select="."/> 
     </fo:block> 
    </fo:list-item-body> 
    </fo:list-item> 
</xsl:template> 

這種方法更加模塊化,具有更好的可重用性,是總體來說不是那麼深的嵌套。

1

<xsl:value-of select="//event"/>替換爲<xsl:value-of select="."/>作爲for-each中的event元素是上下文節點。