2014-07-09 77 views
0

我有類似的問題:Using XSLT to create XSL-FO with nested bold/italic tags。我想要檢測XML文本中的<italic><bold>標記,並將其標記爲使用XSLT格式化。 我試過它像其他問題的解決方案,但它似乎不適合我。我錯過了什麼?使用xpath和/或xsl-fo格式化斜體/粗體標記

這是我的XML結構:

<bibliography> 
    <type1> 
     Some text and <italic>italic Text</italic> and <bold>bold text</bold> 
    </type1> 
    <type2> 
     Some text and <italic>italic Text</italic> and <bold>bold text</bold> 
    </type2> 
</bibliography> 

這XSL工作,但沒有<italic><bold>標籤:

<xsl:template match="/bibliography/*"> 
    <p> 
     <div class="entry{@type}"> 
    [<xsl:number count="*"/>] 
    <xsl:apply-templates/> 
     </div> 
    </p> 
</xsl:template> 

這是我試圖用我的XML結構的解決方案:

<xsl:template match="/bibliography/*"> 
    <p> 
     <div class="entry{@type}"> 
    [<xsl:number count="*"/>] 
    <xsl:apply-templates/> 
     </div> 
    </p> 
</xsl:template> 
<xsl:template match="/"> 
    <div class="entry{@type}"> 
     <p> 
      <fo:root> 
       <fo:page-sequence> 
        <fo:flow> 
         <xsl:apply-templates select="bibliography"/> 
        </fo:flow> 
       </fo:page-sequence> 
      </fo:root> 
     </p> 
    </div> 
</xsl:template> 
<xsl:template match="italic"> 
    <fo:inline font-style="italic"> 
     <xsl:apply-templates select="node()"/> 
    </fo:inline> 
</xsl:template> 

<xsl:template match="bold"> 
    <fo:inline font-weight="bold"> 
     <xsl:apply-templates select="node()"/> 
    </fo:inline> 
</xsl:template> 

回答

1

除了你的XSL輸出混合的HTML和d XSL-FO,它實際上似乎拿起了「大膽」和「斜體」標籤。

如果你是純XSL-FO後,再看着你提到的問題,它並不需要太多的工作,使其與您的XML

<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" indent="yes"/> 

    <xsl:template match="bibliography"> 
     <fo:root> 
      <fo:page-sequence> 
       <fo:flow> 
        <xsl:apply-templates /> 
       </fo:flow> 
      </fo:page-sequence> 
     </fo:root> 
    </xsl:template> 

    <xsl:template match="bibliography/*"> 
     <fo:block font-size="16pt" space-after="5mm"> 
      <xsl:apply-templates /> 
     </fo:block> 
    </xsl:template> 

    <xsl:template match="bold"> 
     <fo:inline font-weight="bold"> 
      <xsl:apply-templates/> 
     </fo:inline> 
    </xsl:template> 

    <xsl:template match="italic"> 
     <fo:inline font-style="italic"> 
      <xsl:apply-templates /> 
     </fo:inline> 
    </xsl:template> 
</xsl:stylesheet> 

當然,原因之一是工作可能不起作用,如果您的實際XML具有名稱空間聲明,則可能是這樣。在這種情況下,您還需要在XSLT中聲明它,並相應地調整模板匹配。

+0

我讀過XSL-Fo僅適用於PDF而不適用於HTML。這是否意味着沒有選項可以將fo對象導出爲HTML?在沒有XSL-FO的情況下,是否可以添加''和''? – Peter

+0

xsl-fo通常用於輸出HTML。如果你想在瀏覽器中顯示結果,只需修改它來輸出HTML標籤。我看到你已經爲此提出了另一個問題,我用這個答案的一個變體來回答。我希望它有幫助。 –

相關問題