2014-02-11 56 views
2

我需要使用連字符作爲應用xsl:fo的嵌套列表上的項目符號。我有兩個模板可以匹配,但只有一個正在應用。如果我只使用第一個模板,外部列表將獲取應用的模板。如果我只使用第二個模板,則嵌套列表將獲取應用的模板。我試圖在第二個模板中匹配的模式是以列表項作爲父項的任何無序列表。不勝感激任何幫助獲得我想要的輸出。xsl:fo模板匹配不在嵌套列表上觸發

XML

<ThisNode> 
<ul> 
    <li>Item One</li> 
    <li>Item Two</li> 
    <li>Item Three 
     <ul> 
      <li>Sub-Item One</li> 
      <li>Sub-Item Two</li> 
     </ul> 
    </li> 
    <li>Item Four</li> 
    <li>Item Five</li> 
</ul> 
</ThisNode> 

XSLT

<xsl:template match="ul"> 
<fo:list-block> 
    <xsl:for-each select="./li"> 
     <fo:list-item> 
      <fo:list-item-label end-indent="label-end()"> 
       <fo:block font-weight="bold">•</fo:block> 
      </fo:list-item-label> 
      <fo:list-item-body start-indent="8pt"> 
       <fo:block><xsl:value-of select="."/></fo:block> 
      </fo:list-item-body> 
     </fo:list-item> 
    </xsl:for-each> 
</fo:list-block> 
</xsl:template> 

<xsl:template match="li//ul"> 
<fo:list-block start-indent="8pt"> 
    <xsl:for-each select="./li"> 
     <fo:list-item> 
      <fo:list-item-label end-indent="label-end()"> 
       <fo:block font-weight="bold">-</fo:block> 
      </fo:list-item-label> 
      <fo:list-item-body start-indent="16pt"> 
       <fo:block><xsl:value-of select="."/></fo:block> 
      </fo:list-item-body> 
     </fo:list-item> 
    </xsl:for-each> 
</fo:list-block> 
</xsl:template> 

<fo:block text-align="left"> 
    <xsl:apply-templates select="./ThisNode"/> 
</fo:block> 

所需的輸出

• Item One 
• Item Two 
• Item Three 
    - Sub-Item One 
    - Sub-Item Two 
• Item Four 
• Item Five 

實際輸出使用要麼只是第一模板或同時使用

• Item One 
• Item Two 
• Item ThreeSub-Item OneSub-Item Two 
• Item Four 
• Item Five 

實際產量僅使用第二個模板

Item One Item Two Item Three 
    - Sub-Item One 
    - Sub-Item Two 
Item Four Item Five 

回答

2

這看起來像一個call-template的好地方與mode,區分2例之間。外部模板調用內部模板:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 

    <xsl:template match="/"> 
     <fo:block text-align="left"> 
     <xsl:apply-templates select="ThisNode"/> 
     </fo:block> 
    </xsl:template> 


    <xsl:template match="ul"> 
     <fo:list-block> 
     <xsl:for-each select="li"> 
      <fo:list-item> 
       <fo:list-item-label end-indent="label-end()"> 
        <fo:block font-weight="bold">•</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="8pt"> 
        <fo:block> 
        <!-- I'm not quite sure what you want here --> 
        <xsl:value-of select="text()"/> 
        <xsl:apply-templates select="ul" mode="inner"/> 
        </fo:block> 
       </fo:list-item-body> 
      </fo:list-item> 
     </xsl:for-each> 
     </fo:list-block> 
    </xsl:template> 

    <xsl:template match="ul" mode="inner"> 
     <fo:list-block start-indent="8pt"> 
     <xsl:for-each select="./li"> 
      <fo:list-item> 
       <fo:list-item-label end-indent="label-end()"> 
        <fo:block font-weight="bold">-</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="16pt"> 
        <fo:block> 
        <xsl:value-of select="."/> 
        </fo:block> 
       </fo:list-item-body> 
      </fo:list-item> 
     </xsl:for-each> 
     </fo:list-block> 
    </xsl:template> 

</xsl:stylesheet> 
2

以下樣式表說明了一種可能的解決方案。您的方法並不遙遠 - 但是,它無法解釋XSLT處理器的行爲。此表達式:

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

默認返回上下文節點的子節點的任何文本節點。但在你的情況下(模板匹配li元素),全部後代文本節點都輸出,不僅是li的直接子節點。

因此,樣式表下面使用

<xsl:value-of select="child::text()"/> 

檢索li元素的文本內容,而不是與<xsl:apply-templates>處理任何li元素是它的子項目。

當你的標題狀態,

XSL:FO模板匹配不嵌套列表

在射擊您正確診斷它。這是因爲 - 在模板匹配li之後 - 您不會讓XSLT處理器處理後代li元素。

樣式表

<?xml version="1.0" encoding="utf-8"?> 

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

    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/ThisNode"> 
     <fo:root> 
     <fo:layout-master-set> 
      <fo:simple-page-master master-name="simple" 
           page-height="29.7cm" 
           page-width="21cm" 
           margin-top="1cm" 
           margin-bottom="2cm" 
           margin-left="2.5cm" 
           margin-right="2.5cm"> 
       <fo:region-body margin-top="3cm"/> 
       <fo:region-before extent="3cm"/> 
       <fo:region-after extent="1.5cm"/> 
      </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="ul[parent::ThisNode]"> 
     <fo:list-block> 
     <xsl:apply-templates/> 
     </fo:list-block> 
    </xsl:template> 

    <xsl:template match="ul[parent::li]"> 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="li"> 
     <fo:list-item> 
     <xsl:choose> 
      <xsl:when test="parent::ul/parent::ThisNode"> 
       <fo:list-item-label start-indent="1cm"> 
        <fo:block font-weight="bold">&#x2022;</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="1.5cm"> 
        <fo:block><xsl:value-of select="child::text()"/></fo:block> 
       </fo:list-item-body> 
      </xsl:when> 
      <xsl:otherwise> 
       <fo:list-item-label start-indent="3cm"> 
        <fo:block font-weight="bold">-</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="3.5cm"> 
        <fo:block><xsl:value-of select="."/></fo:block> 
       </fo:list-item-body> 
      </xsl:otherwise> 
     </xsl:choose> 
     </fo:list-item> 
     <xsl:apply-templates/> 
    </xsl:template> 

</xsl:stylesheet> 

輸出(XSL-FO)

<?xml version="1.0" encoding="UTF-8"?> 
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <fo:layout-master-set> 
     <fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21cm" margin-top="1cm" 
          margin-bottom="2cm" 
          margin-left="2.5cm" 
          margin-right="2.5cm"> 
     <fo:region-body margin-top="3cm"/> 
     <fo:region-before extent="3cm"/> 
     <fo:region-after extent="1.5cm"/> 
     </fo:simple-page-master> 
    </fo:layout-master-set> 
    <fo:page-sequence master-reference="simple"> 
     <fo:flow flow-name="xsl-region-body"> 
     <fo:list-block> 
      <fo:list-item> 
       <fo:list-item-label start-indent="1cm"> 
        <fo:block font-weight="bold">•</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="1.5cm"> 
        <fo:block>Item One</fo:block> 
       </fo:list-item-body> 
      </fo:list-item>Item One 
    <fo:list-item> 
       <fo:list-item-label start-indent="1cm"> 
        <fo:block font-weight="bold">•</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="1.5cm"> 
        <fo:block>Item Two</fo:block> 
       </fo:list-item-body> 
      </fo:list-item>Item Two 
    <fo:list-item> 
       <fo:list-item-label start-indent="1cm"> 
        <fo:block font-weight="bold">•</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="1.5cm"> 
        <fo:block>Item Three 

    </fo:block> 
       </fo:list-item-body> 
      </fo:list-item>Item Three 

      <fo:list-item> 
       <fo:list-item-label start-indent="3cm"> 
        <fo:block font-weight="bold">-</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="3.5cm"> 
        <fo:block>Sub-Item One</fo:block> 
       </fo:list-item-body> 
      </fo:list-item>Sub-Item One 
      <fo:list-item> 
       <fo:list-item-label start-indent="3cm"> 
        <fo:block font-weight="bold">-</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="3.5cm"> 
        <fo:block>Sub-Item Two</fo:block> 
       </fo:list-item-body> 
      </fo:list-item>Sub-Item Two 


    <fo:list-item> 
       <fo:list-item-label start-indent="1cm"> 
        <fo:block font-weight="bold">•</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="1.5cm"> 
        <fo:block>Item Four</fo:block> 
       </fo:list-item-body> 
      </fo:list-item>Item Four 
    <fo:list-item> 
       <fo:list-item-label start-indent="1cm"> 
        <fo:block font-weight="bold">•</fo:block> 
       </fo:list-item-label> 
       <fo:list-item-body start-indent="1.5cm"> 
        <fo:block>Item Five</fo:block> 
       </fo:list-item-body> 
      </fo:list-item>Item Five 
</fo:list-block> 
     </fo:flow> 
    </fo:page-sequence> 
</fo:root> 

輸出(PDF)

enter image description here